diff --git a/src/main/java/TicTacToe/TicTacToeGame.java b/src/main/java/TicTacToe/TicTacToeGame.java index 9784f8b..c551abf 100644 --- a/src/main/java/TicTacToe/TicTacToeGame.java +++ b/src/main/java/TicTacToe/TicTacToeGame.java @@ -18,6 +18,7 @@ public class TicTacToeGame extends JPanel { public cell[] field; public int playerID = 1; + public int turns = 0; public TicTacToeGame() { this.setSize(width, height); @@ -75,7 +76,13 @@ public class TicTacToeGame extends JPanel { JOptionPane.showMessageDialog(getParent(),"Player: " + playerID + " Wins!"); System.exit(0); } - + + turns++; + if(turns >= playFieldSize) { + JOptionPane.showMessageDialog(getParent(),"Draw!"); + System.exit(0); + } + playerID++; if(playerID >= maxPlayers) { playerID = 1; diff --git a/src/test/java/TicTacToe/TicTacToeGameTest.java b/src/test/java/TicTacToe/TicTacToeGameTest.java index 1211ca3..beaabde 100644 --- a/src/test/java/TicTacToe/TicTacToeGameTest.java +++ b/src/test/java/TicTacToe/TicTacToeGameTest.java @@ -24,6 +24,21 @@ class TicTacToeGameTest { assertEquals(_winner, realWinner); } + @ParameterizedTest + @MethodSource("testFieldsDraw") + void testGameEndDraw(int[] _field) { + TicTacToeGame ttt = new TicTacToeGame(); + + for(int i = 0; i < ttt.field.length; i++) { + ttt.field[i].playerID = _field[i]; + } + + int noWinner = ttt.checkPlayfield(); + + assertEquals(0, noWinner); + } + + private static Stream testFieldsWinning(){ return Stream.of( Arguments.of(new int[]{ 1,2,1, @@ -39,6 +54,23 @@ class TicTacToeGameTest { 1,2,1, 1,1,2}, 2) ); + } + + private static Stream testFieldsDraw(){ + return Stream.of( + Arguments.of(new int[]{ 2,1,1, + 2,2,2, + 1,2,1}), + Arguments.of(new int[]{ 2,1,2, + 2,2,1, + 1,2,1}), + Arguments.of(new int[]{ 2,1,2, + 1,2,2, + 1,2,1}), + Arguments.of(new int[]{ 2,1,1, + 1,2,2, + 2,1,1}) + ); }