diff --git a/test/WinCheckerTests.java b/test/WinCheckerTests.java index 5d73d9b..524f29e 100644 --- a/test/WinCheckerTests.java +++ b/test/WinCheckerTests.java @@ -12,9 +12,59 @@ import java.util.Random; import java.util.stream.Stream; public class WinCheckerTests { - private static Random rng = new Random(); + private final static Random rng = new Random(); public static Stream isGameOverTestData() { + Board randomBoard = new Board(); + Player currentBox; + for (int i = 1; i <= 3; ++i) + for (int j = 1; j <= 3; ++j) { + int rand = rng.nextInt(3); + Player p = switch (rand){ + case 1 -> Player.X; + case 2 -> Player.O; + default -> null; + }; + randomBoard.setBox(i, j, p); + } + + boolean expected = false; + + currentBox = randomBoard.getBox(1, 1); + if(currentBox != null){ + if (currentBox == randomBoard.getBox(1, 2) && currentBox == randomBoard.getBox(1, 3)) + expected = true; + if (currentBox == randomBoard.getBox(2, 2) && currentBox == randomBoard.getBox(3, 3)) + expected = true; + if (currentBox == randomBoard.getBox(2, 1) && currentBox == randomBoard.getBox(3, 1)) + expected = true; + } + + currentBox = randomBoard.getBox(2, 1); + if(currentBox != null && currentBox == randomBoard.getBox(2, 2) && currentBox == randomBoard.getBox(2, 3)){ + expected = true; + } + + currentBox = randomBoard.getBox(3, 1); + if(currentBox != null){ + if (currentBox == randomBoard.getBox(1, 3) && currentBox == randomBoard.getBox(2, 2)) { + expected = true; + } + if (currentBox == randomBoard.getBox(3, 2) && currentBox == randomBoard.getBox(3, 3)) { + expected = true; + } + } + + currentBox = randomBoard.getBox(1, 2); + if(currentBox != null && currentBox == randomBoard.getBox(2, 2) && currentBox == randomBoard.getBox(3, 2)){ + expected = true; + } + + currentBox = randomBoard.getBox(1, 3); + if(currentBox != null && currentBox == randomBoard.getBox(2, 3) && currentBox == randomBoard.getBox(3, 3)){ + expected = true; + } + return Stream.of( Arguments.of(new Board(), false), Arguments.of(topRowXWins(), true), @@ -25,7 +75,8 @@ public class WinCheckerTests { Arguments.of(rightColumn(), true), Arguments.of(diagUp(), true), Arguments.of(diagDown(), true), - Arguments.of(stalemate(), true) + Arguments.of(stalemate(), true), + Arguments.of(randomBoard, expected) ); } @@ -152,6 +203,7 @@ public class WinCheckerTests { Board b = new Board(); Player expected = null; boolean shouldThrow = false; + for (int i = 1; i <= 3; ++i) for (int j = 1; j <= 3; ++j) { int rand = rng.nextInt(3); @@ -165,7 +217,45 @@ public class WinCheckerTests { currentBox = b.getBox(1, 1); if(currentBox != null){ - //if (currentBox == b.getBox(1, 2) && currentBox == b.getBox(1, 3)) + if (currentBox == b.getBox(1, 2) && currentBox == b.getBox(1, 3)) + expected = currentBox; + if (currentBox == b.getBox(2, 2) && currentBox == b.getBox(3, 3)) + expected = currentBox; + if (currentBox == b.getBox(2, 1) && currentBox == b.getBox(3, 1)) + expected = currentBox; + } + + currentBox = b.getBox(2, 1); + if(currentBox != null && currentBox == b.getBox(2, 2) && currentBox == b.getBox(2, 3)){ + if (expected == null) expected = currentBox; + else if (expected != currentBox) shouldThrow = true; } + + currentBox = b.getBox(3, 1); + if(currentBox != null && !shouldThrow){ + if (currentBox == b.getBox(1, 3) && currentBox == b.getBox(2, 2)) { + if (expected == null) expected = currentBox; + else if (expected != currentBox) shouldThrow = true; + } + if (currentBox == b.getBox(3, 2) && currentBox == b.getBox(3, 3)) { + if (expected == null) expected = currentBox; + else if (expected != currentBox) shouldThrow = true; + } + } + + currentBox = b.getBox(1, 2); + if(!shouldThrow && currentBox != null && currentBox == b.getBox(2, 2) && currentBox == b.getBox(3, 2)){ + if (expected == null) expected = currentBox; + else if (expected != currentBox) shouldThrow = true; + } + + currentBox = b.getBox(1, 3); + if(!shouldThrow && currentBox != null && currentBox == b.getBox(2, 3) && currentBox == b.getBox(3, 3)){ + if (expected == null) expected = currentBox; + else if (expected != currentBox) shouldThrow = true; + } + + if (shouldThrow) Assertions.assertThrows(IllegalGameStateException.class, () -> new WinChecker().getWinner(b)); + else Assertions.assertEquals(expected, new WinChecker().getWinner(b)); } } diff --git a/tictactoe.iml b/tictactoe.iml index 5dfd50f..cc459f3 100644 --- a/tictactoe.iml +++ b/tictactoe.iml @@ -9,5 +9,6 @@ + \ No newline at end of file