Question: Write a program that simulates Rock, Paper, Scissors in Java. Rules of the game are as follows: Rock smashes Scissors Paper covers Rock Scissors cuts
Write a program that simulates Rock, Paper, Scissors in Java. Rules of the game are as follows:
Rock smashes Scissors
Paper covers Rock
Scissors cuts Paper
You are to write a program that plays the game against the computer
Allow the user to input their name
The computer should choose an object randomly and your program should prompt the user to enter his/her choice OR with a *GUI
You are at liberty to have the user enter a number or the string.
Use a decision structure of your choice to determine a winner
Allow the user to play a set amount of times OR allow the user to determine the number of times they will play
The program should then declare the winner, whether user or CPU and how the winner won the game OR with GUI
Your program should keep track of the number of wins and losses for each and also the number of ties. OR display the results GUI
Output their name when giving them the summary of their games OR GUI
Anything unique after completing all the requirements or a more creative approach
*GUI - Graphical User Interface, any one of your choice (JOptionPane)
Simplest Input and Output
Sample Output: Please enter your name.
Sample Input: Marquez
Sample Output: Please choose, Rock, Paper, or Scissors (Explain the game to them here)
Sample Input: Either a 1, 2 or 3 OR the words "Rock", "Paper", "Scissors"
Sample Output: The computer chose Rock and You chose Paper, you win.
Sample Output: Would you like to play again?
Sample Output: Marquez, You won 1 of 1 games.
-
-
-
This is what I have so far:
-
-
Name of programming assignment: RockPaperScissors
Assignment Description: A short program that lets the user play a game
of rock, paper, scissors against the computer. The program utilizes
if, else if, and while statements; along with a random object in order to
accomplish the task of this program*/
//package declaration
package rockPaperScissors;
//imports
import java.util.Scanner; //creates import for user input
import java.util.Random; //creates import for the random object
//class declaration
public class CopyOfCopyOfFinalRockPaperScissors
{
//main method
public static void main(String [] args)
{
//declare all variables
int compChoice; //to hold the computer's choice
int userChoice; //to hold the user's choice
int compScore = 0; //to hold the computer's score (default 0)
int userScore = 0; //to hold the user's score (default 0)
int ties = 0; //to hold the games number of ties (default 0)
String playGame; //to hold user input for if they want to play
boolean startGame; //to initialize the yes/no question if the user wants to play
boolean endGame; //to hold the user input for if they do not want to play
//create a scanner object
Scanner kb = new Scanner(System.in);
//create the random object
Random rand = new Random();
//display introduction message
System.out.println("Welcome to Jake's RockPaperScissor's game!");
System.out.println("The game will run until the first player reaches ten wins.");
//ask the user if they would like to start a game
System.out.println("Would you like to start the game? (Y/N)");
//save the user's input to the playGame variable
playGame = kb.nextLine();
//start the game if the user's choice = y
startGame = playGame.equalsIgnoreCase("Y");
//end the game if the user's choice != y
endGame = playGame.equalsIgnoreCase("N");
//if the user's choice != y, then close the program
while(endGame)
{
System.out.println("Thanks!");
System.exit(0);
}
//if user's choice = Y, then begin the while loop
while(startGame && compScore <10 && userscore<10)>
{
System.out.println("Controls: Enter 0 for rock, 1 for paper, and 2 for scissors"
+ " " + "(Rock smashes Scissors, Paper covers Rock and Scissors cuts Paper)");
userChoice = kb.nextInt(); //store the user's choice
compChoice = rand.nextInt(3); //initialize a random computer choice b/w 1 and 3
//check if the user inputed valid data
if(userChoice < 0 || userChoice > 2)
{
System.out.println("Invalid choice. Ending program.");
// Exit program
System.exit(0);
}
//compare the choices between the user and the computer
if(userChoice == 0) // User chooses rock
{
if(compChoice == 1)
{
System.out.println("You chose rock; Computer chose paper");
System.out.println("Computer wins!");
compScore++; //increment the computer's score by 1
}
if(compChoice == 2)
{
//checks if the user wins
System.out.println("You chose rock; Computer chose scissors");
System.out.println("You win!");
userScore++; //increment the user's score by one
}
if(compChoice == 0 && userChoice == 0)
{
System.out.println("You chose rock, and computer chose rock");
System.out.println("It's a tie!");
ties++;
}
}
else if(userChoice == 1) // User chooses paper
{
if(compChoice == 0)
{
System.out.println("You chose paper; Computer chose rock");
System.out.println("You win!");
userScore++; //increment the computer's score by one
}
if(compChoice == 2)
{
System.out.println("You chose paper; Computer chose scissors");
System.out.println("Computer wins!");
compScore++; //increment the computer's score by one
}
if(compChoice == 1 && userChoice == 1)
{
System.out.println("You chose paper, and computer chose paper");
System.out.println("It's a tie!");
ties++;
}
}
else if(userChoice == 2) // User chooses scissors
{
if(compChoice == 0)
{
System.out.println("You chose scissors; Computer chose rock");
System.out.println("Computer wins!");
compScore++; //increment the computer's score by one
}
if(compChoice == 1)
{
System.out.println("You chose scissors; Computer chose paper");
System.out.println("You win!");
userScore++; //increment the user's score by one
}
if(compChoice == 2 && userChoice == 2)
{
System.out.println("You chose Scissors, and computer chose Scissors");
System.out.println("It's a tie!");
ties++;
}
}
System.out.println("Your current score: " + userScore); //displays the user's current score
System.out.println("Computer's current score: " + compScore); //displays the computer's current score
System.out.println("Current number of ties: " + ties); //displays the current number of ties
}
//determine who reached 10 wins first
if (userScore>compScore)
System.out.println("You won 10 games first and are victourious!");
else
System.out.println("The computer reached 10 wins first; therefore you are a loser!");
//close the scanner
kb.close();
//terminate the program
System.exit(0);
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
