import java.util.Collections; import java.util.ArrayList; import java.util.List; /** * A utility class that encodes a subset of the rules for * the game Yahtzee. * *

A description of the scoring categories can be * found on the * Yahtzee Wikipedia web page. * * @author EECS1011E_W15 * */ public class Yahtzee { private Yahtzee() { // private and empty by design throw new AssertionError(); } /** * The number of six-sided dice used in a standard game of Yahtzee. */ public static final int NUMBER_OF_DICE = 5; /** * Returns true if the list contains a three of a kind. * *

A three of a kind is defined as at least three dice having * the same value. * * @param dice list of dice * @pre. dice.size() == Yahtzee.NUMBER_OF_DICE * @return true if the list contains a three of a kind */ public static boolean isThreeOfAKind(List dice) { // implementation not shown if (dice.size() != Yahtzee.NUMBER_OF_DICE) { throw new IllegalArgumentException("wrong number of dice: " + dice.size()); } List copy = new ArrayList(dice); Collections.sort(copy); boolean result = copy.get(0).getValue() == copy.get(2).getValue() || copy.get(1).getValue() == copy.get(3).getValue() || copy.get(2).getValue() == copy.get(4).getValue(); return result; } }