Answered step by step
Verified Expert Solution
Question
1 Approved Answer
this code is for JAVA /** * This class gives you the computer's move in Rock, Paper, Scissors. */ public class ComputerOpponent { private static
this code is for JAVA /** * This class gives you the computer's move in Rock, Paper, Scissors. */ public class ComputerOpponent { private static boolean TESTING_MODE = false; private static int cycler = 0; public static String getMove() { if (TESTING_MODE) { switch (cycler++ % 3) { case 0: return "rock"; case 1: return "paper"; default: // (case 2) return "scissors"; } } else { switch ((int) (Math.random() * 3)) { case 0: return "rock"; case 1: return "paper"; default: // (case 2) return "scissors"; } } } }Introduction In the game Rock, Paper, Scissors, two players each choose either rock," "paper," or "scissors," and then show their choice to the other player at the same time. Each choice wins against one other choice and loses to one other choice: Rock wins against scissors, but loses against paper. Paper wins against rock, but loses against scissors. Scissors win against paper, but lose against rock. If both players make the same choice, the result is a tie. Assignment In this lab, you will implement a version of Rock, Paper, Scissors where the user plays against the computer. The user inputs in their choice as a String (either "rock", "paper", or "scissors" - not case sensitive), and the computer will make its choice at pseudorandom. The winner should be determined based on the rules above. The user should be able to play multiple rounds of Rock, Paper, Scissors against the computer. The program should first ask the user how many wins they want to play by prompting them Points to win: , which is shown in the examples. You may assume the user will always input an integer greater than or equal to one for points to win. Your program should keep track of the score of both the computer and the user. If there is a tie, neither player gets a point. The program should keep running until either the player or the computer wins the number of games they specified at the start. So, for example, if the user inputs 3 when prompted, the game should continue until either the user or the computer wins three rounds (not once three rounds have been played). After each turn, the program should print who won (either the player or the computer) and the score after that turn. Show the player's score first (for example, if the computer has won once and the player has not won, show (0-1)). More examples of this can be seen in the sample runs below, and your program's 1/0 must match the examples at the end of this document. Instructions new Start by creating a class named RockPaperScissors. Download the file ComputerOpponent.java from the labs and projects webpage. It contains the code that generates the computer's move for you, and your program must use this class to obtain a computer's move. Once you have downloaded it, move the file into the src folder of your RockPaperScissors project. It should be located directly alongside your Rock PaperScissors.java file in the same src folder. Once you have both files in Eclipse, feel free to explore ComputerOpponent.java. Most of the code we've covered, and some code will be covered later on in the semester. Notice the variable, static boolean TESTING_MODE = false; When TESTING_MODE is false, ComputerOpponent.java will generate computer moves pseudorandomly using Math.random(). However, if you change TESTING_MODE to true, the moves that the computer generates will always come in a fixed order: rock, paper, scissors, rock, paper, scissors, rock, paper, scissors, ... and so on. You should change TESTING_MODE to true when you are testing and debugging your program, since the computer's moves can be predicted. You should also test your program when TESTING_MODE is set to false. Your program should work correctly regardless of the value of TESTING_MODE. We will use our own version of ComputerOpponent.java when grading your lab, so don't worry about messing with the functionality of that file (but make sure that all of the code you write stays in Rock PaperScissors.java). Do not rename or change anything (except for the TESTING_MODE's boolean value) inside of ComputerOpponent.java, as doing so will cause a compilation error when we try to grade your lab. Once both java files are in the same src folder, you must get the computer move from ComputerOpponent.java by using its getMove () method after each valid user input. This method will return a String which will be either "rock", "paper", or "scissors" (each being all lowercase). Below is a line of code to get the computer's move (computerMove is a String variable that should be declared in your program before using the line of code below): computerMove = ComputerOpponent.getMove (); On each turn, you should prompt the user for their move and retrieve it via the nextLine() method. The game should be able to recognize "rock", "paper", and "scissors regardless of capitalization as valid user input. After each valid user input, your program must call ComputerOpponent.getMove () exactly once, and you may assume the computer's move will always be valid. If the user types something else, then prompt the user again, repetitively if necessary, until the user types a valid input. Do not update either player's score if the user did not enter a valid move. Also, do not call ComputerOpponent.getMove () another time if the user did not enter a valid move (the result of this method call must be stored and used once after each valid input made by the user). Considerations While Coding Your program will be doing a lot of the same things over and over again, which means that a loop will be part of your code. Consider the following when working on this assignment. How do we keep track of the player's and computer's scores? Which tasks belong inside the loop? o When you write your code, it may be easier to write the code for one iteration of the loop first, then putting that code inside a loop statement. Which tasks belong outside the loop? Should our call to Computeropponent.getMove () be inside or outside the loop? What happens if we don't put it where it should be (i.e., inside the loop when it should be outside, or outside the loop when it should be inside)? Should it be after the program validates the user's input? What should the condition for the loop be? o In other words, under what circumstances should the loop keep going? How do we handle unintended input, like Spock or gun? o We can do better than simply ending the game immediately! Examples Bold text represents the user's input; all other text should be printed by your program. Your program's 1/0 must match the examples below, and it must work correctly for all valid sequences of input. Each example is a single execution of a correct program. The first example is winning against the computer, the second example is losing to the computer, and the third example is edge cases for user input (testing mode set to true). Your program must work with the provided ComputerOpponent class regardless of testing mode, and your program must call ComputerOpponent.getMove () once after each valid user input and use the result of this method call correctly with each valid user input based on the rules of the game. Points to win: 5 Rock, paper, or scissors? rock The computer chose scissors, so you win! (1-0) Rock, paper, or scissors? paper The computer chose paper, so it's a tie. (1-0) Rock, paper, or scissors? scissors The computer chose rock, so you lose. (1-1) Rock, paper, or scissors? paper The computer chose rock, so you win! (2-1) Rock, paper, or scissors? rock The computer chose scissors, so you win! (3-1) Rock, paper, or scissors? rock The computer chose scissors, so you win! (4-1) Rock, paper, or scissors? scissors The computer chose paper, so you win! (5-1) Congratulations! You won! Points to win: 3 Rock, paper, or scissors? rock The computer chose paper, so you lose. (0-1) Rock, paper, or scissors? paper The computer chose scissors, so you lose. (0-2) Rock, paper, or scissors? scissors The computer chose rock, so you lose. (0-3) Sorry, you lost. Better luck next time! Points to win: 4 Rock, paper, or scissors? PAPEr The computer chose rock, so you win! (1-0) Rock, paper, or scissors? scisor Rock, paper, or scissors? scissors The computer chose paper, so you win! (2-0) Rock, paper, or scissors? SCISSORS The computer chose scissors, so it's a tie. (2-0) Rock, paper, or scissors? scissors The computer chose rock, so you lose. (2-1) Rock, paper, or scissors? SCIssors The computer chose paper, so you win! (3-1) Rock, paper, or scissors? Rock The computer chose scissors, so you win! (4-1) Congratulations! You won
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started