From 300353ffc46e6a4f5de33e7ac2513e92f7898645 Mon Sep 17 00:00:00 2001 From: Elliott Le Guehennec Date: Tue, 7 Feb 2023 21:15:03 +0100 Subject: [PATCH] more tests --- .idea/.gitignore | 8 ++ .idea/misc.xml | 6 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + src/model/Board.java | 2 +- src/model/IllegalGameStateException.java | 7 + src/model/Player.java | 3 +- src/model/WinChecker.java | 7 + test/InputCheckerTests.java | 16 ++- test/WinCheckerTests.java | 169 +++++++++++++++++++++++ tictactoe.iml | 13 ++ 11 files changed, 237 insertions(+), 8 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 src/model/IllegalGameStateException.java create mode 100644 tictactoe.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0319d5d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..2194355 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/model/Board.java b/src/model/Board.java index 20ca38b..f4a753d 100644 --- a/src/model/Board.java +++ b/src/model/Board.java @@ -5,6 +5,6 @@ public class Board { } public Player getBox(int column, int row) { - return Player.None; + return null; } } diff --git a/src/model/IllegalGameStateException.java b/src/model/IllegalGameStateException.java new file mode 100644 index 0000000..69d7b8e --- /dev/null +++ b/src/model/IllegalGameStateException.java @@ -0,0 +1,7 @@ +package model; + +public class IllegalGameStateException extends Exception{ + public IllegalGameStateException(){ + super("This gamestate is illegal"); + } +} diff --git a/src/model/Player.java b/src/model/Player.java index 5df7e61..ec0d0a0 100644 --- a/src/model/Player.java +++ b/src/model/Player.java @@ -2,6 +2,5 @@ package model; public enum Player { X, - O, - None + O } diff --git a/src/model/WinChecker.java b/src/model/WinChecker.java index ba1ba23..0d536dd 100644 --- a/src/model/WinChecker.java +++ b/src/model/WinChecker.java @@ -1,4 +1,11 @@ package model; public class WinChecker { + public boolean isGameOver(Board b) { + return false; + } + + public Player getWinner(Board b) { + return null; + } } diff --git a/test/InputCheckerTests.java b/test/InputCheckerTests.java index c3ed342..5aaee88 100644 --- a/test/InputCheckerTests.java +++ b/test/InputCheckerTests.java @@ -25,12 +25,12 @@ public class InputCheckerTests { public static Stream boxOccupiedTestData() { Board b = randomBoard(); - int index = rng.nextInt(1, 10); + int index = rng.nextInt(9) + 1; boolean isValid = true; for (int i = 1; i <= 3; ++i) for (int j = 1; j <= 3; ++j) - if (b.getBox(i, j) != Player.None) + if (b.getBox(i, j) != null) isValid = false; return Stream.of( @@ -57,11 +57,17 @@ public class InputCheckerTests { } private static Board randomBoard(){ - Player[] vals = Player.values(); Board b = new Board(); for (int i = 1; i <= 3; ++i) - for (int j = 1; j <= 3; ++j) - b.setBox(i, j, vals[rng.nextInt(vals.length)]); + 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; + }; + b.setBox(i, j, p); + } return b; } diff --git a/test/WinCheckerTests.java b/test/WinCheckerTests.java index d4f34d4..5d73d9b 100644 --- a/test/WinCheckerTests.java +++ b/test/WinCheckerTests.java @@ -1,2 +1,171 @@ +import model.Board; +import model.IllegalGameStateException; +import model.Player; +import model.WinChecker; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.Random; +import java.util.stream.Stream; + public class WinCheckerTests { + private static Random rng = new Random(); + + public static Stream isGameOverTestData() { + return Stream.of( + Arguments.of(new Board(), false), + Arguments.of(topRowXWins(), true), + Arguments.of(midRowOWins(), true), + Arguments.of(bottomRow(), true), + Arguments.of(leftColumn(), true), + Arguments.of(midColumn(), true), + Arguments.of(rightColumn(), true), + Arguments.of(diagUp(), true), + Arguments.of(diagDown(), true), + Arguments.of(stalemate(), true) + ); + } + + private static Board stalemate() { + Board b = new Board(); + b.setBox(1, 1, Player.X); + b.setBox(1, 2, Player.O); + b.setBox(1, 3, Player.X); + b.setBox(2, 1, Player.O); + b.setBox(2, 2, Player.O); + b.setBox(2, 3, Player.X); + b.setBox(3, 1, Player.X); + b.setBox(3, 2, Player.X); + b.setBox(3, 3, Player.O); + return b; + } + + private static Board diagDown() { + Board b = new Board(); + b.setBox(3, 1, Player.O); + b.setBox(2, 2, Player.O); + b.setBox(1, 3, Player.O); + return b; + } + + private static Board diagUp() { + Board b = new Board(); + b.setBox(1, 1, Player.O); + b.setBox(2, 2, Player.O); + b.setBox(3, 3, Player.O); + return b; + } + + private static Board rightColumn() { + Board b = new Board(); + b.setBox(3, 1, Player.X); + b.setBox(3, 2, Player.X); + b.setBox(3, 3, Player.X); + return b; + } + + private static Board midColumn() { + Board b = new Board(); + b.setBox(2, 1, Player.O); + b.setBox(2, 2, Player.O); + b.setBox(2, 3, Player.O); + return b; + } + + private static Board leftColumn() { + Board b = new Board(); + b.setBox(1, 1, Player.O); + b.setBox(1, 2, Player.O); + b.setBox(1, 3, Player.O); + return b; + } + + private static Board bottomRow() { + Board b = new Board(); + b.setBox(1, 3, Player.X); + b.setBox(2, 3, Player.X); + b.setBox(3, 3, Player.X); + return b; + } + + private static Board midRowOWins() { + Board b = new Board(); + b.setBox(1, 2, Player.O); + b.setBox(2, 2, Player.O); + b.setBox(3, 2, Player.O); + return b; + } + + private static Board topRowXWins() { + Board b = new Board(); + b.setBox(1, 1, Player.X); + b.setBox(2, 1, Player.X); + b.setBox(3, 1, Player.X); + return b; + } + + public static Stream getWinnerTestNormalData() { + return Stream.of( + Arguments.of(new Board(), null), + Arguments.of(stalemate(), null), + Arguments.of(topRowXWins(), Player.X), + Arguments.of(midRowOWins(), Player.O) + ); + } + + @ParameterizedTest + @MethodSource("isGameOverTestData") + public void isGameOverTest(Object[] args){ + Board b = (Board) args[0]; + boolean expected = (boolean) args[1]; + boolean actual = new WinChecker().isGameOver(b); + Assertions.assertEquals(expected, actual); + } + + @ParameterizedTest + @MethodSource("getWinnerTestNormalData") + public void getWinnerTestNormal(Object[] args){ + Board b = (Board) args[0]; + Player expected = (Player) args[1]; + Player actual = new WinChecker().getWinner(b); + Assertions.assertEquals(expected, actual); + } + + @Test + public void getWinnerTestThrows(){ + Board b = new Board(); + b.setBox(1, 1, Player.X); + b.setBox(1, 2, Player.X); + b.setBox(1, 3, Player.X); + b.setBox(2, 1, Player.O); + b.setBox(2, 2, Player.O); + b.setBox(2, 3, Player.O); + Assertions.assertThrows(IllegalGameStateException.class, () -> new WinChecker().getWinner(b)); + } + + @Test + public void getWinnerTestRandom(){ + Player currentBox; + 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); + Player p = switch (rand){ + case 1 -> Player.X; + case 2 -> Player.O; + default -> null; + }; + b.setBox(i, j, p); + } + + currentBox = b.getBox(1, 1); + if(currentBox != null){ + //if (currentBox == b.getBox(1, 2) && currentBox == b.getBox(1, 3)) + } + } } diff --git a/tictactoe.iml b/tictactoe.iml new file mode 100644 index 0000000..5dfd50f --- /dev/null +++ b/tictactoe.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file