Question

(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 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

import java.util.Scanner;

//import java.util.Scanner;

import javax.swing.JOptionPane;

public class Game {

public static void main(String[] args) {

// TODO Auto-generated method stub

// create the choice variable and ask the user to enter the input

Scanner sc = new Scanner(System.in);

String choice;

char ch = 'Y';

String winner = " ";

int computerChoice = 0;

do {

//choice = JOptionPane.showInputDialog("Please enter rock, paper, or scissor: ");

System.out.println("Please enter rock, paper, or scissor: ");

choice = sc.nextLine();

if (isValid(choice)) {

computerChoice = (int) (Math.random() * 3 + 1);

// System.out.println(aIChoice);

int personChoice = getValue(choice);

winner = whoWin(personChoice, computerChoice);

System.out.println("You picked " + choice);

System.out.println("Computer picked " + getAIChoice(computerChoice));

System.out.println("You " + winner);

System.out.println("Do you wish to continue (Y/N): ");

ch = sc.nextLine().charAt(0);

} else {

System.out.println("Invalid input. Please try again.");

}

} while (ch=='Y' || ch=='y');

// console.close();

}

// check the user enters the valid values

public static boolean isValid(String string) {

if (string.equalsIgnoreCase("rock")) {

return true;

}

if (string.equalsIgnoreCase("paper")) {

return true;

}

if (string.equalsIgnoreCase("scissor")) {

return true;

}

return false;

}

// assign the value for computer choice

public static String getAIChoice(int x) {

if (x == 1) {

return "rock";

}

if (x == 2) {

return "paper";

} else {

return "scissor";

}

}

// convert the user inputs to 1, 2, 3

public static int getValue(String string) {

if (string.equalsIgnoreCase("rock")) {

return 1;

}

if (string.equalsIgnoreCase("paper")) {

return 2;

} else {

return 3;

}

}

// create the whoWin method to determine who win

public static String whoWin(int userChoice, int pcChoice) {

// 1 == rock

// 2 == paper

// 3 == scissor

if (userChoice == 1) {

if (pcChoice != 2) {

if (pcChoice != 1) {

return "win.";

}

return "tie.";

}

return "loose.";

}

if (userChoice == 2) {

if (pcChoice != 3) {

if (pcChoice != 2) {

return "win.";

}

return "tie";

}

return "loose.";

}

if (userChoice == 3) {

if (pcChoice != 1) {

if (pcChoice != 3) {

return "win.";

}

return "tie.";

}

return "loose.";

}

return "";

}

}

================================================

SEE OUTPUT

PLEASE COMMENT if there is any concern.

==============================

============================

Add a comment
Know the answer?
Add Answer to:
(Java) Write a program that lets the user play the game of Rock, Paper, Scissors against...
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
  • 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....

  • 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