The game of rock, paper and scissors can be played with the computer. To play th
ID: 3641902 • Letter: T
Question
The game of rock, paper and scissors can be played with the computer. To play the game the computer must select rock, paper or scissor, the player (or user) must select rock, paper or scissor and then the two result must be compared so that the computer can determine which player (the computer or the user) wins.
Since the computer can only generate random numbers, we will use numbers to represent rock, paper and scissors so:
1. rock
2.paper
3.scissors
Function main()
Function main will have 3 variables; one stores the user's choice of rock, paper or scissors (1, 2, or 3); one stores the computer's "choice" of rock, paper or scissors (1, 2, or 3, generated using rand()); the third allows the user to control a loop that repeats the play of the game. Do not use a counter controlled loop! After defining the needed variables in main, call srand() to initialized rand().
Start the loop that the user controls and inside the loop:
• call the function to get a value for the computer's choice,
•call the function for the user's choice
•call the function that will determine the outcome. The function has two values as arguments; the computer's choice and the user's choice. The function returns a value based on who won.
•if there is a winner, output to the screen if the winner was the computer or the user.
•if there is a tie, repeat the loop otherwise ask the user if they would like to repeat the program
Function Definitions
First Function
This function gets the computer's choice by producing a random number between 1 and 3 and returning it to main. It has no parameters.
Second Function
The function has no parameters. It gets the user's choice by displaying the menu as:
1. rock
2.paper
3.scissors
and then gets the users input. The function uses a loop to validate that the user's input is 1, 2, or 3. After validating the input, the function returns the value input by the user.
Third function
This function will receive as parameters the computer's choice, and the user's choice. The function will display the computer's choice and the user's choice on the screen. It will then compare the 2 values to find out who won. Take some time to think through how to write this! You will need to use an if...else structure. The rules are:
•Rock smashes scissors
•Scissors cut paper
•Paper wraps rock
If the user wins, return 1; if the computer wins return 2; if it is a tie return 3.
Program Requirements:
All submissions must use prototypes and local variables only.
Explanation / Answer
import java.io.*; import java.util.*; class Game { public static void main(String args[]) throws IOException { int comp; int us; char ch='n'; Scanner sc=new Scanner(System.in); do { comp=computerChoice(); us=userChoice(); System.out.println("computer selected no "+comp); if(usRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.