# Maze Solver

In my spare time this weekend I wrote a maze solving algorithm which is reminiscent of a program I wrote as homework over a year ago but slightly better. Its still wasteful in terms of efficient moves, but the improvements can come later.

```    private Point findStart()
{

for(int y = 0; y < m_mazeNodes.length; y++)
{
for(int x = 0; x < m_mazeNodes[y].length; x++)
{
if(m_mazeNodes[y][x].getState() == MazeNode.SlotValue.start)
return new Point(x, y);

}
}

return null;
}

public void solve()
{
Point start = findStart();
solveRecursive(start.x, start.y);
}

private void solveRecursive(int currentX, int currentY)
{
if(m_solved == true)
return;

if(isValidLocation(currentX, currentY) == false)
return;

if(m_mazeNodes[currentY][currentX].getState() == MazeNode.SlotValue.end)
{
m_solved = true;
return;
}

if(m_mazeNodes[currentY][currentX].getState() == MazeNode.SlotValue.visited)
return;

if(m_mazeNodes[currentY][currentX].getState() == MazeNode.SlotValue.wall)
return;

if (m_mazeNodes[currentY][currentX].getState() != MazeNode.SlotValue.start)
m_mazeNodes[currentY][currentX].setState(MazeNode.SlotValue.visited);

solveRecursive(currentX+1, currentY);
solveRecursive(currentX, currentY+1);
solveRecursive(currentX-1, currentY);
solveRecursive(currentX, currentY-1);

}

private boolean isValidLocation(int x, int y)
{
if(x < 0)
return false;
if(y < 0)
return false;
if(y > m_mazeNodes.length-1)
return false;
if(x > m_mazeNodes[y].length-1)
return false;

return true;
}```