package lab.games; import static org.junit.Assert.*; import java.lang.reflect.Field; import java.lang.reflect.Method; import org.junit.Test; public class ValueTest { @Test public void testPlayerWinsPlayersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.PLAYER); model.drop(0, Model.PLAYER); model.drop(0, Model.PLAYER); model.drop(0, Model.PLAYER); assertEquals(-70, ((Integer) method.invoke(model, board, 1, Model.PLAYER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testPlayerWinsComputersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.PLAYER); model.drop(0, Model.PLAYER); model.drop(0, Model.PLAYER); model.drop(0, Model.PLAYER); assertEquals(-70, ((Integer) method.invoke(model, board, 1, Model.COMPUTER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testComputerWinsPlayersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.COMPUTER); model.drop(0, Model.COMPUTER); model.drop(0, Model.COMPUTER); model.drop(0, Model.COMPUTER); assertEquals(70, ((Integer) method.invoke(model, board, 1, Model.PLAYER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testComputerWinsComputersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.COMPUTER); model.drop(0, Model.COMPUTER); model.drop(0, Model.COMPUTER); model.drop(0, Model.COMPUTER); assertEquals(70, ((Integer) method.invoke(model, board, 1, Model.COMPUTER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testEmptyBoardComputersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); assertEquals(0, ((Integer) method.invoke(model, board, 0, Model.COMPUTER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testEmptyBoardPlayersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); assertEquals(0, ((Integer) method.invoke(model, board, 0, Model.PLAYER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testOneCoinPlayersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.COMPUTER); assertEquals(-4, ((Integer) method.invoke(model, board, 1, Model.PLAYER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testOneCoinComputersTurn() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.COMPUTER); assertEquals(9, ((Integer) method.invoke(model, board, 1, Model.COMPUTER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testOneCoinPlayersTurnTwo() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.COMPUTER); assertEquals(5, ((Integer) method.invoke(model, board, 2, Model.PLAYER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void testOneCoinComputersTurnTwo() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.COMPUTER); assertEquals(0, ((Integer) method.invoke(model, board, 2, Model.COMPUTER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } @Test public void almostFilled() { try { Model model = new Model(0); Class clazz = model.getClass(); Field field = clazz.getDeclaredField("board"); field.setAccessible(true); int[][] board = (int[][]) field.get(model); Method method = clazz.getDeclaredMethod("value", board.getClass(), int.class, int.class); method.setAccessible(true); model.drop(0, Model.PLAYER); model.drop(0, Model.PLAYER); model.drop(0, Model.COMPUTER); model.drop(0, Model.PLAYER); model.drop(0, Model.COMPUTER); model.drop(0, Model.COMPUTER); model.drop(1, Model.COMPUTER); model.drop(1, Model.COMPUTER); model.drop(1, Model.COMPUTER); model.drop(1, Model.PLAYER); model.drop(1, Model.PLAYER); model.drop(1, Model.PLAYER); model.drop(2, Model.PLAYER); model.drop(2, Model.COMPUTER); model.drop(2, Model.PLAYER); model.drop(2, Model.COMPUTER); model.drop(2, Model.PLAYER); model.drop(2, Model.COMPUTER); model.drop(3, Model.COMPUTER); model.drop(3, Model.PLAYER); model.drop(3, Model.COMPUTER); model.drop(3, Model.PLAYER); model.drop(3, Model.COMPUTER); model.drop(3, Model.PLAYER); model.drop(4, Model.COMPUTER); model.drop(4, Model.PLAYER); model.drop(4, Model.COMPUTER); model.drop(4, Model.PLAYER); model.drop(4, Model.COMPUTER); model.drop(4, Model.PLAYER); model.drop(5, Model.PLAYER); model.drop(5, Model.PLAYER); model.drop(5, Model.PLAYER); model.drop(5, Model.COMPUTER); model.drop(5, Model.COMPUTER); model.drop(5, Model.COMPUTER); model.drop(6, Model.PLAYER); model.drop(6, Model.COMPUTER); model.drop(6, Model.PLAYER); model.drop(6, Model.COMPUTER); model.drop(6, Model.PLAYER); assertEquals(0, ((Integer) method.invoke(model, board, 2, Model.COMPUTER)).intValue()); } catch (Exception e) { e.printStackTrace(); fail("Unexpected exception"); } } }