Question

In: Computer Science

One file java program that will simulate a game of Rock, Paper, Scissors. One of the...

One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report the win, loss, or tie and continue with another round. Repeating until either the user or the computer has won the correct number of times first. Please output what the user and computers choice is each time and give a running score total as the game goes on.

Output should look like:

Welcome to my game of Rock, Paper, Scissors!
How many winning rounds are needed for victory? :3

Round# 1
Enter 0=rock, 1=paper, 2=scissors:0

Human : Rock Computer : Rock
Draw, try again
Total Score: Human 0 Computer 0

Round# 2
Enter 0=rock, 1=paper, 2=scissors:1

Human : Paper Computer : Rock
Human wins this round
Total Score: Human 1 Computer 0

Round# 3
Enter 0=rock, 1=paper, 2=scissors:2

Human : Scissors Computer : Paper
Human wins this round
Total Score: Human 2 Computer 0

Round# 4
Enter 0=rock, 1=paper, 2=scissors:0

Human : Rock Computer : Rock
Draw, try again
Total Score: Human 2 Computer 0

Round# 5
Enter 0=rock, 1=paper, 2=scissors:0

Human : Rock Computer : Scissors
Human wins this round
Total Score: Human 3 Computer 0

It took 5 rounds, but you won!
Goodbye, play again sometime

Solutions

Expert Solution

CODE:

package com.company;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class Game {
    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        Random random = new Random();

        int choice,wins = 0;
        //these two variables store the wins of each player
        int HumanScore = 0, ComputerScore = 0;
        //this array stores the three choices
        String[] choices = {"Rock","Paper","Scissors"};
        //this 2D array in all the first elements in the sub arrays stores the input case
        //for each input case in a sub array there is another element over which it wins
        //like Rock in the first sub array wins over Scissors which is the second element in that sub array
        String[][] winCases = {{"Rock","Scissors"},{"Paper","Rock"},{"Scissors","Paper"}};

        //asking the user the number wins required to win the game
        System.out.println("Welcome to my game of Rock, Paper, Scissors");
        System.out.print("How many winning rounds are needed for victory?: ");
        wins = Integer.parseInt(in.readLine());
        int i =0;
        while(true){
            System.out.println("\nRound #"+(i+1));
            System.out.print("Enter 0=rock, 1=paper, 2=scissors: ");
            choice = Integer.parseInt(in.readLine());
            //getting the choice for computer
            String computer = choices[random.nextInt(3)];
            //getting the choice from the array
            String human = choices[choice];
            //if both are same then draw
            if(human.equals(computer)){
                System.out.println("Human: "+human+", Computer: "+computer);
                System.out.println("Draw, Try Again");
            }else{
                //if both are not the same
                //and if the computer's choice is the second element in the sub array in which
                //the first element is the human's choice for this round then human wins
                if(winCases[choice][1].equals(computer)){
                    System.out.println("Human: "+human+", Computer: "+computer);
                    System.out.println("Human Wins this round");
                    //human's score is incremented
                    HumanScore += 1;
                }else{
                    //otherwise the computer wins
                    System.out.println("Human: "+human+", Computer: "+computer);
                    System.out.println("Computer Wins this round");
                    ComputerScore += 1;
                }
            }
            i += 1;
            //displaying the score
            System.out.println("\nTotal score: Human: "+HumanScore+", Computer: "+ComputerScore);
            //if any of the two reach the winning point the loop breaks
            if(HumanScore == wins||ComputerScore == wins)
                break;
        }
        //displaying the winner
        if(HumanScore > ComputerScore){
            System.out.println("It took "+(i)+" rounds, but you won!");
        }else{
            System.out.println("It took "+(i)+" rounds for the computer to win!");
        }
        System.out.println("Goodbye, play again sometime");
    }
}

___________________________________________

CODE IMAGES:

______________________________________

OUTPUT:

______________________________________________

Feel free to ask any questions in the comments section
Thank You!


Related Solutions

Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the...
Write a Java class that determines the winner of a rock, paper scissors game. Assume the input from the user is always valid (so no need to check), that is it contains either one of `R`, `P`, or `S` as a single character, or has matching parenthesis, like, `(S&P)` or `((R&P)&S)`, and the `&` character. So for example, the user inputs `(P&R)` and the program will output `P` since paper beats rock. Or if the user inputs `((S&R)&(S&S))` the output...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper,...
In the game Rock Paper Scissors, two players simultaneously choose one of three options: rock, paper, or scissors. If both players choose the same option, then the result is a tie. However, if they choose differently, the winner is determined as follows: • Rock beats scissors, because a rock can break a pair of scissors. • Scissors beats paper, because scissors can cut paper. • Paper beats rock, because a piece of paper can cover a rock. Create a game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game...
Rock, Paper, Scissors Game Write a Python program rps.py that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows: You can set these constant global variables at the top outside of your main function definition: COMPUTER_WINS = 1 PLAYER_WINS = 2 TIE = 0 INVALID = 3 ROCK = 1 PAPER = 2 SCISSORS = 3 For this program 1 represents rock, 2 represents paper, and 3 represents scissors. In...
write a python script for rock scissors paper game
write a python script for rock scissors paper game
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When...
JAVA : Design and implement an application that plays the Rock-Paper-Scissors game against the computer. When played between two people, each person picks one of three options (usually shown by a hand gesture) at the same time, and a winner is determined. In the game, Rock beats Scissors, Scissors beats Paper, and Paper beats Rock. The program should randomly choose one of the three options (without revealing it) and then prompt for the user’s selection. At that point, the program...
Solve the scissors, paper, rock game. This game is well known in many parts of the...
Solve the scissors, paper, rock game. This game is well known in many parts of the world. Two players simultaneously present a hand in one of three positions: an open hand (paper), a closed fist (rock), or two open fingers (scissors). The payoff is 1 unit according to the rule “Paper covers rock, rock breaks scissors, and scissors cut paper.” If both players present the same form, the payoff is 0. Set up the payoff matrix for the game and...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's the user play...
Please use Python 21. Rock, Paper, Scissors Game Write a program that let's 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...
Please write a scheme code for the rock paper scissors game between a player and the...
Please write a scheme code for the rock paper scissors game between a player and the computer (AI). In scheme programming language please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT