(Solved Homework): 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

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents