Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA YAHTZEE PROGRAM: How do I add the following (left column) to the existing code (right column)? Having a hard time understanding some of the

JAVA YAHTZEE PROGRAM: How do I add the following (left column) to the existing code (right column)?

Having a hard time understanding some of the wording and getting my program to run without any errors. Thank you!

AiPlayer class

Questions to answer:

Update method selectCategory() to do the following

Add parameter of class Roll based on updated IPlayer interface

Comment out or delete the statement that throws the UnsupportedOperationException

Implements abstract method selectDice() from superclass Player, remove any autogenerated body

Implements abstract method calculateScore() from superclass Player, remove any autogenerated body

Exisiting Code:

package Core;

import java.util.ArrayList;

public class AiPlayer extends Player {

@Override

public void rollDice(Roll r){

for(Die die: r.getDice()){

die.rollDie();

}}

@Override

public void selectCategory() {

throw new UnsupportedOperationException("Not supported yet!");}}

Constants class

Update the class to include two constants as follows

ZERO set equal to the value of 0

MAX_ROLLS set equal to the value of 3

package constants;

public class Constants {

public final static int MAX_YAHTZEE = 4;

public final static int NUM_DICE = 5;

public final static int MAX_DIE_VALUE = 6;

public final static int ONES = 1;

public final static int TWOS = 2;

public final static int THREES = 3;

public final static int FOURS = 4;

public final static int FIVES = 5;

public final static int SIXES = 6;

public final static int THREE_KIND = 7;

public final static int FOUR_KIND = 8;

public final static int CHANCE = 9;

public final static int NUM_CATERGORY = 13;

public final static int FULL_HOUSE = 25;

public final static int SM_STRAIGHT = 30;

public final static int UPPER_BONUS = 35;

public final static int LG_STRAIGHT = 40;

public final static int YAHTZEE = 50;

public final static int YAHTZEE_BONUS = 100;

}

Die class

package core;

import java.util.Random;

import constants.Constants;

public class Die {

private int faceValue;

public int getFaceValue() {

return faceValue;

}

public void setFaceValue(int faceValue) {

this.faceValue = faceValue;

}

public void rollDie()

{

Random rand = new Random();

faceValue = (rand.nextInt(Constants.MAX_DIE_VALUE) + 1);

}

@Override

public String toString()

{

return Integer.toString(getFaceValue());

}

}

Game class

Update method playGame() to do the following

Create a local variable of data type int called roll initialized to the value of 0

In the for loop that loops through the players of the gamePrior to the call to player.rollDice(dice)

instantiate an instance of class Roll that will store the selected dice of the player for each roll

for the reference object described in the previous step, call method removeDice() passing as an argument the reference object to empty the ArrayList of class Die

Add a while loop based on the condition that the selected dice ArrayList size is less than the maximum number of dice AND the player has not exceeded the maximum number of rolls

Inside the while loop should be the following

The call to player.rollDice(dice)

Call to method selectDice() on class Player passing as arguments

The original Roll object reference

The Roll object for the dice to keep

The int for the current roll number

Increment the local variable roll of data type int

After the while loop, call method selectCategory() in class Player passing as an argument the reference object of class Roll that represents the kept dice by the player

package core;

import java.util.Scanner;

import java.util.ArrayList;

public class Game {private int gameTurn; private ArrayList players;

private Roll dice; public int getGameTurn() {return gameTurn;}

public void setGameTurn(int gameTurn) {this.gameTurn = gameTurn;}public ArrayList getPlayers() {return players;}

public void setPlayers(ArrayList players) {this.players = players;}

public Roll getDice(){return dice;}public void setDice(Roll dice){

this.dice = dice;}public Game(){createPlayers(); displayPlayers();

playGame();}private void createPlayers() {players = new ArrayList();

Scanner scan = new Scanner(System.in);

System.out.println("Enter human player name: ");

String name = scan.next(); HumanPlayer hp = new HumanPlayer();

hp.setName(name); AiPlayer aip = new AiPlayer();

aip.setName("Ai Player"); players.add(hp); players.add(aip);}

private void displayPlayers(){

System.out.println("****************************");

System.out.println("Players for this game are: ");

System.out.println("****************************");

for(Player player : players){System.out.println(player.getName());}}private void playGame(){dice = new Roll();for(Player player : players)

{System.out.println("*************************************"); System.out.println("Player " +player.getName() + " passed the dice");

System.out.println("Player " +player.getName() + " rolling the dice");

player.rollDice(dice);System.out.println("Displaying the dice values: ");dice.displayDice();

System.out.println("*************************************");} }}

HumanPlayer class

Add a custom constructor that does the following

Access level modifier public

Empty parameter list

Instantiates the member variable of class ScoreCard

Update method selectCategory() to do the following

Add parameter of class Roll based on updated IPlayer interface representing the dice the player selected to keep

Prompt the player to select a category

Using an instance of class Scanner, receive the players selection

Loop until the player selects a valid category, data validation must check for

Using try/catch exception handling verify the player entered an integer

Verify the player entered a valid value based on the 13 categories to select from

After the player selects a valid category call method calculateScore() passing as arguments

the instance of class roll received as a parameter

the int of the category selected

Add method calculateScore to do the following

Access level modifier public

Return type void

Parameter list

Instance of class Roll representing the dice the player selected

Primitive data type int representing the selected category

If the category selected is part of the upper section, referencing the member variable of class ScoreCard call the getter for UpperSection and call method evaluateCategory() passing as arguments the instance of class Roll representing the kept dice and the category selected

If the category selected is part of the upper section, referencing the member variable of class ScoreCard call the getter for LowerSection and call method evaluateCategory() passing as arguments the instance of class Roll representing the kept dice and the category selected

Output to the console the total score from class ScoreCard

Add method selectDice() to do the following

Access level modifier public

No return type

Parameter list

Instance of class Roll representing the member variable instantiated in class Game

Instance of class Roll representing the object instantiated for the dice to keep

Primitive data type int representing the current roll number of the player

Prompt the player to select the dice they want to keep based on the index value displayed in front of the die value, offer an option to end the current turn of their roll (note: each player gets three rolls per turn)

Loop until the player enters that they are done selecting and their selection was validIf the player selects that they are doneCheck if all dice have been selected AND they are on their third rollIf true

transfer all remaining dice in the original Roll instance to the keep Roll instance

remove each die from the original Roll instance

Update the variable monitoring if the player is done by setting it to true

Break out of the looping structure

ElseInside a try/catch exception handler

Parse the received data from the player so it is an integer representing the index to the ArrayList of Die

Get the Die instance from the ArrayList and store as an instance of class Die

Add the Die to the instance of class Roll representing the keep dice

Remove the Die from the instance of class Roll representing the original dice

Loop through the collection of Die to keep in the instance of class Roll that represents the keep dice and output to the console

If the size of the ArrayList of Die in class Roll is the maximum number of dice, break out of the looping structure

package core;

public class HumanPlayer extends Player {

@Override

public void rollDice(Roll r)

{

for(Die die : r.getDice())

{

die.rollDie();

}

}

@Override

public void selectCategory()

{

throw new UnsupportedOperationException("Not supported yet!");

}

}

IPlayer interface

Update the method signature for method selectCategory() so that it has one parameter of class Roll

package core; public interface IPlayer {public void rollDice(Roll r);public void selectCategory();

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

What is the purpose of the Salary Structure Table?

Answered: 1 week ago

Question

What is the scope and use of a Job Family Table?

Answered: 1 week ago