I'm making a dice game and need to know how I might set up a class that stores the dice roll numbers from the previous turn such that an if statement can display something. I.E. if computerDie == 1 && userDie == 4 occurs twice in a row something happens. Any suggestions?
public static void main( String [] args ){
Random random = new Random();
int totalNumberOfTurns = 1;
int computerDie;
int userDie;
int computerScore = 0;
int userScore = 0;
for( int turn = 1; turn <= totalNumberOfTurns; turn++ ) {
computerDie = random.nextInt( 6 ) + 1;
userDie = random.nextInt( 6 ) + 1;
if () {
}
}
}
You could use an ArrayList and save each roll for each player if you need to use them later.
Or if you don't care to use them later, you can have 2 auxiliar variables like
lastUserDie
andlastComputerDie
which are updated after you check if they're the same.In this example I used a random between 1-3 to make it more probable to get the same roll, just change it to 6 again
Some sample outputs:
And: