The assignment is program a yahtzee game in java following a template, but after
ID: 668466 • Letter: T
Question
The assignment is program a yahtzee game in java following a template, but after a few days of working on it, i still can't get it to work properly. A solution to this would be helpful, so that i can compare to my own and see where I am going wrong.
Yahtzee class
Instantiate an instance of class Game
Call method displayPlayers() in class Game
Call method playGame() in class Game
core package
Create package core
Create class Die
Create class Game
Create class LowerSection
Create class Player
Create class Roll
Create class ScoreCard
Create class UpperSection
Die class
faceValue, data type int
Create getter/setter for member variable
void return type
empty parameter list
Instantiate an instance of class Random
Set the value of member variable faceValue to a random number between 1 and 6
Override class Object’s method toString() that returns a String value of member variable faceValue
Game class
gameTurn, data type int
players, datatype ArrayList<Player>
input, data type Scanner
Create getter/setter for member variables
Prompt the user for number of players
Read in data entered by user
Loop through number of players and call method newPlayer()
void return type
empty parameter list
prompt the user for the player’s name
read in the data entered by the user
instantiate and instance of class Player
call the setter method for member variable name passing the user’s input as an argument
add the instance of Player to the players member variable
void return type
empty parameter list
loop through the member variable players and display to the console the result of calling method getName()
void return type
empty parameter list
loop through the member variable players and call method rollDice()
LowerSection class
threeKind, data type int
fourKind, data type int
fullHouse, data type int
smStraight, data type int
lgStraight, data type int
yahtzee, data type int
chance, data type int
yahtzeeBonus, data type int
totalScore, data type int
Create getter/setter for member variables
Player class
name, data type String
score, data type ScoreCard
roll, data type Roll
Create getter/setter for member variables
Instantiate an instance of the declared member variable roll
void return type
empty parameter list
call method rollDice() of member variable roll
Roll class
dice, data type ArrayList<Die>
Create getter/setter for member variables
Call method initializeDice()
void return type
empty parameter list
instantiate and instance of class Die
add the instance of Die to member variable dice
void return type
empty parameter list
call method rollDie() in class Die
Display to the console the result of method call toString() in class Die
ScoreCard class
upper, data type UppserSection
lower, data type LowerSection
grandTotal, data type int
Create getter/setter for member variables
UpperSection class
aces, data type int
twos, data type int
threes, data type int
fours, data type int
fives, data type int
sixes, data type int
totalScore, data type int
bonus, data type int
total, data type int
Create getter/setter for member variables
Yahtzee class
Instantiate an instance of class Game
Call method displayPlayers() in class Game
Call method playGame() in class Game
core package
Create package core
Create class Die
Create class Game
Create class LowerSection
Create class Player
Create class Roll
Create class ScoreCard
Create class UpperSection
Die class
Add member variablesfaceValue, data type int
Create getter/setter for member variable
Define method rollDie(); it should do the following:void return type
empty parameter list
Instantiate an instance of class Random
Set the value of member variable faceValue to a random number between 1 and 6
Override class Object’s method toString() that returns a String value of member variable faceValue
Game class
Add member variables:gameTurn, data type int
players, datatype ArrayList<Player>
input, data type Scanner
Create getter/setter for member variables
Override the no-argument constructor Game, it should do the following:Prompt the user for number of players
Read in data entered by user
Loop through number of players and call method newPlayer()
Define method newPlayer(); it should do the following:void return type
empty parameter list
prompt the user for the player’s name
read in the data entered by the user
instantiate and instance of class Player
call the setter method for member variable name passing the user’s input as an argument
add the instance of Player to the players member variable
Define method displayPlayers(); it should do the following:void return type
empty parameter list
loop through the member variable players and display to the console the result of calling method getName()
Define method playGame(); it should do the following:void return type
empty parameter list
loop through the member variable players and call method rollDice()
LowerSection class
Add member variablesthreeKind, data type int
fourKind, data type int
fullHouse, data type int
smStraight, data type int
lgStraight, data type int
yahtzee, data type int
chance, data type int
yahtzeeBonus, data type int
totalScore, data type int
Create getter/setter for member variables
Player class
Add member variablesname, data type String
score, data type ScoreCard
roll, data type Roll
Create getter/setter for member variables
Override no-argument constructor; it should do the following:Instantiate an instance of the declared member variable roll
Define method rollDice(); it should do the following:void return type
empty parameter list
call method rollDice() of member variable roll
Roll class
Add member variablesdice, data type ArrayList<Die>
Create getter/setter for member variables
Override no-argument constructor; it should do the following:Call method initializeDice()
Define method initializeDice(); it should do the following:void return type
empty parameter list
loop five times toinstantiate and instance of class Die
add the instance of Die to member variable dice
Define method rollDice(); it should do the followingvoid return type
empty parameter list
Loop through member variable dicecall method rollDie() in class Die
Display to the console the result of method call toString() in class Die
ScoreCard class
Add member variablesupper, data type UppserSection
lower, data type LowerSection
grandTotal, data type int
Create getter/setter for member variables
UpperSection class
Add member variablesaces, data type int
twos, data type int
threes, data type int
fours, data type int
fives, data type int
sixes, data type int
totalScore, data type int
bonus, data type int
total, data type int
Create getter/setter for member variables
Explanation / Answer
Dice.java
import java.util.Observable;
import java.util.Random;
public class Dice extends Observable {
/**
* The value.
*/
private int value;
/**
* Gets the value.
* @return The value.
*/
public int getValue() {
return value;
}
/**
* Determines whether the dice is locked.
*/
private boolean locked;
/**
* Determines whether the dice is locked.
* @return The locked state.
*/
public boolean isLocked() {
return locked;
}
/**
* Locks the dice.
* @param value The locked state.
*/
public void setLocked(boolean value) {
locked = value;
notifyObservers();
}
/**
* Constructor.
*/
public Dice() {
roll();
}
/**
* Rolls the dice if it is not locked.
*/
public void roll() {
if (!isLocked()) {
value = new Random().nextInt(6) + 1;
notifyObservers();
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasChanged() {
return true;
}
}
LowerSection.java
public class LowerSection extends Section {
/**
* Constructor
* @param resources The resources.
*/
public LowerSection(Resources resources) {
add(new Number(resources.getText
(R.string.one).toString(), 1));
add(new Number(resources.getText
(R.string.two).toString(), 2));
add(new Number(resources.getText
(R.string.three).toString(), 3));
add(new Number(resources.getText
(R.string.four).toString(), 4));
add(new Number(resources.getText
(R.string.five).toString(), 5));
add(new Number(resources.getText
(R.string.six).toString(), 6));
}
/**
* {@inheritDoc}
*/
@Override
public int getBonus() {
int sum = 0;
for (Element element : getElements()) {
if (element.hasResult()) {
sum += element.getResult();
}
}
if (sum >= 63) {
return 35;
}
return 0;
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasBonus() {
return true;
}
}
Player.java
public class Player {
/**
* The name.
*/
private String name;
/**
* The {@link LowerSection}.
*/
private Section lowerSection;
/**
* The {@link UpperSection}.
*/
private Section upperSection;
/**
* Constructor.
* @param name The name.
* @param resources The resources.
*/
public Player(String name, Resources resources) {
this.name = name;
lowerSection = new LowerSection(resources);
upperSection = new UpperSection(resources);
}
/**
* Gets the name.
* @return The name.
*/
public String getName() {
return name;
}
/**
* Gets the {@link LowerSection}.
* @return The {@link LowerSection}.
*/
public Section getLowerSection() {
return lowerSection;
}
/**
* Gets the {@link UpperSection}.
* @return The {@link UpperSection}.
*/
public Section getUpperSection() {
return upperSection;
}
/**
* Get the current sum of the player.
* @return The current sum of the player.
*/
public int getResult() {
return lowerSection.getResult() +
upperSection.getResult();
}
}
Section.java
import java.util.ArrayList;
import java.util.List;
public abstract class Section {
/**
* The elements.
*/
private List<Element> elements = new
ArrayList<Element>();
/**
* Adds the given element.
* @param element The element.
*/
protected void add(Element element) {
elements.add(element);
}
/**
* Gets the elements.
* @return The elements.
*/
public List<Element> getElements() {
return elements;
}
/**
* Gets the sum of the section.
* @return The sum.
*/
public int getResult(){
int sum = 0;
for (Element element : getElements()) {
if (element.hasResult()) {
sum += element.getResult();
}
}
sum += getBonus();
return sum;
}
/**
* Gets the bonus of the section.
* @return The bonus.
*/
public abstract int getBonus();
/**
* Determines whether the section has a bonus.
* @return The bonus state.
*/
public abstract boolean hasBonus();
}
GameContext.java
import java.util.List;
/**
* The context containing all states required for the game.
* @author Drew Stauft
*
*/
public class GameContext {
/**
* The players.
*/
public List<Player> players;
/**
* The marked/selected element.
*/
public Element markedElement;
/**
* The dices.
*/
public Dice[] dices = new Dice[] { new Dice(), new Dice(), new Dice(), new Dice(), new Dice() };
/**
* The number of dice rolls of the current player (1-3)
*/
public int rollCount = 1;
/**
* The current step/round in the game (1-13).
*/
public int currentStep = 1;
/**
* The index of the current player.
*/
public int currentPlayerIndex = 0;
/**
* Constructor.
* @param players The players.
*/
public GameContext(List<Player> players) {
this.players = players;
}
/**
* Gets the current player.
* @return The current player.
*/
public Player getCurrentPlayer() {
return players.get(currentPlayerIndex);
}
/**
* Gets the number of players.
* @return The number of players.
*/
public int getNumberOfPlayers() {
return players.size();
}
}
UpperSection.java
public class UpperSection extends Section {
/**
* Constructor.
* @param resources The resources.
*/
public UpperSection(Resources resources) {
add(new OneKind(resources.getText(R.string.three_of_a_kind).toString(), 3));
add(new OneKind(resources.getText(R.string.four_of_a_kind).toString(), 4));
add(new FullHouse(resources.getText(R.string.full_house).toString()));
add(new SmallStraight(resources.getText(R.string.small_straight).toString()));
add(new LargeStraight(resources.getText(R.string.large_straight).toString()));
add(new Yahtzee(resources.getText(R.string.yahtzee).toString()));
add(new Chance(resources.getText(R.string.chance).toString()));
}
/**
* {@inheritDoc}
*/
//@Override
public int getBonus() {
return 0;
}
/**
* {@inheritDoc}
*/
//@Override
public boolean hasBonus() {
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.