package cse1030; import static org.junit.Assert.*; import org.junit.Test; import java.util.Collections; import java.util.List; import java.util.ArrayList; public class YahtzeeTest { private static final int TRIALS = 10; @Test public void testIsThreeOfAKind() { List dice = null; List copy = null; for (int i = 0; i < TRIALS; i++) { dice = maxNOfAKind(2); copy = new ArrayList(dice); assertFalse(toString(dice), Yahtzee.isThreeOfAKind(dice)); assertEquals("list of dice has changed!", dice, copy); } for (int i = 0; i < TRIALS; i++) { dice = minNOfAKind(3); assertTrue(toString(dice), Yahtzee.isThreeOfAKind(dice)); } } @Test public void testIsFourOfAKind() { List dice = null; List copy = null; for (int i = 0; i < TRIALS; i++) { dice = maxNOfAKind(3); copy = new ArrayList(dice); assertFalse(toString(dice), Yahtzee.isFourOfAKind(dice)); assertEquals("list of dice has changed!", dice, copy); } for (int i = 0; i < TRIALS; i++) { dice = minNOfAKind(4); assertTrue(toString(dice), Yahtzee.isFourOfAKind(dice)); } } @Test public void testIsFiveOfAKind() { List dice = null; List copy = null; for (int i = 0; i < TRIALS; i++) { dice = maxNOfAKind(4); copy = new ArrayList(dice); assertFalse(toString(dice), Yahtzee.isFiveOfAKind(dice)); assertEquals("list of dice has changed!", dice, copy); } for (int i = 0; i < TRIALS; i++) { dice = minNOfAKind(5); assertTrue(toString(dice), Yahtzee.isFiveOfAKind(dice)); } } @Test public void testIsFullHouse() { List dice = null; List copy = null; for (int i = 0; i < TRIALS; i++) { dice = maxNOfAKind(2); copy = new ArrayList(dice); assertFalse(toString(dice), Yahtzee.isFullHouse(dice)); assertEquals("list of dice has changed!", dice, copy); } for (int i = 0; i < TRIALS; i++) { dice = fullHouse(); assertTrue(toString(dice), Yahtzee.isFullHouse(dice)); } } @Test public void testIsSmallStraight() { List dice = null; List copy = null; for (int i = 0; i < TRIALS; i++) { dice = notStraight(); copy = new ArrayList(dice); assertFalse(toString(dice), Yahtzee.isSmallStraight(dice)); assertEquals("list of dice has changed!", dice, copy); } for (int i = 0; i < TRIALS; i++) { dice = straight(4); assertTrue(toString(dice), Yahtzee.isSmallStraight(dice)); } } @Test public void testIsLargeStraight() { List dice = null; List copy = null; for (int i = 0; i < TRIALS; i++) { dice = notStraight(); copy = new ArrayList(dice); assertFalse(toString(dice), Yahtzee.isSmallStraight(dice)); assertEquals("list of dice has changed!", dice, copy); } for (int i = 0; i < TRIALS; i++) { dice = straight(5); assertTrue(toString(dice), Yahtzee.isSmallStraight(dice)); } } private static List minNOfAKind(int n) { Die repeat = new Die(); List dice = new ArrayList(5); for (int i = 0; i < n; i++) { dice.add(new Die(repeat)); } for (int i = n; i < 5; i++) { Die d = new Die(); dice.add(d); } Collections.shuffle(dice); return dice; } private static List maxNOfAKind(int n) { final int N = n * 6; List dice = new ArrayList(N); for (int i = 0; i < n; i++) { for (int j = 1; j <= 6; j++) { Die d = new Die(); d.setValue(j); dice.add(d); } } Collections.shuffle(dice); return dice.subList(0, 5); } private static List fullHouse() { Die x = new Die(); Die y = new Die(); List dice = new ArrayList(5); dice.add(new Die(x)); dice.add(new Die(x)); dice.add(new Die(y)); dice.add(new Die(y)); dice.add(new Die(y)); Collections.shuffle(dice); return dice; } private static List straight(int length) { final int MIN = 6 - length + 1; Die start = new Die(MIN); List dice = new ArrayList(5); dice.add(start); for (int i = 1; i < length; i++) { dice.add(new Die(6, start.getValue() + i)); } for (int i = length; i < 5; i++) { dice.add(new Die()); } Collections.shuffle(dice); return dice; } private static List notStraight() { List dice = new ArrayList(5); int count = 0; while (count < 5) { Die d = new Die(); if (d.getValue() != 3) { dice.add(d); count++; } } return dice; } private static String toString(List dice) { StringBuilder b = new StringBuilder(); b.append('['); for (Die d : dice) { b.append(d.getValue()); b.append(", "); } b.append(']'); return b.toString(); } }