Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Programming 7th Edition Chapter 9, Game Zone Problem 4. Please help with th

ID: 3733173 • Letter: J

Question

Java Programming 7th Edition Chapter 9, Game Zone Problem 4. Please help with this problem:

In Chapter 7, you improved a Rock Paper Scissors game played between a user and the computer. Add an enumeration that holds three values that represent ROCK, PAPER, and SCISSORS, and use it for all comparisons in the program. Save the file as RockPaperScissors3.java.

Here is the code I have for the file. Please help me out.

RockPaperScissors2.java

import java.util.Random;

import java.util.Scanner;

public class RockPaperScissors2 {

public static void main(String[ ] args) {

  

Scanner scanner = new Scanner(System.in);

int tie = 0;

int win = 0;

int lose = 0;

String[] rps = new String[3];

rps[0] = "rock";

rps[1] = "paper";

rps[2] = "scissors";

Random random = new Random();

for(int a = 0; a < 10; a++)

{

System.out.println("Enter your choice: ");

String choice = scanner.nextLine();

choice = choice.toLowerCase();

String c1 = choice.substring(0,2);

//String c2 = choice.substring(1,2);

String s1 = rps[0].substring(0,2);

//String s2 = rps[0].substring(1,2);

String s3 = rps[1].substring(0,2);

//String s4 = rps[1].substring(1,2);

String s5 = rps[2].substring(0,2);

//String s6 = rps[2].substring(1,2);

if(!c1.equals("pa") && !c1.equals("ro") && !c1.equals("sc"))

{

do

{

System.out.println("Please enter a correct choice.");

choice = scanner.nextLine();

c1 = choice.substring(0,2);

}while(c1 == s1 || c1 == s3 || c1 == s5);

}

int ran = random.nextInt(3);

String comp = rps [ran];

System.out.println("computer's choice " + comp);

if(comp.substring(0,1).equalsIgnoreCase(rps[0].substring(0,1)))

{

if(choice.substring(0,1).equalsIgnoreCase(rps[0].substring(0,1)))

{

System.out.println("tie");

tie++;

}

else if(choice.substring(0,1).equalsIgnoreCase(rps[1].substring(0,1)))

{

System.out.println("win");

win++;

}

else if(choice.substring(0,1).equalsIgnoreCase(rps[2].substring(0,1)))

{

System.out.println("lose");

lose++;

}

}

else if(comp.substring(0,1).equalsIgnoreCase(rps[1].substring(0,1)))

{

if(choice.substring(0,1).equalsIgnoreCase(rps[0].substring(0,1)))

{

System.out.println("lose");

lose++;

}

else if(choice.substring(0,1).equalsIgnoreCase(rps[1].substring(0,1)))

{

System.out.println("tie");

tie++;

}

else if(choice.substring(0,1).equalsIgnoreCase(rps[2].substring(0,1)))

{

System.out.println("win");

win++;

}

}

else if(comp.substring(0,1).equalsIgnoreCase(rps[2].substring(0,1)))

{

if(choice.substring(0,1).equalsIgnoreCase(rps[0].substring(0,1)))

{

System.out.println("win");

win++;

}

else if(choice.substring(0,1).equalsIgnoreCase(rps[1].substring(0,1)))

{

System.out.println("lose");

lose++;

}

else if(choice.substring(0,1).equalsIgnoreCase(rps[2].substring(0,1)))

{

System.out.println("tie");

tie++;

}

}

}

System.out.println("You win: " + win + " You lose: " + lose

+ "Games Tied: " + tie);

}

}

Explanation / Answer

import java.util.Random; import java.util.Scanner; public class RockPaperScissors3 { enum Option{ ROCK, PAPER, SCISSORS }; public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); int tie = 0; int win = 0; int lose = 0; String[] rps = new String[3]; rps[0] = "rock"; rps[1] = "paper"; rps[2] = "scissors"; Random random = new Random(); for(int a = 0; a < 2; a++) { Option choice = null; String input; do { System.out.print("Enter your choice: "); input = scanner.nextLine(); if(input.equalsIgnoreCase("rock")){ choice = Option.ROCK; } if(input.equalsIgnoreCase("paper")){ choice = Option.PAPER; } if(input.equalsIgnoreCase("scissors")){ choice = Option.SCISSORS; } }while(choice != Option.ROCK && choice != Option.PAPER && choice != Option.SCISSORS); int ran = random.nextInt(3); Option comp = null; if(ran == 0){ comp = Option.ROCK; } if(ran == 1){ comp = Option.PAPER; } if(ran == 2){ comp = Option.SCISSORS; } System.out.println("computer's choice " + comp); if(comp == Option.ROCK) { if(choice == Option.ROCK) { System.out.println("tie"); tie++; } else if(choice == Option.SCISSORS) { System.out.println("lose"); lose++; } else if(choice == Option.PAPER) { System.out.println("win"); win++; } } else if(comp == Option.PAPER) { if(choice == Option.SCISSORS) { System.out.println("win"); win++; } else if(choice == Option.PAPER) { System.out.println("tie"); tie++; } else if(choice == Option.ROCK) { System.out.println("lose"); lose++; } } else if(comp == Option.SCISSORS) { if(choice == Option.PAPER) { System.out.println("lose"); lose++; } else if(choice == Option.ROCK) { System.out.println("win"); win++; } else if(choice == Option.SCISSORS) { System.out.println("tie"); tie++; } } } System.out.println("You win: " + win + " You lose: " + lose + "Games Tied: " + tie); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote