Rock, paper, scissors game Write a program that lets the user play the game of R
ID: 3904576 • Letter: R
Question
Rock, paper, scissors game Write a program that lets the user play the game of Rock, Paper, Scissors against the computer The program should work as follows. 1. When the program begins, a random number in the range of 1 through 3 is generated. 2. Ask to input the player's name (save that for later) If the number is 1, then the computer has chosen rock. b. a. If the number is 2, then the computer has chosen paper If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet.) c. The user enters his or her choice of "rock", "paper", or "scissors" at the keyboard. (You can use a menu if you prefer. 3 4. 5. The computer's choice is displayed. 6. 4. A winner is selected according to the following rules: a. If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.) b. If one player chooses scissorsand the other player chooses paper, then scissorswins. (Scissors cuts paper.) c. If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.)* d. If both players make the same choice, the game must be played again to determine the winner Create text file in append mode to register the Players name, and win loose or draw text according to the results above. Ask the user to either continue or quit playing Be sure to divide the program into methods that perform each major task. Analysis (Describe the problem including input and output in your own words.) Design: (Describe the major steps for solving the problem.) Coding: (Copy and Paste Source Code here. Copy and Paste Your program here]Explanation / Answer
Hello,
Hereby , i have written the java code for the game Rock,Paper and Scissors.
thank you
Player.java
public class Player {
private String name
Scanner sc = new Scanner(System.in);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void askName() {// Will let the user enter his name
System.out.println("Please enter your name");
name = sc.next();
}
public int getInput() {
System.out.println("Select ROCK PAPER SCISSOR");
String input = sc.next();
input = input.toUpperCase();
char c = input.charAt(0);
if (c == 'R')
return Gameplay.ROCK;
else if (c == 'P')
return Gameplay.PAPER;
else if (c == 'S')
return Gameplay.SCISSORS;
else {
getInput();
return 0;
}
}
public boolean playAgain() {
sc = new Scanner(System.in);
System.out.print("Do you want to play once more? ");
String userInput = sc.nextLine();
userInput = userInput.toUpperCase();
return userInput.charAt(0) == 'Y';
}
Computer.java
public class Computer {
public int getInput() {
Random random = new Random();
int input = random.nextInt(3) + 1;
return input;
}
}
Gameplay.java
public static final int ROCK = 1;
public static final int PAPER = 2;
public static final int SCISSORS = 3;
public static void display(String who, int s) {
switch (s) {
case Gameplay.ROCK:
System.out.print(who + " selected ROCK ");
break;
case Gameplay.PAPER:
System.out.print(who + " selected PAPER ");
break;
case Gameplay.SCISSORS:
System.out.print(who + " selected SCISSORS ");
break;
default:
break;
}
}
public static int compareChoices(int userChoice, int computerChoice) {
if(userChoice==computerChoice) return 0;
switch (userChoice) {
case ROCK:
return (computerChoice == SCISSORS ? 1 : -1);
case PAPER:
return (computerChoice == ROCK ? 1 : -1);
case SCISSORS:
return (computerChoice == PAPER ? 1 : -1);
}
return 0;
}
}
MAIN.java
private Player player;
private Computer computer;
private int playerScore;
private int computerScore;
private int numberOfAttempts ;
public MAIN() {
player = new Player();
computer = new Computer();
playerScore = 0;
computerScore = 0;
numberOfAttempts = 0;
}
public static void main(String[] args) {
MAIN Main = new MAIN();
Main.getplayerName();
Main.startGame();
}
public void startGame() {
int playerinput = player.getInput();
Gameplay.display(player.getName(), playerinput);
int computerinput = computer.getInput();
Gameplay.display("computer", computerinput);
int compareResult=Gameplay.compareChoices(playerinput, computerinput);
switch (compareResult) {
case 0:
System.out.println("Tie!");
break;
case 1:
System.out.println(player.getName()+ " Beats " + "computer" +" You won!");
playerScore++;
break;
case -1:
System.out.println("Computer" +" Beats "+ player.getName()+" You Lost!");
computerScore++;
break;
}
numberOfAttempts ++;
if(playerScore==5)
{
System.out.println(player.getName()+" HAS WON THE GAME.............");
new MAIN();
}
if(computerScore==5)
{
System.out.println("Computer "+" HAS WON THE GAME............");
new MAIN();
}
if (player.playAgain()) {
System.out.println();
startGame();
} else {
printStatus ();
}
}
public void getplayerName() {
player.askName();
}
public void printStatus ()
{
System.out.println("Number of games played is "+numberOfAttempts );
System.out.println(player.getName()+"'s score "+playerScore);
System.out.println("Computers score "+computerScore);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.