Rock paper scissors class design problem using JAVA. Below I posted what needs t
ID: 3845215 • Letter: R
Question
Rock paper scissors class design problem using JAVA.
Below I posted what needs to be done(2 classes and 1 driver)
I also posted a grading criteria.
Class Design: Player The Player class is intended to be an abstract and simplified representation of someone playing a “Rock, Paper, Scissors” game.
Class Properties (MUST be private)
• Name or nickname of player
• Array storing the player’s choices for all rounds in a game Class Invariants
• Player name must be at least three characters long.
• Choices can only be “Rock,” “Paper,” or “Scissors” Class Components
• Constructor that takes in as argument the player’s name (enforce invariants)
• Public Getters for name and choices o Optional: Private Setter for name and choices
• Public method that takes in a choice and stores it in the array of choices (enforce invariants)
Class Design: Game
The Game class is intended to be an abstract and simplified representation of a “Rock, Paper, Scissors” game. This object should keep track of players and outcomes for the game.
Class Properties (MUST be private)
• First player
• Second player Class Invariants
• Players must have different names (otherwise ambiguous as to who the winner is)
• Players must each have made at least one choice (at least one round played) Class Components
• Constructor that takes in as arguments two players (enforce invariants)
• NO public getters or setters (private getters/setters acceptable)
• Public method that returns the name of the winner of the round
o You will need to compare the choices that the players have made and determine who the winner is – assume “best 2 out of 3” rule
If a player only made two choices and won those two choices, they won regardless of how many choices the other player made
If both players only played enough rounds to end in a tie, you must determine what to return in the case of a tie
If a player did not make enough choices to win, that player is considered to have forfeit the game and the other player wins. Examples: Player 1 Rock Rock Paper Player 2 Scissors Paper Paper Rock
This results in “Player 2” winning because it was a tie when “Player 1” gave up. Player 1 Rock Rock Scissors Player 2 Scissors Paper Paper Rock
This results in “Player 2” winning because “Player 1” is presumed to have given up before the round concludes (even though “Player 1” was winning by the third choice).
Driver Class: GameDriver
The GameDriver class is intended to be a simple driver to test the Game object:
• Create two players (e.g. player 1 and player 2)
• Add some choices to each player such that player 1 wins
• Create a Game
• Print out the winner of the game
• Create another game that results in a tie
GRADING CRITERIA:
• Player class object (Total: 12 points)
o [1 point] Proper Class definition syntax
o [2 points] Implements all required properties
o [1 points] Implements required getters
o [1 points] Name invariant properly enforced
o [3 points] Choice invariant properly enforced
o [2 points] Constructor properly implemented
o [2 points] add choice method properly implemented
• Game class object (Total: 16 points)
o [1 point] Proper Class definition syntax
o [2 points] Implements all required properties
o [3 points] All invariants properly enforced
o [2 points] Constructor properly implemented
o Method to determine winner works correctly to determine
[3 points] Player 1 wins
[3 points] Player 2 wins
[2 points] Tie • GameDriver driver class (Total: 12 points)
o [1 point] Proper Class definition syntax
o [1 point] Proper main method syntax
o [3 points] Created two players
o [2 points] Created a game
o [2 points] Print out the winner of the game
o [3 points] Created another game that results in a tie
• Extra Credit [2 points] for proper creation/usage of an enum for choices
Explanation / Answer
import tio.*;
public class RockPaperScissors {
public static void main (String[] args) {
int userScore = 0;
int compScore = 0;
String userMove, compMove;
int winner;
System.out.println("ROCK - PAPER - SCISSORS");
System.out.println("This game plays 10 rounds.");
// here Loop once for each round of the game.
for (int rnd=1; rnd<=10; rnd++) {
// here Get the moves for this round.
userMove = getUserMove();
compMove = getComputerMove();
System.out.println("Computer's move: " + compMove);
// it Determines the winner of this round.
winner = determineRoundWinner(compMove, userMove);
// Print a message and add one to the score of the winner.
if (winner == 1) {
System.out.println(compMove + " beats " +
userMove +
" the computer wins this round.");
compScore++;
}
else if (winner == -1) {
System.out.println(userMove + " beats " +
compMove +
" the user wins this round.");
userScore++;
}
else {
System.out.println(userMove + " ties " +
compMove +
" nobody wins this round.");
}
System.out.println("Score: User=" + userScore +
" Computer=" + compScore);
System.out.println();
}
// The ten rounds are over...
// Print out the winner of the game.
displayGameWinner(userScore, compScore);
}
/**
* Read in a move from the user. Valid
* responses are Rock, Paper or Scissors.
* Prompt until a valid move is entered.
*
* @return The user's move as one of:
* Rock, Paper, Scissors
*/
static String getUserMove() {
String userMove = "club";
// Loop while the user has not entered a valid
// move. NOTE: The ==, != operators do not work
// on String objects. However, the String
// object provides an equals method that returns
// true if the String is equal to the argument.
while (!userMove.equals("ROCK") &&
!userMove.equals("PAPER") &&
!userMove.equals("SCISSORS")) {
System.out.print("Enter your move " +
"[ROCK, PAPER, SCISSORS]: ");
// Read the user's move and convert it to
// uppercase to make the comparision easier.
userMove = Console.in.readLine();
userMove = userMove.toUpperCase();
}
return userMove;
}
/**
* Pick the computer's move at random.
*
* @return The computer's move as one of:
* Rock, Paper, Scissors
*/
static String getComputerMove() {
int compMoveInt;
String compMove;
// Generate a random move for the
// computer.
compMoveInt = randomInt(1,3);
// Convert the random integer into a
// string that represents the computer's
// move.
if (compMoveInt == 1) {
compMove = "ROCK";
}
else if (compMoveInt == 2) {
compMove = "PAPER";
}
else {
compMove = "SCISSORS";
}
return compMove;
}
/**
* Generate a random integer in the range [lowEnd...highEnd].
*
* @param lowEnd the low end of the range of possible numbers.
* @param highEnd the high end of the range of possible numbers.
* @return a random integer in [lowEnd...highEnd]
*/
public static int randomInt(int lowEnd, int highEnd) {
int theNum;
// Pick a random double in the range [0...highEnd-lowEnd+1)
// then truncate it to an integer and shift it up by lowEnd.
theNum = (int)(Math.random() * (highEnd - lowEnd + 1)) + lowEnd;
return theNum;
}
/**
* Compare the user's move to the computer's
* move to determine the winner of this round.
*
* @param userMove the user's move.
* @param compMove the computer's move.
* @return 1 if the computer wins.
* 0 if it is a tie.
* -1 if the user wins.
*/
static int determineRoundWinner(String userMove,
String compMove) {
int winner;
// Check for ties.
if (compMove.equals(userMove)) {
winner = 0;
}
// if it is not a tie check for all the ways the
// computer can win.
// Rock smashes scissors...
else if (compMove.equals("ROCK") &&
userMove.equals("SCISSORS")) {
winner = 1;
}
// Paper covers rock...
else if (compMove.equals("PAPER") &&
userMove.equals("ROCK")) {
winner = 1;
}
// Scissors cut paper...
else if (compMove.equals("SCISSORS") &&
userMove.equals("PAPER")) {
winner = 1;
}
// Its not a tie and the computer did not
// win so the user must have won!
else {
winner = -1;
}
return winner;
}
/**
* Display a message showing the score of the game
* and declaring the winner.
*
* @param userScore the user's score for the 10 rounds.
* @param compScore the computer's score for the 10 rounds.
*/
static void displayGameWinner(int userScore,
int compScore) {
System.out.println(" Final Score:");
System.out.println(" User=" + userScore +
" Computer=" + compScore);
System.out.println();
if (userScore > compScore) {
System.out.println("The user wins!");
}
else if (compScore > userScore) {
System.out.println("The computer wins!");
}
else {
System.out.println("Its a tie, nobody wins.");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.