1. Display the menu options to play the game, score, or quit.
2. Asks the user to input their choice of rock, paper, scissors, lizard or spock.
3. Randomly assigns the computer's throw & returns the value
4. Compares the two throws & returns who the winner is.
So far, i think i have majority of the code down, but when i run it my output seems to be wrong. For example this is how it should look like when you run it:
Menu: 1. Play game 2. Show score 3. Quit 1 Choose your weapon: R. Rock P. Paper S. Scissors L. Lizard K. Spock P You chose Paper Computer chose Lizard You lose
Instead, when I run it, this what my outout looks like:
Menu: 1. Play game 2. Show score 3. Quit 1 Choose your weapon: R. Rock P. Paper S. Scissors L. Lizard K. Spock R You chose Computer chose Paper You win -1
It doesn't even print what You choose as your weapon, still somehow finds a winner & also prints the -1 which is just supposed to be an int value that is the value of you winning. This is my code so far:
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int menu = 0;
do {
displayMenuPlayGame();
/*String input = in.nextLine();*/
boolean test = true;
while (test) {
if (in.hasNextInt()) {
menu = in.nextInt();
test = false;
} else {
System.out.println("Invalid Input - Retry");
String dummy = in.next();
}
}
String weapon ="";
String compThrow ="";
switch (menu) {
case 1:
displayWeaponMenu();
choose();
computerThrow();
compareChoices(weapon, compThrow);
break;
}
} while (menu != 3);
}
// This method displays the menu
public static void displayMenuPlayGame() {
System.out.printf("\n");
System.out.printf("\nMenu:");
System.out.printf("\n1. Play game");
System.out.printf("\n2. Show score");
System.out.printf("\n3. Quit");
System.out.printf("\n");
}
// This method displays the weapon menu & plays the game
public static void displayWeaponMenu() {
System.out.println("Choose your weapon:");
System.out.println("R. Rock");
System.out.println("P. Paper");
System.out.println("S. Scissors");
System.out.println("L. Lizard");
System.out.println("K. Spock");
}
public static void choose() {
Scanner in = new Scanner(System.in);
boolean test1 = true;
String weaponChoice = in.nextLine();
String weapon = "";
String compThrow = "";
int winner ;
if (weaponChoice == "R") {
weapon = "Rock";
}
else if (weaponChoice == "P") {
weapon = "Paper";
}
else if (weaponChoice == "S") {
weapon = "Scissors";
}
else if (weaponChoice == "L") {
weapon = "Lizard";
}
else if (weaponChoice == "K") {
weapon = "Spock";
}
System.out.println("You chose " +weapon );
/*System.out.println(winner);*/
}
// This method randomly assigns the computer's throw
public static void computerThrow() {
String compThrow = "";
double rando = Math.random();
int upperBound = 5;
int lowerBound = 1;
int rand = (int)((Math.random() * (upperBound - lowerBound + 1)) + lowerBound);
if (rand == 1) {
compThrow = "Rock";
}
else if (rand == 2) {
compThrow = "Paper";
}
else if (rand == 3) {
compThrow = "Scissors";
}
else if (rand == 4) {
compThrow = "Lizard";
}
else if (rand == 5) {
compThrow = "Spock";
}
System.out.println("Computer chose " + compThrow);
}
// This method compares the choices
public static void compareChoices(String weapon, String compThrow) {
/*1 = computer wins
0 = tie
-1 = user wins*/
int winner;
if (compThrow == weapon) {
winner = 0;
}
if (compThrow == "Scissors" && weapon == "Paper" ) {
winner = 1;
}
if (compThrow == "Paper" && weapon == "Rock" ) {
winner = 1;
}
if (compThrow == "Rock" && weapon == "Lizard" ) {
winner = 1;
}
if (compThrow == "Lizard" && weapon == "Spock" ) {
winner = 1;
}
if (compThrow == "Spock" && weapon == "Scissors" ) {
winner = 1;
}
if (compThrow == "Scissors" && weapon == "Lizard" ) {
winner = 1;
}
if (compThrow == "Lizard" && weapon == "Paper" ) {
winner = 1;
}
if (compThrow == "Paper" && weapon == "Spock" ) {
winner = 1;
}
if (compThrow == "Spock" && weapon == "Rock" ) {
winner = 1;
}
if (compThrow == "Rock" && weapon == "Scissors" ) {
winner = 1;
}
else {
winner = -1;
}
if (winner == 1) {
System.out.println("Computer wins");
}
if (winner == 0) {
System.out.println("Tie");
}
if (winner == -1) {
System.out.println("You win");
}
System.out.println(winner);
}
}
Much help will be appreciated. Thank you.

New Topic/Question
Reply



MultiQuote






|