I'm creating a game where there is a 10x5 grid with "#" as walls, "O" as the player and "X" as the checkpoint you need to get to to win. I need to make sure that whatever direction the player chooses to go, they do not go out of bounds. I feel like I've made the conditions for when the player goes out of bounds and when it is acceptable to move pretty clear in my code, yet whenever the player makes a move that would put them out of bounds, it throws an error.
class Game {
Random rand = new Random();
Scanner scan = new Scanner(System.in);
public char spots[][] = {
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'},
{'.','.','.','.','.','.','.','.','.','.'}
};
int playerRow, playerColumn;
public void setWalls() {
int wallChance;
for(int y=0; y<5; y++) {
for(int x=0; x<10; x++) {
wallChance = rand.nextInt(10);
if(wallChance==0) {
spots[y][x] = '#';
}
}
}
}
public void setPlayerAndExit() {
boolean peSet = false;
while(peSet==false) {
int row = rand.nextInt(5);
int column = rand.nextInt(10);
if(spots[row][column]!='#') {
spots[row][column] = 'O';
playerRow = row;
playerColumn = column;
peSet = true;
}
}
peSet = false;
while(peSet==false) {
int r = rand.nextInt(5);
int c = rand.nextInt(10);
if(spots[r][c]!='#' && spots[r][c]!='O') {
spots[r][c] = 'X';
peSet = true;
}
}
}
public void printGame() {
System.out.println(spots[0][0] + " " + spots[0][1] + " " + spots[0][2] + " " + spots[0][3] + " " + spots[0][4] + " " + spots[0][5] + " " + spots[0][6] + " " + spots[0][7] + " " + spots[0][8] + " " + spots[0][9]);
System.out.println(spots[1][0] + " " + spots[1][1] + " " + spots[1][2] + " " + spots[1][3] + " " + spots[1][4] + " " + spots[1][5] + " " + spots[1][6] + " " + spots[1][7] + " " + spots[1][8] + " " + spots[1][9]);
System.out.println(spots[2][0] + " " + spots[2][1] + " " + spots[2][2] + " " + spots[2][3] + " " + spots[2][4] + " " + spots[2][5] + " " + spots[2][6] + " " + spots[2][7] + " " + spots[2][8] + " " + spots[2][9]);
System.out.println(spots[3][0] + " " + spots[3][1] + " " + spots[3][2] + " " + spots[3][3] + " " + spots[3][4] + " " + spots[3][5] + " " + spots[3][6] + " " + spots[3][7] + " " + spots[3][8] + " " + spots[3][9]);
System.out.println(spots[4][0] + " " + spots[4][1] + " " + spots[4][2] + " " + spots[4][3] + " " + spots[4][4] + " " + spots[4][5] + " " + spots[4][6] + " " + spots[4][7] + " " + spots[4][8] + " " + spots[4][9]);
}
public void playGame() {
String dir;
boolean gameOver = false;
setWalls();
setPlayerAndExit();
while(gameOver==false) {
System.out.println("--------------------");
printGame();
System.out.println("--------------------");
System.out.println("go up, down, left or right?");
dir = scan.nextLine();
//unacceptable moves
if(!dir.equals("up") && !dir.equals("down") && !dir.equals("left") && !dir.equals("right")) {
System.out.println("Wrong input");
}
else if((dir.equals("up") && spots[playerRow-1][playerColumn]=='#') || (dir.equals("up") && playerRow-1<0)) {
System.out.println("Can't move there");
}
else if((dir.equals("down") && spots[playerRow+1][playerColumn]=='#') || (dir.equals("down") && playerRow+1>4)) {
System.out.println("Can't move there");
}
else if((dir.equals("left") && spots[playerRow][playerColumn-1]=='#') || (dir.equals("left") && playerColumn-1<0)) {
System.out.println("Can't move there");
}
else if((dir.equals("right") && spots[playerRow][playerColumn+1]=='#') || (dir.equals("right") && playerColumn+1>9)) {
System.out.println("Can't move there");
}
//acceptable moves
else if(dir.equals("up") && (playerRow-1)>=0) {
spots[playerRow][playerColumn] = '.';
playerRow--;
if(spots[playerRow][playerColumn] == 'X') {
spots[playerRow][playerColumn] = 'O';
System.out.println("You win!");
gameOver = true;
}
else {
spots[playerRow][playerColumn] = 'O';
}
}
else if(dir.equals("down") && (playerRow+1)<=4) {
spots[playerRow][playerColumn] = '.';
playerRow++;
if(spots[playerRow][playerColumn] == 'X') {
spots[playerRow][playerColumn] = 'O';
System.out.println("You win!");
gameOver = true;
}
else {
spots[playerRow][playerColumn] = 'O';
}
}
else if(dir.equals("left") && (playerColumn-1)>=0) {
spots[playerRow][playerColumn] = '.';
playerColumn--;
if(spots[playerRow][playerColumn] == 'X') {
spots[playerRow][playerColumn] = 'O';
System.out.println("You win!");
gameOver = true;
}
else {
spots[playerRow][playerColumn] = 'O';
}
}
else if(dir.equals("right") && (playerColumn+1)<=9) {
spots[playerRow][playerColumn] = '.';
playerColumn++;
if(spots[playerRow][playerColumn] == 'X') {
spots[playerRow][playerColumn] = 'O';
System.out.println("You win!");
gameOver = true;
}
else {
spots[playerRow][playerColumn] = 'O';
}
}
}
}
}
I've tried messing around with the numbers and conditions, such as having the out of bounds condition saying if the number for the row or column is equal to the number that would bring them out of bounds that it should say you can't move there. I'm expecting that any time the player moves on a # or moves somewhere that is out of bounds, they will be told they can't move there.