99.9k views
1 vote
You are to write a 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 computer's choice by using a random number. 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 computer's choice is each time and give a running score total as the game goes on. Each of these programs must be done in a separate file. Name them.

1 Answer

1 vote

Answer:

The code is written in the explanation.

Step-by-step explanation:

the code is the following:

import java.util.*;

class Game{

}

public class Main

{

static scanner in = new Scanner(System.in);

public static final String Rock = "Rock";

public static final String Paper = "Paper";

public static final String Scissors = "Scissors";

/*Get computer's move using Random

class nextInt() method */

public static string getcomputermove()

{

string computermove;

Random random = new Random();

int input = random.nextInt(3)+1;

if (input == 1)

computermove = Main.Rock;

else if(input == 2)

computermove = Main.Paper;

else

computermove = Main.Scissors;

System.out.println("computer move is: " + computermove);

System.out.println();

return computermove;

}

/* get player's move using scanner

class */

public static String getplayermove()

{

String input = in.next();

String playermove = input.toUpperCase();

System.out.println("player move is: "+ playermove);

return playermove;

}

public static void main(String args[])

{

System.out.println("welcome to rock, paper, scissors");

System.out.println("enter number of winning rounds");

int r=in.nextInt();

int playerScore=0;

int computerScore=0;

while(playerScore!=r && computerScore!=r){

System.out.println("enter any one of the following inputs: ");

System.out.println("Rock");

System.out.println("Paper");

System.out.println("Scissors");

System.out.println();

String playerMove = Main.getplayermove();

String computerMove = Main.getcomputermove();

//rules of the game applied:

/*if both playermove and computermove

produces the same selection, then

game is a tie*/

if (playermove.equals(computermove))

System.out.println("Game is tie");

// if playermove is Rock

else if (playerMove.equals(Main.Rock))

if(computerMove.equals(Main.Paper)){

System.out.println("computer wins");

computerscore=1;

}else{

System.out.println("player wins");

playerscore=1;

}

// if playermove is Paper

else if (playermove.equals(Main.Paper)){

if(computermove.equals(Main.Scissors)){

System.out.println("computer wins");

computerscore=1;

}else{

System.out.println("player wins");

playerscore=1;

}

}

// if playerMove is Scissors

else{

if(computermove.equals(Main.Rock)){

System.out.println("computer wins");

computerscore=1;

}else{

System.out.println("player wins");

playerscore=1;

}

}

System.out.println("After the round, the computer score is:"+computerscore+" and the player score is:"+playerscore);

}

if(computerscore==r){

System.out.println("You lose, computer wins");

}else{

System.out.println("You Won");

}

}

}