forum

Home / DeveloperSection / Forums / How to prevent a user from going out of bounds in simple array game.

How to prevent a user from going out of bounds in simple array game.

Royce Roy 2039 30-Apr-2015
I am making a very simple 2D array game where the player is asked what size they would like the game board to be. After entering, it displays the board and the player 'P' starts at index [0][0]. After that, they are asked for an action, which can be "up", "down", "left", "right", or "exit". I will be including some extra later (like a treasure at the end, or random obstacles), but for now this is what the game consists of. 

I was instructed to "do nothing" when/if the player attempts to go out of bounds. I am trying to simply print an error, such as "Out of bounds! Try again.", then prompt the player again for an action. I even tried to make a boolean method to catch it, but to no avail. 

I don't want the exception to occur at all. I just simply want the error message to print to the player and ask for another action. I would prefer not to use try/catch, or try/catch/finally since I already tried that and it still gave the exception error. 

This program consists of two classes. I will show the class containing the main first, then the client-server type class second. 

import java.util.Scanner;
 
public class Driver {
     
     
     
    public static void main(String[] args) {
        
       World world = new World();
       boolean keepPlaying;
       keepPlaying = true;
       boolean isOutOfBounds;
       isOutOfBounds = false;
       int height = 0;
       int width = 0;
       int x = 0;
       int y = 0;
        
       Scanner input = new Scanner(System.in);
        
       System.out.print("Enter World width: ");
       width = input.nextInt();
       System.out.print("Enter World height: ");
       height = input.nextInt();
        
       world.displayWorld(height, width, x, y);
        
       input.nextLine();
        
       while(keepPlaying) {
        
       System.out.print("ACTION > ");
       String action = input.nextLine();
        
       if(world.isOutOfBounds()) {
            
           System.out.println("Out of bounds! Try again.");
            
           System.out.println("ACTION > ");
           input.nextLine();
            
        }
       else if(action.equalsIgnoreCase("down")) {
           world.moveDown(height, width, x, y);
            
        }
       else if(action.equalsIgnoreCase("up")) {
           world.moveUp(height, width, x, y);
            
        }
       else if(action.equalsIgnoreCase("left")) {
           world.moveLeft(height, width, x, y);
        }
       else if(action.equalsIgnoreCase("right")) {
           world.moveRight(height, width, x, y);
        }
       else if(action.equalsIgnoreCase("exit")) {
           System.out.println("Thanks for playing. Goodbye!");
           world.exitGame();
        }
    }
        
        
        
        
   
       }//end main
}//end class


HERE IS THE SECOND CLASS: 

class World {
     
    private int characterRow;
    private int characterColumn;
    private char[][] theWorld; 
    char player = 'P';
    int playerX = 0;
    int playerY = 0;
     
    public World() {
         
        characterRow = 0;
        characterColumn = 0;
    }
    public World(int height, int width, int x, int y) {
         
        this.characterRow = height;
        this.characterColumn = width;
        this.playerX = x;
        this.playerY = y;
    }
    public void setPlayerX(int x) {
        x = 0;
        playerX = x;
    }
    public void setPlayerY(int y) {
        y = 0;
        playerY = y;
    }
    public int getPlayerX() {
        return playerX;
    }
    public int getPlayerY() {
        return playerY;
    }
    public boolean isOutOfBounds() {
        boolean isOutOfBounds = false;
         
        for(int i = 0; i < theWorld.length-1; i++)
        {
            for(int j = 0; j < theWorld[i].length-1; j++)
            {
                if(playerX < 0 || playerY < 0 || playerX > theWorld.length-1 || playerY > theWorld.length-1) {
                    isOutOfBounds = true;
                }
            }
        }
        return isOutOfBounds;
     
     
    }
    public void moveUp(int height, int width, int x, int y) {
        x--;
        playerX = playerX + x;
        displayWorld(height, width, x, y);
    }
    public void moveDown(int height, int width, int x, int y) {
        
        x++;
        playerX = playerX + x;
        displayWorld(height, width, x, y);
    }
    public void moveLeft(int height, int width, int x, int y) {
        y--;
        playerY = playerY + y;
        displayWorld(height, width, x, y);
    }
    public void moveRight(int height, int width, int x, int y) {
        y++;
        playerY = playerY + y;
        displayWorld(height, width, x, y);
    }
    public void exitGame() {
         
        System.exit(0);
    }
    public void displayWorld(int height, int width, int x, int y) {
        theWorld = new char[height][width];
        for(height = 0; height < theWorld.length; height++)
        {
            for(width = 0; width < theWorld[height].length; width++)
            {
                 
                theWorld[height][width] = '-';
 
                theWorld[getPlayerX()][getPlayerY()] = player;
                 
                System.out.print(theWorld[height][width] + " ");
            }
            System.out.println();
        }
    }//end displayWorld
}//end class
    


PLEASE HELP. This is driving me nuts. Thanks in advance. :-) 


Updated on 06-Feb-2018

Can you answer this question?


Answer

2 Answers

Liked By