Cannot get out of do-while loop
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:
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!
Re: Cannot get out of do-while loop
If I understand the logic of the loop only one cell ever has the value 5. So
Code :
board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5
this expression will always be true. That is, either 5 is not located at (2,5) or it is not located at (3,2) because it can't be at both.
Re: Cannot get out of do-while loop
Quote:
Originally Posted by
pbrockway2
If I understand the logic of the loop only one cell ever has the value 5. So
Code :
board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5
this expression will always be true. That is, either 5 is not located at (2,5) or it is not located at (3,2) because it can't be at both.
Right, i realized that i shouldve made
Code :
board[2][5]!=5 ||board[3][2]!=5 || board[4][4]!=5
to
Code :
board[2][5]==5 ||board[3][2]==5 || board[4][4]==5
but still doesnt work any ideas to why?
Re: Cannot get out of do-while loop
What does "doesnt work" mean? In particular, under what circumstances is the loop supposed to finish?
Remember the condition you put in the while part is the condition that means the loop keeps going.
Re: Cannot get out of do-while loop
Quote:
Originally Posted by
pbrockway2
What does "doesnt work" mean? In particular, under what circumstances is the loop supposed to finish?
Remember the condition you put in the while part is the condition that means the loop keeps going.
Thanks for this! I rethinked the way I approached this and used a while looop! Working now! Here's the code
Now just gotta work on making the player stay on the same location when he accidentally gets off the board
Code :
while (flag=true)
{
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);
}
if (board[4][8]==5)
{
System.out.println("WIN");
break;
}
else if (board[2][5]==5 ||board[3][2]==5 || board[4][4]==5)
{
System.out.println("LOSE");
break;
}
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("");
}
}
Re: Cannot get out of do-while loop
while (flag=true)
That just makes flag true every iteration, you mean while(flag==true) which honestly can just be while(flag)