In the proceeding 2d array, the #'s represent the walls of the maze, and the dots represent squares in the possible paths through the maze. Moves can be made only to a location in the array that contains a dot... I need a recursive method mazeTraverse to "walk" through the maze. It should receive the array and the starting location of the maze as arguments. As it attempts to locate the exit from the maze, it should place the character 'X' in each square in the path. The method should display the maze after each move so the user can watch as the maze is solved.
I'm not really sure how to progress past this. Thank you to anyone who can help.
public class BonusMaze {
public static void main(String args[]){
char field[][]={
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','•','•','•','#','•','•','•','•','•','•','#',},
{'•','•','#','•','#','•','#','#','#','#','•','#',},
{'#','#','#','•','#','•','•','•','•','#','•','#',},
{'#','•','•','•','•','#','#','#','•','#','•','•',},
{'#','#','#','#','•','#','•','#','•','#','•','#',},
{'#','•','•','#','•','#','•','#','•','#','•','#',},
{'#','#','•','#','•','#','•','#','•','#','•','#',},
{'#','•','•','•','•','•','•','•','•','#','•','#',},
{'#','#','#','#','#','#','•','#','#','#','•','#',},
{'#','•','•','•','•','•','•','#','•','•','•','#',},
{'#','#','#','#','#','#','#','#','#','#','#','#'},
};
printField(field);
mazeTraverse(field,2,0);
}
public static void mazeTraverse(char[][] field, int x, int y){
field[2][0]='X';
if(field[x+1][y]=='•'){
field[x+1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y]=='•'){
field[x-1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='•'){
field[x][y+1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y-1]=='•'){
field[x][y-1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
}
public static void printField(char[][] field){
for(int x=0; x<11; x++){
for(int y=0; y<11; y++){
System.out.print(field[x][y]);
}
System.out.println();
}
System.out.print("\n\n");
}
}
I've changed it to this, but it's all wacky:
public class BonusMaze {
public static boolean east=true, north=false, south=false, west=false;
/*
east=false;
north=false;
south=false;
west=false;
*/
public static void main(String args[]){
char field[][]={
{'#','#','#','#','#','#','#','#','#','#','#','#'},
{'#','•','•','•','#','•','•','•','•','•','•','#',},
{'•','•','#','•','#','•','#','#','#','#','•','#',},
{'#','#','#','•','#','•','•','•','•','#','•','#',},
{'#','•','•','•','•','#','#','#','•','#','•','•',},
{'#','#','#','#','•','#','•','#','•','#','•','#',},
{'#','•','•','#','•','#','•','#','•','#','•','#',},
{'#','#','•','#','•','#','•','#','•','#','•','#',},
{'#','•','•','•','•','•','•','•','•','#','•','#',},
{'#','#','#','#','#','#','•','#','#','#','•','#',},
{'#','•','•','•','•','•','•','#','•','•','•','#',},
{'#','#','#','#','#','#','#','#','#','#','#','#'},
};
printField(field);
field[2][0]='X';
mazeTraverse(field,2,0);
}
public static void mazeTraverse(char[][] field, int x, int y){
if(x==2&&y==0){
field[2][1]='X';
mazeTraverse(field,2,1);
printField(field);
}
if(east){
if(field[x][y+1]=='•' || field[x+1][y]=='X'){
field[x][y+1]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
else if(west){
if(field[x-1][y]=='•' || field[x-1][y]=='X'){
field[x-1][y]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
else if(north){
if(field[x][y+1]=='•' || field[x][y+1]=='X'){
field[x][y+1]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
else if(south){
if(field[x][y-1]=='•' || field[x][y-1]=='X'){
field[x][y-1]='X';
}
if(field[x+1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
south=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+2]=='•'){
east=false;
north=false;
south=false;
west=false;
east=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y+1]=='•'){
east=false;
north=false;
south=false;
west=false;
north=true;
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='#'){
east=false;
north=false;
south=false;
west=false;
west=true;
printField(field);
mazeTraverse(field,x-1,y);
}
}
/*if(field[x+1][y]=='•'){
field[x+1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x-1][y]=='•'){
field[x-1][y]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y+1]=='•'){
field[x][y+1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}
else if(field[x][y-1]=='•'){
field[x][y-1]='X';
printField(field);
mazeTraverse(field,x+1,y);
}*/
}
public static void printField(char[][] field){
for(int x=0; x<12; x++){
for(int y=0; y<12; y++){
System.out.print(field[x][y]);
}
System.out.println();
}
System.out.print("\n\n");
}
}