In: Computer Science
Objectives
• Use the static methods and static fields.
•Use the method-call/return mechanism.
• Use the random-number generation.
• Overload methods. Problem Specification
Write a program that lets a user play "Rock, Paper, Scissors" against the computer or computer against computer. In user-computer game, the program should ask the user to choose one of the three choices, and then the computer randomly picks one (without knowing what the user has chosen). For this problem, the user should be asked to enter an integer: 1 for rock, 2 for paper, 3 for scissors. Rock beats scissors, scissors beats paper, paper beats rock. The program should say who wins, and then keep playing until someone (the user or the computer) has won 5 rounds. The computer needs to keep track of the current score and should also display it before each round. In computer-computer game, play for 100 rounds and display which object won majority of the times.
Design Specification
• Create Java class RPS.
• Create static variables for storing Score1 and Score2.
• Create a static method getRandom to get random integer between 1 and 3.
• Overload play method like in the below screenshot.
• Play() is for computer vs computer, play(userchoice) is for user vs computer.
in Java, please
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// RPS.java
import java.util.Scanner;
public class RPS {
static int score1=0;
static int score2=0;
static int getRandom()
{
return (int)(Math.random()*(3));
}
void play()
{
int comp1,comp2,res;
for(int i=1;i<=100;i++)
{
comp1=getRandom();
comp2=getRandom();
res=whoWon(comp1, comp2);
if(res==1)
{
score1++;
}
else
if(res==2)
{
score2++;
}
}
System.out.println("Computer1 Score
"+score1+" Computer2 score "+score2);
if(score1>score2)
{
System.out.println("Computer1 won");
}
else if(score1<score2)
{
System.out.println("Computer2 won");
}
else
{
System.out.println("It's a Tie.");
}
}
void play(int userChoice)
{
int compChoice=getRandom();
int res=whoWon(userChoice,compChoice);
if(res==1)
{
score1++;
}
else if(res==2)
{
score2++;
}
else
{
System.out.println("ITS A
TIE!");
}
}
int whoWon(int choice1,int choice2)
{
int res=0;
if(choice1==1 && choice2==1)
{
res=0;
}
else if(choice1==1 && choice2==2)
{
res=2;
}
else if(choice1==1 && choice2==3)
{
res=1;
}
else if(choice1==2 && choice2==1)
{
res=1;
}
else if(choice1==2 && choice2==2)
{
res=0;
}
else if(choice1==2 && choice2==3)
{
res=2;
}
else if(choice1==3 && choice2==1)
{
res=2;
}
else if(choice1==3 && choice2==2)
{
res=1;
}
else if(choice1==3 && choice2==3)
{
res=0;
}
return res;
}
public static void main(String[] args) {
int choice;
/*
* Creating an Scanner class
object which is used to get the inputs
* entered by the user
*/
Scanner sc = new
Scanner(System.in);
System.out.println("Select mode : Enter 1 for Computer
vs Computer , Enter 2 User vs Computer ");
choice=sc.nextInt();
RPS r=new RPS();
if(choice==1)
{
r.play();
}
else
{
for(int i=1;i<=5;i++)
{
System.out.println("Select 1: Rock
2:Paper 3:Scissors");
choice=sc.nextInt();
r.play(choice);
System.out.println("You score
:"+score1+" Computer Score :"+score2);
}
System.out.println("Total no of games 5");
if(score1>score2)
{
System.out.println("You
won");
}
else if(score1<score2)
{
System.out.println("You
lost");
}
else
{
System.out.println("ITS
TIE");
}
}
}
}
==================================
Output#1:
Select mode : Enter 1 for Computer vs Computer ,
Enter 2 User vs Computer
1
Computer1 Score 15 Computer2 score 11
Computer1 won
==================================
Output#2:
Select mode : Enter 1 for Computer vs Computer ,
Enter 2 User vs Computer
2
Select 1: Rock 2:Paper 3:Scissors
1
You score :0 Computer Score :1
Select 1: Rock 2:Paper 3:Scissors
2
You score :1 Computer Score :1
Select 1: Rock 2:Paper 3:Scissors
2
ITS A TIE!
You score :1 Computer Score :1
Select 1: Rock 2:Paper 3:Scissors
3
You score :1 Computer Score :2
Select 1: Rock 2:Paper 3:Scissors
1
You score :1 Computer Score :3
Total no of games 5
You lost
=====================Could you plz rate me well.Thank You