I'm trying to do roshambo (or Rock-Paper-Scissors) and have the computer output Rock, Paper, or Scissors in place of a random value from 0 to 2. However, I'm not getting random values. When the program runs it goes through a while loop, and each time it goes through the loop, the value remains the same. However, if I stop, then re-run the application, the value changes, and remains that same value for the duration of the loop. Am I coding this method correctly?
public class Player2 extends Player{
private Random rand;
public Player2(){
rand = new Random();
}
public Roshambo generateRoshambo() {
int min = 0;
int max = 2;
int randomNum = rand.nextInt(max - min + 1) + min;
if(randomNum == 0){
return Roshambo.ROCK;
} else if (randomNum == 1) {
return Roshambo.PAPER;
} else {
return Roshambo.SCISSORS;
}
}
}
Here is the loop:
Roshambo value = p2.generateRoshambo();
String cont = "Yes";
do{
System.out.print("\nRock, Paper, or Scissors: ");
String choice = sc.nextLine();
System.out.println("\n" + name + ": " + choice);
System.out.println("Computer: " + value);
if (value == ...)
{
System.out.println("\n" + name + " Wins!\n");
}
else if (value == ...)
{
System.out.println("\nYou Tied!\n");
}
else
{
System.out.println("\nThe Computer Wins!\n");
}
System.out.print("Play again? (Yes/No): ");
cont = sc.nextLine();
} while (cont.equalsIgnoreCase("Yes"));
rand.nextInt(3)to have 3 possible values. You can add+1outside the brackets in order to get values from 1 to 3 instead of 0 to 2. Unless yourminandmaxwill be of use later on, you shouldn't make it more complicated than needed.