Question: Description In this assignment, you will write a program that plays Rock, Paper, Scissors. In case you are unfamiliar with this game, at the same

Description In this assignment, you will write a program that plays "Rock, Paper, Scissors". In case you are unfamiliar with this game, at the same time, two players randomly select one of three weapons: a rock, a piece of paper, or a scissors. They then compare their choices to determine a winner. The paper covers the rock, so if these are the two weapons, the owner of the paper wins. The scissors cuts the paper, so the scissors wins. Finally, the rock smashes the scissors, so the rock wins over scissors. To summarize: Weapon Weapon Winner Rock Paper Paper Rock Scissors Rock Scissors Paper Scissors If both players choose the same weapon, the game is a tie. Your program will be one of the players. A user running the program will be the other. An Interaction with the Program This program MUST be in a file called RockPaperScissors.java. Here is what a typical interaction with the program will look like. Please read this interaction carefully before we discuss it. Bold text is input from the user. hallersm@basin:~$ java RockPaperScissors This program plays duels of RockPaperScissors against the computer. You'll type in your guess of (R)ock, (P)aper, or (S)cissors and try to beat the computer as many times as you can. Best out of how many duels (must be odd)? 4 Invalid number of duels. Type a positive odd number! Best out of how many duels (must be odd)? 2 Invalid number of duels. Type a positive odd number! Best out of how many duels (must be odd)? 3 Duel 1: Choose your weapon? P I chose the weapon: P Tie! Duel 2: Choose your weapon? R I chose the weapon: S You win! Duel 3: Choose your weapon? R I chose the weapon: S You win! Do you want to play again? y Duel 1: Choose your weapon? S I chose the weapon: S Tie! Duel 2: Choose your weapon? S I chose the weapon: S Tie! Duel 3: Choose your weapon? S I chose the weapon: R You lose! Do you want to play again? n Overall results: total duels = 6 wins = 2 losses = 1 ties = 3 win % = 33.33 hallersm@basin:~$ Printing the Introduction First, notice that the program prints and introduction with instructions. Write a method introduction() to do this. This method takes no parameters and does NOT return a value. It just prints the text above. You should have a main() that looks like this at this point: public static void main(String[] args) { introduction(); } Compile and test your program so far. Make sure it is working before you continue. Your output should look like this so far: This program plays duels of RockPaperScissors against the computer. You'll type in your guess of (R)ock, (P)aper, or (S)cissors and try to beat the computer as many times as you can. Getting the Number of Duels from the User Next the program prompts the user for the number of duels he/she wants to play against the program. Write a method getDuels() to do this. This method is passed a Scanner object that has been constructed on System.in. It prompts the user for the number of duels until the user enters an odd integer. The method then returns this integer to the main(). Change your main() method to the following: public static void main(String[] args) { introduction(); Scanner console = new Scanner(System.in); int duels = getDuels(console); System.out.println("Number of duels entered: " + duels); } Compile and test your program so far. Make sure it is working before you continue. Your output should look something like this so far (new stuff in red): This program plays duels of RockPaperScissors against the computer. You'll type in your guess of (R)ock, (P)aper, or (S)cissors and try to beat the computer as many times as you can. Best out of how many duels (must be odd)? 4 Invalid number of duels. Type a positive odd number! Best out of how many duels (must be odd)? 3 playManyGames() Version 1 This method is passed the Scanner object and the number of duels to play. It will use two nested loops. The outer loop is a do-while loop. For right now, have the body of this loop be empty. For the test, it calls another method called playAgain(). This method is passed the Scanner. One set of duels is what we will call a complete game. The playAgain() method prompts the user to see if he/she wants to play another set of duels (that is, another game). If the user types "Y" or "y", this method returns true, otherwise, it returns false. So for now, the dowhile loop in in playManyGames() looks like this: do { } while (playAgain(console)); where console is a reference to the Scanner object. Compile and test your program so far. Make sure it is working before you continue. Your output should look something like this so far (new stuff in red): This program plays duels of RockPaperScissors against the computer. You'll type in your guess of (R)ock, (P)aper, or (S)cissors and try to beat the computer as many times as you can. Best out of how many duels (must be odd)? 4 Invalid number of duels. Type a positive odd number! Best out of how many duels (must be odd)? 3 Do you want to play again? Y Do you want to play again? y Do you want to play again? N playManyGames() Version 2 This method plays a set of duels and then asks the user if he/she wants to play again (the part you have already done). The set of duels will be in a for-loop inside the do-while loop. For this step, just have the inner for-loop print out the number of each duel up to the total number of duels that you passed into the playManyGames() method. At this point, your output should look like this (new stuff in red): This program plays duels of RockPaperScissors against the computer. You'll type in your guess of (R)ock, (P)aper, or (S)cissors and try to beat the computer as many times as you can. Best out of how many duels (must be odd)? 4 Invalid number of duels. Type a positive odd number! Best out of how many duels (must be odd)? 3 Duel 1: Duel 2: Duel 3: Do you want to play again? Y Duel 1: Duel 2: Duel 3: Do you want to play again? y Duel 1: Duel 2: Duel 3: Do you want to play again? n playManyGames() Version 3 Now add a method called playDuel(). This method is called from playManyGames() in the forloop, and it plays one duel with the user. The playDuel() method returns 1 if the user wins, 2 if the program wins, and 3 if there is a tie. 1. Start writing playDuel(). This method is passed the Scanner object. For now, just have this method return 1 (signaling that the user won). 2. Declare and initialize three variables at the start of playManyGames(): int totalDuels = 0; // total duels int wins = 0; // total user wins int losses = 0; // total user losses (program wins) 3. Put the following code in the for-loop in playManyGames(): int winner = playDuel(console); if (winner == 1) { wins++; } else if (winner == 2) { losses++; } where console is the name of the Scanner passed in to playManyGames(). If you named your Scanner something different, change console to that name, obviously. playDuel() Make sure your code compiles up to this point. Now lwrite a real playDuel(). We will need yet another couple of methods that playDuel() calls. 1. Write getUserWeapon(). This method is passed the Scanner that was passed to playDuel(). It prompts the user for his/her choice of weapon, either R, S, or P. It should accept r, s, and p as well. It keeps prompting the user until it gets R, S, or P, and it returns this String to playDuel(). The method should return a capital letter. That is, if the user enters r, it should return R. 2. Write getRandomWeapon(). This method has no parameters, but it returns either R, S, or P as the program's choice of weapon. Obviously, you should use a random number to pick one of these three Strings to return. This method should also print out the weapon that it has chosen before returning it -- something like: I chose the weapon: R 3. Now in playDuel() write an if else/if else to compare the user's weapon to the random weapon chosen by the program. If the user has won, print out You win! and return a 1. If the program has won, print out You lose! and return 2. If it is a tie, print out Tie! and return 3. playManyGames() Version 4 The only thing left is to report the results. Add this final line of code at the bottom of playManyGames(): reportStats(totalDuels, wins, losses); Write reportStats(). This method is passed three integers that represent the total number of duels played, the user's wins, and the user's losses. If reports these and also the percentage of time that the user won (see example above). Round your percentage to two decimal places

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!