Trying to restrict movement within tile based game

Viewed 112

NOOBIE Question... please help! I have a tile-based game I'm building where I'm moving an icon up down left and right. The dimensions of the game is 15 tiles x 15 tiles. When I'm approaching the boundaries, how can I prevent the icon from moving off the "map". I'm using W (up) A (left) S (down) and D (right) for directional input.

What would be a good way to prevent that icon from going off the map once it's reached the maximum point on the X or Y axis? I defined PlayerY and PlayerX as points where the player exists on the map, but this is the code i currently have for movement.

    if (choice.equals("w")){
        playerY--;
    }
    else if (choice.equals("s")){
        playerY++;
    }
    else if (choice.equals("d")){
        playerX++;
    }
    else if (choice.equals("a")){
        playerX--;
    }

Would you put something here where you say something like player is at max value and d inputted, do nothing? But I don't know how you'd say "do nothing"...

if (player Y == 15 && choice.equals("d")){
   ________;
}

Sorry again for the dumb question... I'm relatively new to Java and still trying to get my bearings

4 Answers

I suggest maintaining some constants for the maximum x and y size. Then, add logic which checks the boundaries before making any move:

final int X_DIM = 15;   // assuming the board be represented by an array, then 14 is the
final int Y_DIM = 15;   // maximum index

if (choice.equals("w")) {
    if (playerY > 0) {
        playerY--;
    }
    else {
        System.out.println("Error: Cannot make an off-board move");
    }
}
else if (choice.equals("s")) {
    if (playerY < Y_DIM - 1) {
        playerY++;
    }
    else {
        System.out.println("Error: Cannot make an off-board move");
    }
}
else if (choice.equals("d")) {
    if (playerX < Y_DIM - 1) {
        playerX++;
    }
    else {
        System.out.println("Error: Cannot make an off-board move");
    }
}
else if (choice.equals("a")) {
    if (playerX > 0) {
        playerX--;
    }
    else {
        System.out.println("Error: Cannot make an off-board move");
    }
}

Just check if it is on the last or first tiles or not, like this:

    if (choice.equals("w")){
        if ( playerY - 1 >= 0 ) { // if the next player move in Y axis is positive, then move
            playerY--;
        }
    }
    else if (choice.equals("s")){
        if ( playerY + 1 < 15 ) { // if the next player move in Y axis is less than 15 (because you have 15 tiles), then move
            playerY++;
        }
    }
    else if (choice.equals("d")){
        if ( playerX + 1 < 15 ) { // if the next player move in X axis is less than 15 (because you have 15 tiles), then move
            playerX++;
        }
    }
    else if (choice.equals("a")){
        if ( playerX - 1 >= 0 ) { // if the next player move in X axis is positive, then move
            playerX--;
        }
    }

Before make an move, you have to check whether the move is valid or not. If the move is valid, move it. If not, don't make the move and optionally show a warning for the player.

boolean isValidMove(choice){
    int nextX, nextY;
    if (choice.equals("w")){
        nextY = playerY-1;
    }
    else if (choice.equals("s")){
        nextY = playerY+1;
    }
    else if (choice.equals("d")){
        nextX = playerX+1;
    }
    else if (choice.equals("a")){
        nextX = playerX-1;
    }
    //immediately return false if X or Y out of board
    if(nextX<0||nextX>=15) return false;
    if(nextY<0||nextY>=15) return false;
    return true; //return true if nextX and nextY is in the board
}

In main method where run the game:

choice = //user input
if(isValidMove(choice)){
   //do the move request and represent board game
} else{
   //optionally show a warning to players 
}

I suggest starting with a helper method to determine whether or not any given position is on the map or not:

private boolean isOnMap(int x, int y) {
   return x >= 0 && x < 15
        && y >= 0 && y < 15;
}

Then, you can use that helper method to decide whether or not the move the user requested is valid, and only make the move if it is. I'd split the logic into two steps to make this easier to work with: first step is to naively calculate the new position that the user wants to move to, and then the second step is to decide if that new position is a valid move or not

    // Step 1:  calculate new position from requested move
    int newX = playerX;
    int newY = playerY;
    if (choice.equals("w")){
        newY--;
    }
    else if (choice.equals("s")){
        newY++;
    }
    else if (choice.equals("d")){
        newX++;
    }
    else if (choice.equals("a")){
        newY--;
    }

    // Step 2:  apply the new position, but only if the move is valid
    if (isOnMap(newX, newY)) {
        playerX = newX;
        playerY = newY;
    }
Related