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;

private static String CHOICE_NAMES[] = {“Rock”, “Paper”, “Scissors”};

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

private int choices[];

private int num_choices;

 

public Player(String name) {

setName(name);

choices = new int[100];

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

choices[j] = -1;

num_choices = 0;

}

public String getName() {

return name;

}

public void setName(String name) {

if(name.length()>=3)

this.name = name;

}

public int[] getChoices() {

return choices;

}

public void setChoices(int[] choices) {

this.choices = choices;

}

 

public void addChoice(int choice)

{

if(choice>=0 && choice<=2)

choices[num_choices++] = choice;

}

 

public int getTotalChoices()

{

return num_choices;

}

}

Game.java

package rockpaperscissor;

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

int ch1[] = player1.getChoices();

int ch2[] = 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(ch1[i] == -1 && ch2[i] == -1)

break;

if(ch1[i] != ch2[i])

{

if(ch1[i] == 0)

{

if(ch2[i] == 1)

P2WinCounts++;

else if(ch2[i] == 2)

P1WinCounts++;

}

else if(ch1[i] == 1)

{

if(ch2[i] == 0)

P1WinCounts++;

else if(ch2[i] == 2)

P2WinCounts++;

}

else if(ch1[i] == 2)

{

if(ch2[i] == 0)

P2WinCounts++;

else if(ch2[i] == 1)

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 {

 

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

public static void main(String args[])

{

Player player1 = new Player(“Kaju”);

Player player2 = new Player(“Arif”);

player1.addChoice(0);

player1.addChoice(1);

player1.addChoice(2);

player1.addChoice(0);

player1.addChoice(2);

 

player2.addChoice(1);

player2.addChoice(1);

player2.addChoice(2);

player2.addChoice(2);

player2.addChoice(1);

 

Game game1 = new Game(player1, player2);

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

 

player1.addChoice(2);

player1.addChoice(1);

player1.addChoice(0);

 

player2.addChoice(0);

player2.addChoice(2);

player2.addChoice(2);

 

Game game2 = new Game(player1, player2);

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

}

}

OUTPUT:

Winner: Kaju

Winner: Both. It’s a tie

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