package hw3.categories;
import java.util.ArrayList;
import hw3.DiceGroup;
import hw3.api.IScoreCategory;
/**
* Scoring category for a yahtzee. A DiceGroup
* with N dice satisfies this category only if all N
* values are the same. For a dice group that satisfies
* this category, the score is a fixed value specified in the constructor;
* otherwise, the score is zero.
*/
//Note: You must edit this declaration to implement IScoreCategory
//or to extend some other class that implements IScoreCategory
public class YahtzeeCategory implements IScoreCategory
{
private String category;
private int point;
private ArrayList<Integer> diceSet;
private DiceGroup dGroup;
/**
* Constructs a YahtzeeCategory with the given display name
* and score.
* @param displayName
* name of this category
* @param points
* points awarded for a dice group that satisfies this category
*/
public YahtzeeCategory(String displayName, int points)
{
category = displayName;
point = points;
diceSet = new ArrayList<Integer>();
}
@Override
public boolean isDone() {
if(diceSet.size() > 0)
return true;
else
return false;
}
@Override
public int getScore() {
if(isDone())
return getPotentialScore(dGroup);
else
return 0;
}
@Override
public DiceGroup getDice() {
return dGroup;
}
@Override
public String getDisplayName() {
return category;
}
@Override
public void setDone(DiceGroup group) {
dGroup = group;
for(int i = 0; i < group.getAll().length; i++)
diceSet.add(group.getAll()[i]);
}
@Override
public boolean isSatisfiedBy(DiceGroup group) {
int counter = 0;
for(int i = 0; i < group.getAll().length-1; i++){
if(group.getAll()[i] == group.getAll()[i+1])
counter++;
}
if(counter == group.getAll().length-1)
return true;
else
return false;
}
@Override
public int getPotentialScore(DiceGroup group) {
if(isSatisfiedBy(group))
return point;
else
return 0;
}
}
This is, obviously a scoring category for a yahtzee game. This one specifically handles the score someone would get should the achieve a yahtzee. There are 7 other scoring category classes that are very similar. In fact! isDone(), getScore(), getDice(), getDisplayName(), and setDone() are the same for each of the seven, exactly the same, and each category has the exact same instance variables...
So I should be able to construct an abstract super class yes? But I don't know how to go about utilizing it.
Do I put exactly what I have for my methods into the super class methods, then in the categories call the superclass? Also do I get rid of instance variables for each category and only put them in the superclass? oor keep them in in superclass and categories or just in categories lol?
thanks for any help!