I got this code, the idea is to check a stack (collection of grid position) and make sure it qualifies a certain pattern (stack to have unique elements and adherent stack elements are next to each other in the grid)
The problem I ran into is that when I reach the first if statement in the loop on the first loop round, the program jumps out of the loop and start back at the if statement just above the loop.
The original code is that I ran stack.pop() inside the loop, but I moved it out to check if that maybe it alternates the stack size and causes the error, but it still happens.
I also tried to see if my statement was the problem so I used if-else instead of if(!statement), and it passed, no problem
I don't think the error is the issue neither since it works with the statement above the loop.
If you guys have any ideas, please let me know. Thank you.
CODE:
if (stack.peek() != endPoint) {
error("Path does not end at maze exit");
}
int a = stack.size();
temp = stack.pop();
for (int i = a; i > 1; i--) {
if (checkNextTo(grid,temp).contains(stack.peek())) {}
else{cout<<"stack element is not valid ";}
checkDuplicated.add(temp);
if (!checkRepeat.contains(stack.peek())) {
error("stack element is duplicate");
}
temp = stack.pop();
}
if(stack.peek() != startPoint){
error("Element is not an entrance");
}
}
Aucun commentaire:
Enregistrer un commentaire