WinCheckerTests complete

master
Elliott LE GUEHENNEC 2 years ago
parent 300353ffc4
commit 309d217751

@ -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<Arguments> 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));
}
}

@ -9,5 +9,6 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="junit.jupiter" level="project" />
<orderEntry type="library" name="junit" level="project" />
</component>
</module>
Loading…
Cancel
Save