In: Computer Science
Ch 4 Program 5: RockPaperScissors.java
Please adhere to the Standards for Programming Assignments and the Java Coding Guidelines.
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
Hints:
Example Output with Valid inputs:
********Rock Paper Scissors*************** Enter one of the following: -- Rock -- Paper --Scissors Rock You picked rock Computer picked rock Winner: Tie Do you want to play again: Y/N ? y ********Rock Paper Scissors*************** Enter one of the following: -- Rock -- Paper --Scissors Scissors You picked scissors Computer picked rock Winner: Computer Do you want to play again: Y/N ? y ********Rock Paper Scissors*************** Enter one of the following: -- Rock -- Paper --Scissors paper You picked paper Computer picked rock Winner: Player Do you want to play again: Y/N ? N You won 1 times. You lost 1 time. We tied 1 times. Good Bye! |
********Rock Paper Scissors*************** Enter one of the following : -- Rock -- Paper --Scissors Scissors You picked scissors Computer picked paper Winner: Player Do you want to play again: Y/N? y ********Rock Paper Scissors*************** Enter one of the following : -- Rock -- Paper --Scissors rack Invalid Choice! Do you want to play again: Y/N? y ********Rock Paper Scissors*************** Enter one of the following : -- Rock -- Paper --Scissors Rock You picked rock Computer picked paper Winner: Computer Do you want to play again: Y/N ? n You won 1 times. You lost 1 times. We tied 0 times. Goodbye!! |
Example Output with an Invalid input:
********Rock Paper Scissors*************** Enter one of the following: -- Rock -- Paper --Scissors Scissors You picked scissors Computer picked paper Winner: You Do you want to play again: Y/N ? y ********Rock Paper Scissors*************** Enter one of the following: -- Rock -- Paper --Scissors rack Invalid Choice! Do you want to play again: Y/N ? Y ********Rock Paper Scissors*************** Enter one of the following: -- Rock -- Paper --Scissors Rock You picked rock Computer picked paper Winner: Computer Do you want to play again: Y/N ? n You won 1 times. You lost 1 times. We tied 0 times. Goodbye!! |
Answer :
The program is using 0 for scissor, 11 for paper, 22 for rock.So, choose the numbers accordingly.
And the Score will be given in this way (Loss:-1,Win:+1,Draw:0)
The program consists of a class name "Game" and if else conditions for different conditions.
import java.util.Scanner;
public class Game{
public static void main(String[] Args) {
boolean yes = true;
boolean no=false;
int b;
char choice;
int score=0;
String winner ;
System.out.println("Write 0 for ✂,11 for✋ and 22 for?");
System.out.println("and then press the enter key.");
System.out.println("Good luck.");
for(int i=0;i<5;i++){
String str1,str2;
int a = (int)(Math.random()*3); // will generate random paper,
rock, scissors
Scanner sc = new Scanner(System.in);
b = sc.nextInt();
if (b==0){
System.out.println("Your move:✂");
if(no){
no=!no;
}
}
if (b==11){
System.out.println(b);
if(no){
no=!no;
}
System.out.println("Your move:✋");
}
if (b==22){
System.out.println("Your move:?");
if(no){
no=!no;
}
}
if(b!=00&&b!=11&&b!=22){
System.out.println("Invalid move.");
no=true;
i--;
}
if(!no){
if (a==0){
System.out.println("comp move:✂");
}
if (a==1){
System.out.println("comp move:✋");
}
if (a==2){
System.out.println("comp move:?"); }
}
if(a==b){
System.out.println("Tie");
}
else{
if(a==0&&b==11){
System.out.println("Computer won");
score--;
}
if(a==0&&b==22){
score++;
winner = "you have";
System.out.println(winner+" won");
}
if(a==1&&b==11){
System.out.println("TIE");
}
if(a==1&&b==22){
winner = "computer";
System.out.println(winner+" has won");
score--;
}
if(a==1&&b==0){
System.out.println("You won");
score++;
}
if(a==2&&b==22){
winner = "";
System.out.println("TIE");
}
if(a==2&&b==11){
System.out.println("You won.");
score++;
}
if (a==2&&b==0){
System.out.println("Computer won.");
score--;
}
}
}
System.out.println("Nice game.");
System.out.println("score="+score+"(Loss:-1,Win:+1,Draw:0)");
//Result
if (score == 0){
System.out.println("Game draw.");
}
else if(score >0){
System.out.println("You won the series.");
}
else if(score < 0){
System.out.println("Computer won the series.");
}
}
}
I hope this answer is helpful to you. If you like Please Upvote(Thums Up) my answer, I'm need of it, Thank you.