Question

In: Computer Science

Ch 4 Program 5: RockPaperScissors.java                                    &

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

  • Rock beats scissors because a rock can break a part of scissors
  • Scissors beats paper because scissors can cut paper
  • Paper beats rock because a piece of paper can cover a rock

  1. Create a game, in which the computer randomly chooses rock, paper, or scissors(without revealing it).

  1. Let the player enter “rock,” “paper” or “scissors” as a string and then determine the winner or a tie. The player can enter rock, paper, or scissors using either upper case, lower case, or a combination of cases.

  1. The program must validate the player’s choice. If the input is invalid, then the program writes an appropriate message, informing the player. If the input is valid, then the program informs the player who won(computer or the player ) or is it a tie.

  1. The program then asks the player if he/she wants to continue playing. The player must answer either “yes” or “No” in either upper or lower case.

  1. The game ends when the player chooses to stop. It then prints the total number of player’s wins, losses, and ties.

Hints:  

  • You can assign a number to each “rock,” paper,” and “scissors” strings, in order to determine what the computer picks randomly. For example, if the random number generated is a “1” then computer picks rock and so on.  
  • Review methods in String and Random classes to find the relevant ones.

  • Do not try to write the entire program in one go. It is much easier to write a small piece and test it, then write another small piece and test it. For example, start by writing the game logic without worrying about the input validation. Then add the code that validates the input. Then add each additional step, one at a time. You should test your program with simple test inputs to check that you handle each case.

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!!

Solutions

Expert Solution

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.


Related Solutions

Ch 4 Program 3  – Geometry Calculator Please adhere to the Standards for Programming Assignments and the...
Ch 4 Program 3  – Geometry Calculator Please adhere to the Standards for Programming Assignments and the Java Coding Guideline. : List of source code with comments to document Test output listed as comments at the end of your program GeometryCalculator.java (15 pts) Write a program that displays the following menu: Calculate the area of a circle Calculate the area of a rectangle Calculate the area of a triangle Calculate the volume of a cylinder Quit Prompt the user to enter...
Ch 5 Program 1: Sphere.java                Complete the implementation of the Sphere class based on the Diagram...
Ch 5 Program 1: Sphere.java                Complete the implementation of the Sphere class based on the Diagram below. Use the DecimalFormat class in Sphere.java to format your output in the toString method. Then download the tester application SphereTester.java from canvas to test your Sphere class. Do not change SphereTester.java. Sphere private double radius //radius of the sphere object private static int numSpheres //static or class variable. All Sphere objects share it public Sphere() //constructor . Initialize Sphere objects’ instance variable radius...
6.27 Ch 2 Program: Painting a wall (C++) (4)Calculate the cost of the paint. Paint comes...
6.27 Ch 2 Program: Painting a wall (C++) (4)Calculate the cost of the paint. Paint comes in six colors (Red, Yellow, Green, Blue, Black,White) Assign numbers 0-5 to these paint colors ie Red =0 and White =5. The costs for each of these paints is as follows:(2 pts) Now your program will be submission ready. Main.cpp #include <iostream> #include <cmath> using namespace std; #include "paint.h" int main() {    double wallHeight;    double wallWidth;    double wallArea;    double gallonsPaintNeeded;...
Ch 21The Lymphatic and Immune Systems   4. Describe how lymph nodes function as lymphatic organs.   5....
Ch 21The Lymphatic and Immune Systems   4. Describe how lymph nodes function as lymphatic organs.   5. Describe the function, recirculation, and activation of lymphocytes.   6. Relate the structure of lymphoid tissue to its infection-fighting function.   7. Describe the locations, histological structure, and immune functions of the following lymphoid organs: thymus, lymph nodes, spleen, tonsils, aggregated lymphoid nodules in the intestine and appendix.   8. Describe the basic characteristics of two disorders of the lymphatic vessels: chylothorax, lymphangitis; and three disorders of...
The thermal decomposition of dimethyl ether CH 3 2 O g CH 4 g H 2...
The thermal decomposition of dimethyl ether CH 3 2 O g CH 4 g H 2 g CO g is to be carried out in an isothermal 2.00-liter laboratory reactor at 600°C. The reactor is charged with pure dimethyl ether at a pressure of 350 torr. After about two hours, the reactor pressure is 875 torr. (a) Has the reaction proceeded to completion at the end of the two-hour period? If not, what percentage of the dimethyl ether has decomposed?...
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6)....
Write a Python program that has a list of 5 numbers [2, 3, 4, 5, 6). Print the first 3 elements from the list using slice expression. a. Extend this program in a manner that the elements in the list are changed to (6, 9, 12, 15, 18) that means each element is times 3 of the previous value. b. Extend your program to display the min and max value in the list.
3. Refer to Ch 4, pg 132- 4 in BM. We are going to calculate the...
3. Refer to Ch 4, pg 132- 4 in BM. We are going to calculate the velocity of the Earth today (let’s say 7 Feb 2020) using the vis viva equation. Take a deep breath... (a) Calculate the "mean anomaly" of the Earth M. This is the angle from perihelion to the position of Earth as if the Earth were in a circular orbit. You have to look up the perihelion of the Earth. See pg 34 in BM. This...
Biological Anthropology: Ch. 3- Genetics: Reproducing Life & Producing Variation Ch. 4-Genes Their Evolution: Population Genetics...
Biological Anthropology: Ch. 3- Genetics: Reproducing Life & Producing Variation Ch. 4-Genes Their Evolution: Population Genetics (Please TYpe) Chapter 4 1. Microevolution, macroevolution 2. Reproductive isolation 3. Hardy-Weinberg Law 4. Four forces of evolution 5. Mutation types 6. Three patterns of natural selection 7. Admixture, founder’s effect Chapter 5 1. Race: historical and modern concepts, issues with the concept, etc. 2. Blumenbach, Boas, Lewontin 3. Four levels of human adaptation 4. Terms related to adaptation (stress, homeostasis, plasticity, functional adaptation)...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase struct to contain a new data member. (2 pt) char itemDescription[ ] - set to "none" in MakeItemBlank() Implement the following related functions for the ItemToPurchase struct. PrintItemDescription() Has an ItemToPurchase parameter. Ex. of PrintItemDescription() output: Bottled Water: Deer Park, 12 oz. (2) Create three new files: ShoppingCart.h - struct definition and...
Readings: Early Ch 6 & Ch 9, Solomon Ch 12 & 25, Lippert Ch 6 Module...
Readings: Early Ch 6 & Ch 9, Solomon Ch 12 & 25, Lippert Ch 6 Module Learning Objectives: Define the structures that make up the neurological system. Explain the importance of each structure as it relates to the overall function of the body Compare and contrast the clinical presentations of each disease process of the neurological system. Give examples of the functional implications of each disease process in both the adult and pediatric populations. Provide general occupational therapy assessments &...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT