Answered! Rock paper scissors class design problem using JAVA. Below I posted what needs to be done(2 classes and 1 driver)…

Rock paper scissors class design problem using JAVA.

Below I posted what needs to be done(2 classes and 1 driver)

PLEASE FOLLOW ALL THE DIRECTIONS. NO ARRAYLIST, NO IMPORTS(ONLY SCANNER IS OKAY)

KEEP EVERYTHING AS SIMPLE AS YOU CAN. THIS IS AN INTRO TO COMPUTER SCIENCE CLASS

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

Expert Answer

 PROGRAM CODE:

Player.java

package rockpaperscissor;

public class Player {

 

 

private String name;

public static enum CHOICES{ROCK, PAPER, SCISSORS};

private CHOICES choices[];

private int num_choices;

 

public Player(String name) {

setName(name);

choices = new CHOICES[100];

for(int j=0; j<100; j++)

choices[j] = null;

num_choices = 0;

}

public String getName() {

return name;

}

public void setName(String name) {

if(name.length()>=3)

this.name = name;

}

public CHOICES[] getChoices() {

return choices;

}

public void setChoices(CHOICES[] choices) {

this.choices = choices;

}

 

public void addChoice(CHOICES choice)

{

choices[num_choices++] = choice;

}

 

public int getTotalChoices()

{

return num_choices;

}

}

Game.java

package rockpaperscissor;

import rockpaperscissor.Player.CHOICES;

public class Game {

 

private Player player1;

private Player player2;

 

public Game(Player p1, Player p2) {

this.player1 = p1;

this.player2 = p2;

}

 

public String getWinner()

{

//both players will have equal length

CHOICES player1Choices[] = player1.getChoices();

CHOICES player2Choices[] = player2.getChoices();

int P1WinCounts = 0, P2WinCounts = 0;

 

//choices are interpreted as 0 – Rock, 1 – Paper, 2 – Scissors

for(int i=0; i<100; i++)

{

if(player1Choices[i] == null && player2Choices[i] == null)

break;

if(player1Choices[i] != player2Choices[i])

{

if(player1Choices[i] == Player.CHOICES.ROCK)

{

if(player2Choices[i] == Player.CHOICES.PAPER)

P2WinCounts++;

else if(player2Choices[i] == Player.CHOICES.SCISSORS)

P1WinCounts++;

}

else if(player1Choices[i] == Player.CHOICES.PAPER)

{

if(player2Choices[i] == Player.CHOICES.ROCK)

P1WinCounts++;

else if(player2Choices[i] == Player.CHOICES.SCISSORS)

P2WinCounts++;

}

else if(player1Choices[i] == Player.CHOICES.SCISSORS)

{

if(player2Choices[i] == Player.CHOICES.ROCK)

P2WinCounts++;

else if(player2Choices[i] == Player.CHOICES.PAPER)

P1WinCounts++;

}

}

 

}

 

if(P1WinCounts > P2WinCounts)

return player1.getName();

else if(P1WinCounts < P2WinCounts)

return player2.getName();

else

{

if(player1.getTotalChoices() < player2.getTotalChoices())

return player2.getName();

else if(player1.getTotalChoices() > player2.getTotalChoices())

return player1.getName();

else return “Both. It’s a tie”;

}

}

}

GameDriver.java

package rockpaperscissor;

public class GameDriver {

 

public static void main(String args[])

{

Player player1 = new Player(“Kaju”);

Player player2 = new Player(“Arif”);

player1.addChoice(Player.CHOICES.SCISSORS);

player1.addChoice(Player.CHOICES.ROCK);

player1.addChoice(Player.CHOICES.SCISSORS);

player1.addChoice(Player.CHOICES.SCISSORS);

 

player2.addChoice(Player.CHOICES.PAPER);

player2.addChoice(Player.CHOICES.SCISSORS);

player2.addChoice(Player.CHOICES.ROCK);

player2.addChoice(Player.CHOICES.PAPER);

 

Game game1 = new Game(player1, player2);

System.out.println(“Winner of this game: ” + game1.getWinner());

 

player1.addChoice(Player.CHOICES.ROCK);

player1.addChoice(Player.CHOICES.SCISSORS);

 

player2.addChoice(Player.CHOICES.PAPER);

player2.addChoice(Player.CHOICES.ROCK);

 

Game game2 = new Game(player1, player2);

System.out.println(“Winner of this game: ” + game2.getWinner());

}

}

OUTPUT:

Winner of this game: Kaju

Winner of this game: Both. It’s a tie

Still stressed from student homework?
Get quality assistance from academic writers!