SO im making a program the will solve a maze recursively and im having problems with my if else statements. I had a similar problem in a different part of my code and i solved it by just taking out all the else's and just having a bunch of if statements. But now i need it the stop searching after it finds the correct way to go.
public static void solveMaze(int ROW, int COL){
int isFinished = 0;
//ROW COL
//set up base case. find end if possible to get in 1 move then end
if(drawArray[ROW][COL+1] == ' '){
if(drawArray[ROW][COL+2] == 'E'){
isFinished = 1;
move(ROW,COL,ROW,COL+2);
}
}
if(drawArray[ROW][COL-1] == ' '){
if(drawArray[ROW][COL-2] == 'E'){
isFinished = 1;
move(ROW,COL,ROW,COL-2);
}
}
if(drawArray[ROW+1][COL] == ' '){
if(drawArray[ROW+2][COL] == 'E'){
isFinished = 1;
move(ROW,COL,ROW+2,COL);
}
}
if(drawArray[ROW-1][COL] == ' '){
if(drawArray[ROW-2][COL] == 'E'){
isFinished = 1;
move(ROW,COL,ROW-2,COL);
}
}
//find first open cell and choose it
int foundOpen = 0;
if(isFinished == 0){
if(drawArray[ROW][COL+1] == ' ' && drawArray[ROW][COL+2] != '*'){
drawArray[ROW][COL+2] = '*';
prevCol = COL;
prevRow = ROW;
COL+= 2;
foundOpen = 1;
}
else if(drawArray[ROW+1][COL] == ' ' && drawArray[ROW+2][COL] != '*'){
drawArray[ROW+2][COL] = '*';
prevCol = COL;
prevRow = ROW;
ROW+= 2;
foundOpen = 1;
}
else if(drawArray[ROW][COL-1] == ' ' && drawArray[ROW][COL-2] != '*'){
drawArray[ROW][COL-2] = '*';
prevCol = COL;
prevRow = ROW;
ROW-= 2;
foundOpen = 1;
}
else if(drawArray[ROW-1][COL] == ' ' && drawArray[ROW-2][COL] != '*'){
drawArray[ROW-2][COL] = '*';
prevCol = COL;
prevRow = ROW;
ROW-= 2;
foundOpen = 1;
}
}
//i have two recursive voids so this is chosing between them
if(foundOpen == 1){
move(prevRow,prevCol,ROW,COL);
solveMaze(ROW,COL);}
else if (foundOpen == 0 && isFinished == 0)
wrongChoice(ROW, COL);
}
SO basically this part of the program checks to see first if the end is near and if it is basically finish. I use to have if else statements for the first part to but i wouldnt work and the only way i could fix it was by taking out the else's. So in one maze that i have to solve the first move should be COL-1 == ' ' and COL-2 != '*'. so that if statement should happen but it doesnt. I put in a bunch of println statements to check where it was going off and isFInished == 0 so it starts into that if statement but then something goes wrong and nothing solution is found so wrongChoice is called. Im prety new to java and recursion so i might be missing something obvios.
you could System.out.print and check where you want it to stop then use @mserioli suggestion which will work. good luck
boolean isFinished = true;
if (drawArray[ROW][COL+1] == ' ' && drawArray[ROW][COL+2] == 'E') {
move(ROW,COL,ROW,COL+2);
} else if (drawArray[ROW][COL-1] == ' ' && drawArray[ROW][COL-2] == 'E') {
move(ROW,COL,ROW,COL-2);
} else if (drawArray[ROW+1][COL] == ' ' && drawArray[ROW+2][COL] == 'E') {
move(ROW,COL,ROW+2,COL);
} else if (drawArray[ROW-1][COL] == ' ' && drawArray[ROW-2][COL] == 'E') {
move(ROW,COL,ROW-2,COL);
} else {
isFinished = false;
}
int foundOpen = 0;
if (!isFinished) {
That should do.
If you do this kind of looking at naybors more often:
static final int[][] naybors = {
{ 0, 1 },
{ 0, -1 },
{ 1, 0 },
{ -1, 0 },
};
boolean isFinished = false;
for (int[] nb : naybors) {
if (drawArray[ROW + nb[0]][COL + nb[1]] == ' '
&& drawArray[ROW + 2*nb[0]][COL + 2*nb[1]] == 'E') {
move(ROW, COL, ROW + 2*nb[0], COL + 2*nb[1);
isFinished = true;
break;
}
}
Also saw:
else if(drawArray[ROW][COL-1] == ' ' && drawArray[ROW][COL-2] != '*'){
drawArray[ROW][COL-2] = '*';
prevCol = COL;
prevRow = ROW;
//Wrong: ROW-= 2;
COL -= 2; // Good?
foundOpen = 1;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.