Rock Paper Scissors,。
Program Overview
You will create a program in a file named RPS.java. The program will allow the user to play as many rounds of the game Rock-Paper-Scissors against the computer as they desire. If you are not familiar with the rules of the game, you can read about it at http://en.wikipedia.org/wiki/Rock-paper-scissors. The user will make their own choice of rock, paper or scissors. The Java Random.nextInt() method will be used to generate the computer’s choice. Your program should be able to produce the sample I/O session shown below exactly, EXCEPT that a) the user’s input is shown here boldfaced and underlined only for easy identification, and b) the computer’s choice will be unpredictable from run to run because it is randomly generated.
Type R(ock), P(aper) or S(cissors): x Invalid answer. Re-type R, P or S: y Invalid answer. Re-type R, P or S: Z Invalid answer. Re-type R, P or S: r You played rock. The computer played rock. That round ended in a tie. Do you want to play another round? (Y/N) y Type R(ock), P(aper) or S(cissors): P You played paper. The computer played rock. Yippee! You won that round. Do you want to play another round? (Y/N) Y Type R(ock), P(aper) or S(cissors): S You played scissors. The computer played rock. Rats! The computer won that round. Do you want to play another round? (Y/N) y Type R(ock), P(aper) or S(cissors): s You played scissors. The computer played rock. Rats! The computer won that round. Do you want to play another round? (Y/N) N You won 1 rounds. The computer won 2 rounds. Rats! The computer won the game.
User is trapped in loop until they enter valid R, r, P, p, S, or s value. Both uppercase and lowercase input is allowed and valid.
After user enters valid choice, A) tell them what they played, B) tell them what random choice the computer played and C) tell them who won the round. Use a loop to control the number of rounds of the game that get played. Both uppercase and lowercase input allowed
HINT: See pages the Random Numbers section in 5.1 of your textbook for ideas on how to generate a random choice of play for the computer.
Keep track of how many rounds computer and user each win.
Stop playing when user indicates that they want to quit.
Display user and computer wins and tell who won the game.
Functionality Requirements
Your program must be able to produce an I/O session formatted exactly as the sample session shown above.
You may assume that any user response that is not Y or y when asked if they want to play another round, is a No answer.
Language Usage Requirements
For full credit, your program must use the following elements of the Java language appropriately:
- A while-loop.
- A do-while loop.
- A boolean variable or method.
- a Java Random object and its nextInt() method.
- You may use only the Java language elements that have been presented in the textbook in the first 5 chapters to solve this problem.
- Procedural Decomposition: your program must contain the following methods
- A method that A) prompts for, B) inputs and C) returns the user’s choice of rock, paper of scissors.
- You must trap them in a loop until they enter a valid response to your prompt.
- You must accept both uppercase and lowercase input as equally valid.
- Considerations: Should the user’s choice of R, r, P, p, S or s be handled as a char or String value? There are pros and cons to both. The choice will be yours
- A method that uses the nextInt()method of a Random object to compute and return a random rock, paper or scissors choice for the computer.
Good Style Requirements
Programs in active production must be maintained over years, often by programmers who didn’t write the original code. They must update the code to correct bugs, or to upgrade the program to add capabilities not required in the original specs. Good style rules are governed by what makes code maintainable. Your programs should incorporate the following style elements for this lab:
- appropriate and consistent spacing and blank lines to enhance readability
- appropriate and consistent indentation of class bodies, method bodies, if/else bodies and loop bodies
- meaningful, self-documenting names for variables, constants and methods
- named constants such as ROCK and PAPER that make code more readable
- comments at the top of the file include the following
- name of programmer and purpose of program
- total hours spent on assignment including this lab
- comments strategically placed throughout the code to highlight major logic
- procedural decomposition: No method (including main()) has more than 20 lines of code, not counting comments, blank lines and lines containing single { or } characters. If a method gets too long, subdivide it.
Checkpoint Requirements
Write the code needed to produce the following I/O session, with the understanding that the user might pick any valid choice and the computer could play any valid game piece. User input is shown boldfaced and underlined only for easy identification:
Type R(ock), P(aper) or S(cissors): X Invalid answer. Re-type R, P or S: y Invalid answer. Re-type R, P or S: Z Invalid answer. Re-type R, P or S: R You played rock. The computer played scissors.
That is, write the code for getting a valid response from the user and for generating a random choice for the computer. Display the game pieces that were played by the two contestants. Your checkpoint code does not have to meet the requirements for the final version of the program. That is, you may write all the code in main() for the checkpoint if you wish. For the final version of the program you must engage in procedural decomposition once you’ve figured out the logic.