please provide a pesudo code and raptor flowchart ( use random ) Do not use C++
ID: 3870448 • Letter: P
Question
please provide a pesudo code and raptor flowchart ( use random )
Do not use C++ or jave
Design a program that lets the user play the game of Rock, paper, Scissors against the computer . the program should work as follows:
1) When the program hegins, a random number in the range of 1 through 3 is generated , if the number is 1 , then the computer has chosen rock . if the number is 2 , then the computer has chosen paper . If the number is 3 , then the computer has chosen scissors. ( Don’t display the computer’s choice yet)
2) The user enters his or her choice of “ Rock” , “paper” , or “scissors” at the keyboard.
3) The computer’s choice is displayed.
4) The program should display a message indicating whether the user or the computer was the winner.A winner is selected according to the following rules:
- If one player chooses rock and the other player choose scissors, then rock wins.( the rock smashes the scissors)
- If one player chooses scissors and the other player chooses paper, then scissors wins.( scissors cut paper )
- If one player choose paper and the other player choose rock, then paper wins. ( paper wraps rock )
- If both players make the same choice , the game must be played again to determine the winner.
Explanation / Answer
First a method to determine choices:
public static String getChoice (int number)
{
String choice;
switch (number)
{
case 1:
choice = "rock";
break;
case 2:
choice = "paper";
break;
case 3:
choice = "scissors";
break;
default:
choice = null;
}
return choice;
}
Computer’s choice:
public static String computerChoice()
{
// Create a Random object.
Random rand = new Random();
// Generate a random number in the range of
// 1 through 3.
int num = rand.nextInt(3) + 1;
// Return the computer's choice.
return getChoice (num) ;
}
User’s choice:
public static String userChoice(Scanner keyboard) {
// Ask the user for input
System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");
int userChoice = keyboard.nextInt();
String play = getChoice (userChoice);
// Validate the choice.
while (play == null)
{
System.out.print("Enter 1 - rock, 2 - paper, or 3 - scissors: ");
userChoice = keyboard.nextInt();
play = getChoice (userChoice);
}
// Return the user's choice.
return play;
}
Determine winner:
public static String determineWinner (String computerChoice, String userChoice)
{
checkNotNull(computerChoice, "computer must have a choice");
checkNotNull(userChoice, "user must have a choice");
String output;
output = "The computer's choice was " + computerChoice + ". ";
output += "The user's choice was " + userChoice + ". ";
// check logic
if (userChoice.equalsIgnoreCase("rock")) {
if (computerChoice.equalsIgnoreCase("scissors"))
output += "Rock beats scissors. The user wins!";
else if (computerChoice.equalsIgnoreCase("paper"))
output += "Paper beats rock. The computer wins!";
else
output += "The game is tied! Play again...";
}
else if (userChoice.equalsIgnoreCase("paper"))
{
if (computerChoice.equalsIgnoreCase("scissors"))
output += "Scissors beats paper. The computer wins!";
else if (computerChoice.equalsIgnoreCase("rock"))
output += "Paper beats rock. The user wins!";
else
output += "The game is tied! Play again...";
}
else if (userChoice.equalsIgnoreCase("scissors"))
{
if (computerChoice.equalsIgnoreCase("rock"))
output += "Rock beats scissors. The computer wins!";
else if (computerChoice.equalsIgnoreCase("paper"))
output += "Scissors beats paper. The user wins!";
else
output += "The game is tied! Play again...";
}
return output;
}
Putting it together:
public static void main(String[] args)
{
String computer;
String user;
Scanner keyboard = new Scanner(System.in);
// Play the game as long as there is a tie.
do {
// Get the computer's choice.
computer = computerChoice();
// Get the user's choice.
user = userChoice(keyboard);
// Determining the winner
String output = determineWinner(computer, user);
System.out.println(output);
}
while (user.equalsIgnoreCase(computer));
keyboard.close();
}
Required output:
Enter 1 - rock, 2 - paper, or 3 - scissors: 3
The computer's choice was paper.
The user's choice was scissors.
Scissors beats paper.
The user wins!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.