Search code examples
javaarraysmazedirection

java maze game error going in one direction only, using array


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.


Solution

  • break only breaks out of the innermost loop, so it just stops processing the current row, and your program then moves on to the next row.

    This is not a problem East or West: since the player ends up in the same row and you abort the row, you won't find the player again. It's also not a problem going North: since the player ends up in a previously processed row, you will not find it again.

    However, when you go South, you will find the player again in the next row and move them another step down. Then you'll encounter them again, and obviously move them again. Over and over.

    To avoid this, stop COMPLETELY after you've found the player. Do not keep looking for the player. Without knowing more about your code, one way to do this is to use a loop label to break out of both loops:

    outerloop:
    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 outerloop;
    
                    }
                }
                if (direction.equals("south")) {
                    if (currentPos[i + 1][j].equals("---")) {
                        continue;
                    } else {
                        currentPos[i][j] = "  ";
                        currentPos[i + 2][j] = "P";
    
                        break outerloop;
    
                    }
                }
            }
    
        }
    }