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.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.testSpin( new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 }); // TODO: add more test cases for testSpin test.testSpinStatistics(100); // TODO: add more SlotsTest objects and related tests } }