Write a function that will read in the user choice of ‘P’, ‘P’ or ‘S’ . This fun
ID: 3622344 • Letter: W
Question
Write a function that will read in the user choice of ‘P’, ‘P’ or ‘S’ . This function takes NO incoming parameters and returns a char, the users choice. They function will use a do-while loop that does not exit until the user enters in a valid choice.
Write a function that generates the computer move. This function has NO parameters also. Generate a random number from 0 to 2. If the number is 0, return is ‘p’, if number is 1, the computer move to return is ‘r’ and if the number is 2, the computer move to return is ‘s’.
Write a function that takes in two parameters, the human move and the computer move. This function will compare the human move to the computer move and determine which won the hand. Return a value that can be checked in main( ) so main can print who won the hand.
paper beats rock
rock beats scissors
scissors beat paper
a tie is if neither wins (they both have the same choice)
Write main( ) using the requirements given below. Once this compiles, you can link and run and test the entire program.
main( )
Declare all the variables used in main.
Play the game of paper-rock-scissors. In main ask the user how many hands he/she wants to play. This must be an odd number, so you must validate this input!
After each hand print a message stating whether the computer won the hand or the human won the hand and print the current record:
Human: 3 wins Computer: 2 wins
Once either the computer or the human has won the majority of hands in the round, then print a good-bye message.
For example, if the user wants to play best of 5, then stop playing the hands when either the human or the computer wins 3 hands. Print a message stating who won the round.
This is how far I got, and I got stuck.
Explanation / Answer
import java.util.*; public class rockpaperscissor {static String moves[]={"rock","paper","scissors"}; static Random randomGenerator = new Random(); public static void main(String[] args) {Scanner in = new Scanner(System.in); boolean again=true; int usermove; int computermove; while(again) {usermove= user(in); computermove=computer(); score(usermove,computermove); again=conclusion(in); } } public static int user(Scanner in) {char a; int move=0; System.out.print("Choose your weapon (r,p,s): "); a=in.nextLine().charAt(0); a=Character.toLowerCase(a); while(a!='r'&&a!='p'&&a!='s') {System.out.print("Choose your weapon (r,p,s): "); a=in.nextLine().charAt(0); a=Character.toLowerCase(a); } switch(a) {case 'r': move=0; break; case 'p': move=1; break; case 's': move=2; break; } return move; } public static int computer() {int move=randomGenerator.nextInt(3); System.out.println("I chose "+moves[move]); return move; } public static void score(int user,int computer ) { if(user==computer) System.out.println("Tie!"); else if(user==0&&computer==2) System.out.println("You break my sissors. you win!"); else if(user==0&&computer==1) System.out.println("I cover your rock. you lose!"); else if(user==1&&computer==2) System.out.println("I cut your paper. you lose!"); else if(user==1&&computer==0) System.out.println("You cover my rock. you win!"); else if(user==2&&computer==0) System.out.println("I break your sissors, you lose!"); else System.out.println("You cut my paper. you win!"); return; } public static boolean conclusion(Scanner in) {char a; System.out.println(" Good game. Play again!(Y/N) "); a=in.nextLine().charAt(0); a=Character.toLowerCase(a); while(a!='y'&&a!='n') {System.out.print("invalid entry Play again(Y/N)? "); a=in.nextLine().charAt(0); a=Character.toLowerCase(a); } if(a=='y') return true; else return false; } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.