In: Computer Science
1.Create full program in Java that will simulate a Rock Paper Scissors Game You will create the code so that when the program is run the user will see in the console the following: We are going to play rock paper scissors. Please choose 1 for Rock 2 for Paper or 3 for scissors. The program will have your choice which is the integer-valued typed in by the user and compChoice which will be the randomly generated value of either 1 2 or 3. You will test all the ways the user wins if he or she beats the computer using || with if and then you will check all the ways that computer could win using else if with || and lastly the only other thing that can happen would be a tie if both choices are the same for the default else. Display what was chosen and what was randomly chosen by the computer and who wins or if there is a tie. For extra credit, you can use dialog boxes and show images.
Hi, I Hope you are doing will.
Below is the program for ROCK PAPER AND SCISSORS Game
import java.util.*;
class Game
{
public static final String ROCK = "ROCK";
public static final String PAPER = "PAPER";
public static final String SCISSORS = "SCISSORS";
public static void main(String args[])
{
System.out.println("Enter any of the following inputs: ");
System.out.println("1 for ROCK");
System.out.println("2 for PAPER");
System.out.println("3 for SCISSORS");
System.out.println();
String playerMove = getPlayerMove();
String computerMove = getComputerMove();
/*if both playerMove and computerMove
produces the same formation, then
Game is tie*/
if (playerMove.equals(computerMove))
System.out.println("Game is Tie !!");
// if player's Move is ROCK and input is 1
else if (playerMove.equals(Game.ROCK))
System.out.println(computerMove.equals(Game.PAPER) ? "Computer Wins": "Player wins");
// if player's Move is PAPER and input is 2
else if (playerMove.equals(Game.PAPER))
System.out.println(computerMove.equals(Game.SCISSORS) ? "Computer Wins": "Player wins");
// if player's Move is SCISSORS and input is 3
else
System.out.println(computerMove.equals(Game.ROCK) ? "Computer Wins": "Player wins");
}
/* Get Computer's move using Random method */
public static String getComputerMove()
{
String computermove;
Random random = new Random();
int input = random.nextInt(3)+1;
if (input == 1)
computermove = Game.ROCK;
else if(input == 2)
computermove = Game.PAPER;
else
computermove = Game.SCISSORS;
System.out.println("Computer move is: " + computermove);
System.out.println();
return computermove;
}
/* Get Player's move using Scanner
class */
public static String getPlayerMove()
{
Scanner in = new Scanner(System.in);
int playermove = in.nextInt();
System.out.println("Player move is: "+playermove);
if(playermove == 1)
return "ROCK";
else if(playermove == 2)
return "PAPER";
else
return "SCISSORS";
}
}
OUTPUT: