In: Computer Science
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
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!