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!

doesn't need to be an abstract class. actually, a normal class would be better, since I doubt you would be adding abstract methods in there.

just create a Scoring class, that contains all the methods that are equal for all of them, and have you
YathzeeScoring/MahJongScoring/...
extend that Scoring class.

It looks like you are missing some essential steps in your understanding of how inheritance of methods and variables works... Heres a very good tutorial that will teach you everything you need to know
http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html

ps:
The expression in an if test always gives a boolean so

public boolean isDone() {
  if(diceSet.size() > 0)
    return true;
  else
    return false;
}

is redundant - if the if is true then return true, if it's false return false, so what you really mean is

 public boolean isDone() {
   return diceSet.size() > 0;
 }

Edited 2 Years Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.