I am creating a game of rock paper scissors, on the playGame() method I cannot get the code to work, it only prints out what the user and computer has chosen. I have tried to figure out where i am going wrong and all i could get it to do was the print out a draw but it made the game always draw!
I haven't finished the method yet, i also need to add a score counter up to 3.
import java.util.Random;
public class RockPaperScissors
{
private InputReader reader;
private int yourScore;
private int computerScore;
private Random ran;
/**
* Constructor for objects of class RockPaperScissors
*/
public RockPaperScissors()
{
reader = new InputReader();
yourScore = 0;
computerScore = 0;
ran = new Random(1);
}
/**
* An example of a method - replace this comment with your own
*/
public void printPrompt()
{
System.out.println();
System.out.println();
System.out.println("Enter your choice, paper, rock or scissors >");
}
public String userChoice()
{
String input = reader.getInput();
input = input.trim().toLowerCase();
return input;
}
public String computerChoice()
{
Random ran = new Random();
int myRanInt = ran.nextInt(3);
String computerRanChoice ="";
switch(myRanInt) {
case 0: computerRanChoice = "paper";
break;
case 1: computerRanChoice = "scissors";
break;
case 2: computerRanChoice = "rock";
break;
}
return computerRanChoice;
}
public void playGame()
{
printPrompt();
String userMove = this.userChoice();
System.out.println("you have chosen " + userMove + " and the computer has chosen " + computerChoice());
int roundWinner = 0;
int count = 1;
switch(roundWinner) {
case 0: computerChoice().equals(userChoice());
System.out.println("This game is a draw");
break;
case 1: computerChoice().equals("rock");
System.out.println(userMove.equals("paper") ? "The computer is the winner" : "You are the winner");
break;
case 2: computerChoice().equals("paper");
System.out.println(userMove.equals("scissors") ? "The computer is the winner" : "You are the winner");
break;
case 3: computerChoice().equals("scissors");
System.out.println(userMove.equals("rock") ? "The computer is the winner" : "You are the winner");
break;
}
}
}
In the switch block of playgame() method, you have initialised the value of roundWinner as 0, so it will always print out case 0, i.e. the draw statement.
Using switch in this case seems a bit tricky, as you are working with two variables, the computer's and the player's choice, while switch works on only one variable. Try using some if statements first and then work your logic to switch if needed