Question

IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...

IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows.

When the program begins, 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.

The user enters his or her choice of “rock”, “paper”, or “scissors” at the keyboard. 3. The computer’s choice is displayed.

A winner is selected according to the following rules: •

If one player chooses rock and the other player chooses scissors, then rock wins. The rock smashes the scissors.

• If one player chooses scissors and the other player chooses paper, then scissors wins. Scissors cuts paper.

• If one player chooses paper and the other player chooses rock, then paper wins. Paper wraps rock.

• If both players make the same choice, the game must be played again to determine the winner.

Be sure to divide the program into methods that perform each major task. Here is the description of the methods you should implement:

• public static String computerChoice() – This method will return the computer’s choice of “rock”, “paper”, or “scissors”. •

public static String userChoice () – This method will return the user’s choice of “rock”, “paper”, or “scissors” after validating the user’s choice using isValidChoice method.

• public static boolean isValidChoice (String choice) – This method will take a String holding the user’s choice as parameter and return true if the choice is valid (“rock”, “paper”, or “scissors”), false otherwise. Be sure that the comparison is not case sensitive. •

public static void determineWinner(string computer,string user)– This method will take both the choices as parameter and display the winner.

Call the method isValidChoice() in a while loop in the userChoice() method to verify that the choice that user enters must be “rock”, “paper”, or “scissors”. If invalid string is input, isValidChoice()will return false and the program should ask for new input until the valid input is given.

The user is allowed to play the game as often as desired .

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE - Main.java

import java.util.Random;
import java.util.Scanner;

public class Main {
//function to determine Winner take user move and computersMove as arguements
   public static int determineWinner(int usersMove, int computersMove) {
//if usersMove and computersMove are equal than return 2 means draw
if (usersMove == computersMove)
return 2;
//check if user choose rock
else if (usersMove == 1)
{ //if computer choose scissor than return 1 user wins
   if (computersMove == 3)
   return 1;
//if computer choose paper than return 0 computer wins
   else if (computersMove == 2)
   return 0;
}
//check if user choose paper
else if (usersMove == 2) {
//if computer choose rock than return 1 user wins
if (computersMove==1)
return 1;
  
//if computer choose scissor than return 0 computer wins
   else if (computersMove == 3)
   return 0;
}
  
//check if user choose scissor
else if (usersMove == 3)
{ //if computer choose paper than return 1 user wins
   if (computersMove == 2)
   return 1;
   //if computer choose rock than return 0 computer wins
   else if (computersMove == 1)
   return 0;
}
return 5;
}
//function to get computer Choice
public static int computerChoice(){
//variable declare
       int computersNum;
       //random object
       Random random = new Random();
       //generate random number
       // 1 = rock, 2 = paper ,3 = scissors
       computersNum = random.nextInt(2)+1;
       //return computers choice
       return computersNum;
   }
   //ask user choice
   public static int userChoice(){
   Scanner scanner = new Scanner(System.in);
   //ask use to enter choice
   System.out.println("Enter 1 = rock, 2 = paper ,3 = scissors");
   //store in input
       int input = scanner.nextInt();
       //check user choice is valid or not
       while(!isValidChoice(input)){
       //Invalid choice ask again user to enter choice
       System.out.println("Invalid choice , Pleaser enter 1 = rock, 2 = paper ,3 = scissors");
       //store in input
   input = scanner.nextInt();
       }
return input;
}
//function to check choice is valid or not
public static boolean isValidChoice(int userChoice){
if(userChoice==1||userChoice==2||userChoice==3)
return true;
else
return false;
}
  
   public static void main(String[] args) {
   //get use move
       int userInput = userChoice();
       //get computer move
       int computersMove = computerChoice();
       //print user choice
       if(userInput == 1)
       System.out.print("Human : Rock " );
       else if(userInput == 2)
       System.out.print("Human : Paper " );
       else
       System.out.print("Human : Scissors ");
      
       //print computer choice
       if(computersMove == 1)
       System.out.print("Computer : Rock" );
       else if(computersMove == 2)
       System.out.print("Computer : Paper" );
       else
       System.out.print("Computer : Scissors");
       System.out.println();
       int win = determineWinner(userInput,computersMove);
       //print winner
       if(win == 1){
       System.out.println("Human wins this round");
       }
       else if (win == 0){
       System.out.println("Computer wins this round");
       }
       else if(win == 2){
       System.out.println("Its a draw");
       }
       System.out.println();
       }
   }

Screenshots -

pls do give alike ,thank you

Add a comment
Know the answer?
Add Answer to:
IN JAVA. Write a program that lets the user play the game of Rock, Paper, Scissors...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...

    (Java) Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. When the program begins, 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. The...

  • In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play...

    In JAVA Chapter 5Assignment(Rock, Paper, Scissors)–20pointsYour goal is towrite a program that lets the user play the game of Rock, Paper, Scissors against the computer.Your program should have the following: •Make the name of the project RockPaperScissors•Write a method that generates a random number in the range of 1 through 3. The randomly generated number will determine if the computer chooses rock, paper, or scissors. If the number is 1, then the computer has chosen rock. If the number is...

  • C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this...

    C++ Part 2: Rock Paper Scissors Game Write a program that lets the user play this game against the computer. The program should work as follows: When the program begins, a random number between 1 and 3 is generated. If the number is 1, the computer has chosen rock. If the number is 2, the computer has chosen paper. If the number is 3, the computer has chosen scissors. Don't display the computer's choice yet. Use a menu to display...

  • In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user...

    In this problem, you’ll play a simple rock, paper, scissors game. First, you’ll ask the user to pick rock, paper, or scissors. Then, you’ll have the computer randomly choose one of the options. After that, print out the winner! You should keep playing the game until the user hits enter. Note: You’ll need to implement a method called String getWinner(String user, String computer). Luckily, you just wrote that in an earlier program! Here is a sample run of the program....

  • In python language Write a program that lets the user play the game of Rock, Paper,...

    In python language Write 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 begins, a random number in the range of 1 and 3 is generated. 1 = Computer has chosen Rock 2 = Computer has chosen Paper 3 = Computer has chosen Scissors (Dont display the computer's choice yet) 2. The user enters his or her choice of “Rock”, “Paper", “Scissors" at...

  • Write a Python program (using python 3.7.2) that lets the user play the game of Rock,...

    Write a Python program (using python 3.7.2) that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. 1. When the program begins, 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...

  • For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-...

    For this week’s assignment, we will be recreating the Rock, Paper, Scissors program using Object-Oriented Programming. You will be working with me on a team to build the program. I have already written my part of the program, and the Game.java file is attached. Your task will be to write a RockPaperScissors class that contains the following methods: getUserChoice: Has the user choose Rock, Paper, or Scissors. After validating the input, the method returns a String containing the user choice....

  • java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of...

    java pls Rock Paper Scissors Lizard Spock Rock Paper Scissors Lizard Spock is a variation of the common game Rock Paper Scissors that is often used to pass time (or sometimes to make decisions.) The rules of the game are outlined below: • • Scissors cuts Paper Paper covers Rock Rock crushes Lizard Lizard poisons Spock Spock smashes Scissors Scissors decapitates Lizard Lizard eats Paper Paper disproves Spock Spock vaporizes Rock Rock crushes Scissors Write a program that simulates the...

  • C++ You are asked to implement scissors-rock-paper game where the players are a computer and a...

    C++ You are asked to implement scissors-rock-paper game where the players are a computer and a user. The player that wins 3 hands successively is the winner of the game. Your program should prompt the user with a message at the beginning of each hand to enter one of scissors, rock or paper. If the user’s entry is not valid, i.e. entry is neither scissors, rock, nor paper, your program throws a message to the user to enter a valid...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT