So my problem is I have to create this maze game. Everything works fine except moving south in the "maze". So I scan where the player wants to go and based on that edit the place of the player "P" in an array. This works fine to go to north, east or west but not for south.
Background on the code: So I have a 11*11 array and in one of the elements this is equal to P. Based on user input this P goes up north or south or east or west, provided there is no wall (displayed as ---).
for (int i = 0; i < currentPos.length; i++) {
for (int j = 0; j < currentPos[0].length; j++) {
if (currentPos[i][j].equals("P")) {
if (direction.equals("north")) {
if (currentPos[i - 1][j].equals("---")) {
continue;
} else {
currentPos[i][j] = " ";
currentPos[i - 2][j] = "P";
break;
}
}
if (direction.equals("south")) {
if (currentPos[i + 1][j].equals("---")) {
continue;
} else {
currentPos[i][j] = " ";
currentPos[i + 2][j] = "P";
break;
}
}
}
}
}
The import part would be the [i-2][j] or the [i+2][j] part I guess. I only placed two directions here, but the other 2 work just fine with [i][j-2] and [i][j+2].
Before I checked for walls (the if [i-1][j].equals("---")){), the south would always give an out of bound error. Now that I check for a wall it just goes south until it hits a wall.