Hi, i'm having trouble with this assignment and the goal of this assignment is to create a 2d array that looks like this:
0 0 0 0 0 0 0 0 0
0 5 0 0 0 0 0 0 0
0 0 0 0 0 7 0 0 0
0 0 7 0 0 0 0 0 0
0 0 0 0 7 0 0 0 4
where the number 5 is the player and 4 is the goal and avoid the 7s which are traps. At first it got out of the do-while loop when there was only one argument inside the while() (when I tested it without hitting any 7s/traps). However, when I added more things inside the while so that the when the player hits any trap or get to the goal without hitting any trap, it would not get out of the do-while loop.
I also want it so that the player would stay at the edge(same position) when the player accidentally move out off the board.
This is my code:
import java.io.*; import java.util.*; public class Dungeon { static Scanner in = new Scanner (System.in); public static void main (String[] args) { int [][] board= new int [5][9]; //starting point int row=1; int col=1; //moves int move; int up= 8; int right=6; int left=4; int down=5; // how the map looks: board[row][col]=5; board[2][5]=7; board[3][2]=7; board[4][4]=7; board[4][8]=4; //print map before game for (int i=0; i<board.length; i++) { for (int j=0; j<board[i].length; j++) { System.out.print(" "+ board[i][j]); } System.out.println(""); } do { System.out.println("Enter move"); move=in.nextInt(); if (move==up) { board[row][col]=0; board[--row][col]=5; System.out.println(row+ " "+ col); } else if(move==down) { board[row][col]=0; board[++row][col]=5; System.out.println(row+ " "+ col); } else if (move==right) { board[row][col]=0; board[row][++col]=5; System.out.println(row+ " "+ col); } else if (move==left) { board[row][col]=0; board[row][--col]=5; System.out.println(row+ " "+ col); } for (int i=0; i<board.length; i++) { for (int j=0; j<board[i].length; j++) { System.out.print(" "+ board[i][j]); } System.out.println(""); } } while (board[4][8]!=5 ||board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5); if (board[4][8]==5) { System.out.println("WIN"); } else if (board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5) { System.out.println("LOSE"); } } }
Thanks in advance!