I am trying to write a program that is given a maze and tries to find the way out. M is the entrance, E is the exit, 1s are walls, and 0s are pathways. It is supposed to find each path and put P in the path. It is supposed to find all available paths. Right now it finds part of a path.
Here is the code:
public class Maze
{
private int size;
private String[][] board;
private int total; //# of boards
private int eX;
private int eY;
private int mX;
private int mY;
public Maze( int size, String[][] board )
{
this.size = size;
this.board = board;
total = 0;
}
private void find( String c )
{
int x=0, y=0;
for( int i = 0; i < size; i++ )
{
for( int j = 0; j < size; j++ )
{
if( board[i][j].equals(c) )
{
x = i;
y = j;
}
}
}
if( c.equals("M") )
{
mX = x;
mY = y;
}
else if( c.equals("E") )
{
eX = x;
eY = y;
}
}
public void findPath( )
{
find( "M" );
find( "E" );
findNext( mX, mY );
}
public void findNext( int x, int y )
{
String last = board[x][y];
if( board[x][y].equals("P") ) board[x][y] = "1";
board[x][y] = "P";
if( rightAvailability(x,y) )
{
findNext(x+1, y);
}
else if( leftAvailability(x,y) )
{
findNext(x-1, y);
}
else if( aboveAvailability(x,y) )
{
findNext(x, y+1);
}
else if( belowAvailability(x,y) )
{
findNext(x, y-1);
}
else
{
total++;
printBoard();
}
board[x][y]= last;
}
public boolean rightAvailability( int x, int y )
{
if( x+1 >= size ) return false;
else if( board[x+1][y].equals("1") ) return false;
else if( board[x+1][y].equals("P") ) return false;
else return true;
}
public boolean leftAvailability( int x, int y )
{
if( x-1 < 0) return false;
else if( board[x-1][y].equals("1") ) return false;
else if( board[x-1][y].equals("P") ) return false;
else return true;
}
public boolean aboveAvailability( int x, int y )
{
if( y+1 >= size ) return false;
else if( board[x][y+1].equals("1") ) return false;
else if( board[x][y+1].equals("P") ) return false;
else return true;
}
public boolean belowAvailability( int x, int y )
{
if( y-1 < 0) return false;
else if( board[x][y-1].equals("1") ) return false;
else if( board[x][y-1].equals("P") ) return false;
else return true;
}
public void printBoard()
{
System.out.println( "The board number " +total+ " is: ");
for(int i=0; i< size; i++ )
{
for(int j=0; j< size; j++ )
{
if( (i==mX) && (j==mY) )
{
System.out.print("M");
}
else if( (i==eX) && (j==eY) )
{
System.out.print("E");
}
else if( board[i][j].equals("1") )
{
System.out.print("1");
}
else if( board[i][j].equals("0") )
{
System.out.print("0");
}
else
{
System.out.print("P");
}
}
System.out.println();
}
}
}
Here is the tester:
public class MazeTester
{
public static void main(String[] args)
{
int size = 11;
String[][] board = new String[][]
{
{"1","1","1","1","1","1","1","1","1","1","1"},
{"1","0","0","0","0","0","1","0","0","0","1"},
{"1","0","1","0","0","0","1","0","1","0","1"},
{"E","0","1","0","0","0","0","0","1","0","1"},
{"1","0","1","1","1","1","1","0","1","0","1"},
{"1","0","1","0","1","0","0","0","1","0","1"},
{"1","0","0","0","1","0","1","0","0","0","1"},
{"1","1","1","1","1","0","1","0","0","0","1"},
{"1","0","1","M","1","0","1","0","0","0","1"},
{"1","0","0","0","0","0","1","0","0","0","1"},
{"1","1","1","1","1","1","1","1","1","1","1"},
};
Maze m = new Maze( size, board );
m.findPath();
}
}
Here is the current output:
The board number 1 is:
11111111111
1PPPPP1PPP1
1P1PPP1P1P1
EP1PPPPP1P1
101111101P1
10101PPP1P1
10001P1PPP1
11111P1PP01
101M1P1PP01
100PPP1PP01
11111111111
The board number 2 is:
11111111111
1PPPPP1PPP1
1P1PPP1P1P1
EP100PPP1P1
101111101P1
10101PPP1P1
10001P1PPP1
11111P1PP01
101M1P1PP01
100PPP1PP01
11111111111
The board number 348 is:
11111111111
1PPPPP10001
1P1PPP10101
EP1PPPPP101
1011111P101
10101PPP101
10001P10001
11111P10001
101M1P10001
100PPP10001
11111111111
EDIT: I added if( board[x][y].equals("P") ) board[x][y] = "1"; at the beginning of findIndex. I also fixed the x <= 0 problem. I updated the output to what I am getting now (it is actually print 348 similar boards).