Write a program to play a game of rock/paper/scissors; the user will play agains
ID: 3641178 • Letter: W
Question
Write a program to play a game of rock/paper/scissors; the user will play against the computer.
Requirements:
* All screen input/output will be from gui display and input boxes. (Valid inputs are R, S and P)
* The computer’s set of moves will be stored in a file, so the user only get’s to see their own selection.
* Exceptions handling will be implemented for the file access using try/catch statements.
* The game will start by displaying a nice information box explaining the game, and then it will ask the user for their name. The computers name will be set in the constructor, so you as a programmer can name your “computer”.
* Ask the user how many times you want to play the game, as long as the total number is less than 10. It is possible that the computer data file has less number of moves than the number of times entered by the player:
o Player enters 6 but the computer move file has 5; the game will be played 5 times.
o Player enters 5 but the computer move file as 10 entries, the game will be played 5 times.
o A maximum of 10 rounds can be played in one session.
* The game will determine who wins each round, and then display a final winner after all rounds have been played.
The program will consist of three classes:
* Test Game: Driver program that runs the game
* Player: Store the player information (Name, # of win, ties, loss). Remember the computer player name will be set through the constructor. Give a creative name for your computer.
* RockPaperScissor: Implements logic to instantiates players, set player choice, play game, display the result of the game and the final winner.
Game Rules
Player Computer Results
R R Tie
R P P2 win
R S P1 win
P R P1 win
P P tie
P S P2win
S R P2win
S P P1win
S S Tie
ComputerMove.dat
R
P
S
S
P
R
Explanation / Answer
import java.util.Scanner;
public class RockGame {
private final static int ROCK=1;
private final static int PAPER =2;
private final static int SCISSOR =3;
public static void main(String args[]){
double playerOneScore = 0;
double computerScore = 0;
int userPlay, computerPlay;
Scanner key = new Scanner(System.in);
while(playerOneScore <2 && computerScore <2){
System.out.println("choose 1 for rock 2 for paper 3 for sciscors");
userPlay = key.nextInt();
computerPlay = (int)(Math.random()*3) +1;
if (userPlay==1)
userPlay = ROCK;
else if (userPlay==2)
userPlay =PAPER;
else if (userPlay==3)
userPlay=SCISSOR;
if (computerPlay==1)
computerPlay = ROCK;
if (computerPlay==2)
computerPlay =PAPER;
if (computerPlay==3)
computerPlay=SCISSOR;
if (computerPlay ==ROCK && userPlay==SCISSOR ){
System.out.println("computer chose rock you lose");
computerScore++;
}
if (computerPlay ==ROCK && userPlay==PAPER ){
System.out.println("computer chose rock you win");
playerOneScore++;
}
if (computerPlay ==PAPER && userPlay==SCISSOR ){
System.out.println("computer chose scissor you win");
playerOneScore++;
}
if (computerPlay ==PAPER && userPlay==ROCK ){
System.out.println("computer chose paper you lose");
computerScore++;
}
if (computerPlay ==SCISSOR && userPlay==ROCK ){
System.out.println("computer chose scissor you win");
playerOneScore++;
}
if (computerPlay ==SCISSOR && userPlay==PAPER ){
System.out.println("computer chose scissor you lose");
computerScore++;
}
if (computerPlay == userPlay ){
System.out.println("TIE");
}
}
if(computerScore > playerOneScore)
System.out.println("hahaha computer win score is - "+ computerScore + " -" + playerOneScore );
else
System.out.println("congrats you beat a random gen computer score is " + playerOneScore + "-" + computerScore );
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.