package test1; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * A utility class that provides methods related to 6-sided dice. * */ public class DiceUtil { private DiceUtil() { // empty by design } /** * The number of sides on a standard die. */ public static final int SIDES = 6; /** * Returns the smallest number of dice required to roll the * given value. For example, 1 die is required to roll * a value between 1 and 6, 2 dice are required to roll * a value between 7 and 12, and 3 dice are require to roll * a value between 13 and 18. * * @param value a value * @return the smallest number of dice required to roll the given value * @pre. value is greater than zero */ public static int neededToRoll(int value) { if (value % SIDES == 0) { return value / SIDES; } return value / SIDES + 1; } /** * Given a list of dice sorted in ascending order, returns the * median value of the dice in the list. The median value is * defined as the value of the die in the middle of the list. * If the list has an even number of dice, then the value of * the last die in the front half of the list is returned. * For example: * *
	 * dice                 returns
	 * ----------------------------
	 * [2, 2, 3]               2
	 * [3, 4, 5, 5]            4
	 * [1, 2, 3, 4, 4]         3
	 * [1, 1, 4, 5, 5, 6]      4
	 * 
* * @param dice a list of dice * @return the median value of the dice in the list * @throws IllegalArgumentException if the list of dice is empty */ public static int median(List dice) { int n = dice.size(); if (n == 0) { throw new IllegalArgumentException(); } if (n == 1) { return dice.get(0); } return dice.get(n / 2 - 1); } /** * Returns true if a list of dice is sorted in ascending order, * and false otherwise. Returns true if the list is empty. * This method does not modify the argument list. * Some examples: * *
	 * dice                 returns
	 * ----------------------------
	 * []                    true        
	 * [3]                   true
	 * [1, 4]                true
	 * [1, 2, 2]             true
	 * [3, 2]                false
	 * [1, 6, 4]             false
	 * [1, 2, 5, 3]          false
	 * [4, 4, 4, 4]          true
	 * 
* * @param dice a list of dice * @return true if a list of dice is sorted in ascending order, * and false otherwise */ public static boolean isSorted(List dice) { boolean sorted = true; if (dice.size() > 1) { for (int i = 1; i < dice.size(); i++) { if (dice.get(i - 1) > dice.get(i)) { sorted = false; } } } return sorted; } /** * Returns true if a list of exactly 5 dice is a full house, and * false otherwise. This method may modify the order of the values * in the argument list, but will not remove or add values to * the list. * *

* 5 dice form a full house if exactly 2 dice have the same value * (form a pair) and the remaining 3 dice have the same value * (form a three-of-a-kind) which is different than the value * of the pair. For example, the following lists are all full houses: * *

	 * [1, 1, 1, 5, 5]   two 5s and three 1s
	 * [4, 4, 4, 2, 2]   two 2s and three 4s
	 * [6, 3, 3, 6, 3]   two 6s and three 3s
	 * 
* *

* but the list: * *

	 * [2, 2, 2, 2, 2]   two 2s and three 2s???
	 * 
* *

* is not considered a full house. * * @param dice a list of dice * @return true if a list of exactly 5 dice is a full house, and * false otherwise * @pre. dice.size() == 5 */ public static boolean isFullHouse(List dice) { List t = new ArrayList<>(dice); Collections.sort(t); return (t.get(0) == t.get(1) && t.get(2) == t.get(4) && t.get(0) != t.get(2)) || (t.get(0) == t.get(2) && t.get(3) == t.get(4) && t.get(0) != t.get(3)); } }