import java.util.Arrays; /** * @author forbes */ public class SlotsTest { private int[] myStopIndices; private SlotMachine myMachine; public SlotsTest (SlotMachine machine) { myMachine = machine; } public void testCalculateWinnings (int[] spin, int bet, int correct) { int result = myMachine.calculateWinnings(spin, bet); if (result == correct) { System.out.print("pass"); } else { System.out.print("failed testCalculateWinnings spin:"); printArray(spin); System.out.print(" bet: " + bet); System.out.print(" expected \"" + correct + "\""); System.out.print(" got \"" + result + "\""); } System.out.println(); } // Test an individual spin given virtualReelSettings public void testSpin(int[] virtualReelSettings, int[] correct) { int[] currentSpin = myMachine.spin(virtualReelSettings); if (Arrays.equals(currentSpin, correct)) { System.out.print("pass"); } else { System.out.print("failed testSpin virtualReelSettings:"); printArray(virtualReelSettings); System.out.print(" expected "); printArray(correct); System.out.print(" got "); printArray(currentSpin); } System.out.println(); } // Report spin statistics based on a number of trials public void testSpinStatistics(int trials) { int[] virtualReels = myMachine.getVirtualReelMap(); int[] currentSpin; // TODO: keep track of spin statistics for (int i = 0; i < trials; i++) { System.out.print("Spin #" + (i + 1) + ": "); currentSpin = myMachine.spin(); printArray(currentSpin); System.out.println(); } } // print an integer array in standard form without a newline at the end private void printArray(int[] a) { System.out.print("["); for (int i = 0; i < a.length - 1; i++) System.out.print(a[i] + ", "); // handle last one (fencepost) if (a.length > 0) System.out.print(a[a.length - 1] + ""); System.out.print("]"); } public static void main(String[] args) { // Create tester SlotsTest test = new SlotsTest(new SlotMachine( new int[] { 0, 1, 1, 1, 1, 2, 4 }, new int[] { 0, 0, 2, 10 }, new int[] { 0, 1, 2, 3, 4, 5, 6 } )); // Test Winnings test.testCalculateWinnings(new int[] { 0, 1, 2 }, 1, 0); test.testCalculateWinnings(new int[] { 0, 1, 1 }, 1, 2); // TODO: add more test cases for testCalculateWinnings test.testCalculateWinnings(new int[] {0, 0, 1}, 2, 0); test.testCalculateWinnings(new int[] { 1, 1, 1 }, 1, 10); test.testCalculateWinnings(new int[] { 4, 1, 4 }, 5, 10); // Test Mappings test.testSpin(new int[] { 1, 2, 3 },new int[] { 1, 2, 3 }); // TODO: add more test cases for testSpin test.testSpin (new int[] {7, 8, 9 }, new int[] {0, 1, 2}); test.testSpin(new int[] { 1, 1, 1 }, new int[] { 1, 1, 1 }); test.testSpin(new int[] { 1, 2, 1 }, new int[] { 1, 2, 1 }); test.testSpinStatistics(100); // TODO: add more SlotsTest objects and related tests SlotsTest test2 = new SlotsTest(new SlotMachine( new int[] { 1, 2, 3, 4, 3 }, new int[] { 0, 2, 3, 4, 50}, new int[] { 3, 1, 1, 1, 2, 2 })); // Test Winnings test2.testCalculateWinnings(new int[] { 0, 1, 2, 3 }, 1, 20); test2.testCalculateWinnings(new int[] { 4, 3, 2, 1 }, 4, 96); test2.testCalculateWinnings(new int[] { 4, 4, 2, 1 }, 2, 38); // Test Mappings test2.testSpin(new int[] { 1, 2, 3, 4 }, new int[] { 1, 1, 1, 2 }); test2.testSpin(new int[] { 1, 1, 1, 1 }, new int[] { 1, 1, 1, 1 }); test2.testSpin(new int[] { 1, 3, 5, 5 }, new int[] { 1, 1, 2, 2 }); } }