From f4f21470bebbee785bd8e86b517ba1c18979823a Mon Sep 17 00:00:00 2001 From: MathJAY10 Date: Fri, 31 Jul 2026 12:27:30 +0530 Subject: [PATCH 1/2] state-of-tic-tac-toe: add missing test cases for invalid board states --- .../src/reference/java/StateOfTicTacToe.java | 18 ++++++++++++++++- .../src/test/java/StateOfTicTacToeTest.java | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java b/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java index 1fcee86d2..d8e500b57 100644 --- a/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java +++ b/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java @@ -51,6 +51,22 @@ public GameState determineState(String[] board) { } } + // --- NAYE CHECKS START --- + // Agar X jeeta hai, toh xCount zaroor (oCount + 1) hona chahiye. Agar barabar hai, matlab O ne extra move chala! + if (xWin > 0 && xCount != oCount + 1) { + throw new IllegalArgumentException( + "Impossible board: game should have ended after the game was won" + ); + } + + // Agar O jeeta hai, toh xCount zaroor oCount ke barabar hona chahiye. Agar xCount zyaada hai, matlab X ne extra move chala! + if (oWin > 0 && xCount != oCount) { + throw new IllegalArgumentException( + "Impossible board: game should have ended after the game was won" + ); + } + // --- NAYE CHECKS END --- + if (xWin > 0 || oWin > 0) { return GameState.WIN; } @@ -108,4 +124,4 @@ private int count(char mark, String[] board) { return result; } -} +} \ No newline at end of file diff --git a/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java b/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java index d264db005..823c0636a 100644 --- a/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java +++ b/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java @@ -282,4 +282,24 @@ public void testInvalidBoardPlayersKeptPlayingAfterAWin() { .isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"XXX", "OOO", "XOX"})) .withMessage("Impossible board: game should have ended after the game was won"); } + + @Disabled("Remove to run test") + @Test + @DisplayName("Invalid board: O kept playing after X wins") + public void testInvalidBoardOKeptPlayingAfterXWins() { + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"XXX", "OO ", "O "})) + .withMessage("Impossible board: game should have ended after the game was won"); + } + + @Disabled("Remove to run test") + @Test + @DisplayName("Invalid board: X kept playing after O wins") + public void testInvalidBoardXKeptPlayingAfterOWins() { + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"OOO", "XX ", "XX "})) + .withMessage("Impossible board: game should have ended after the game was won"); + } } From c5707fe258a20380960a4e2e59133740556ff0bb Mon Sep 17 00:00:00 2001 From: MathJAY10 Date: Fri, 31 Jul 2026 18:46:38 +0530 Subject: [PATCH 2/2] fix: align test cases with canonical data and clean up comments --- .../.meta/src/reference/java/StateOfTicTacToe.java | 7 ++----- .../practice/state-of-tic-tac-toe/.meta/tests.toml | 2 +- .../src/test/java/StateOfTicTacToeTest.java | 10 +++++----- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java b/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java index d8e500b57..9c0449142 100644 --- a/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java +++ b/exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java @@ -51,21 +51,19 @@ public GameState determineState(String[] board) { } } - // --- NAYE CHECKS START --- - // Agar X jeeta hai, toh xCount zaroor (oCount + 1) hona chahiye. Agar barabar hai, matlab O ne extra move chala! + // If X wins, X must have played exactly one more move than O. if (xWin > 0 && xCount != oCount + 1) { throw new IllegalArgumentException( "Impossible board: game should have ended after the game was won" ); } - // Agar O jeeta hai, toh xCount zaroor oCount ke barabar hona chahiye. Agar xCount zyaada hai, matlab X ne extra move chala! + // If O wins, X and O must have played an equal number of moves. if (oWin > 0 && xCount != oCount) { throw new IllegalArgumentException( "Impossible board: game should have ended after the game was won" ); } - // --- NAYE CHECKS END --- if (xWin > 0 || oWin > 0) { return GameState.WIN; @@ -98,7 +96,6 @@ private List getDiagonals(String[] board) { String[] diags = new String[2]; for (int i = 0; i < 3; i++) { - if (diags[0] == null) { diags[0] = String.valueOf(board[i].charAt(i)); } else { diff --git a/exercises/practice/state-of-tic-tac-toe/.meta/tests.toml b/exercises/practice/state-of-tic-tac-toe/.meta/tests.toml index 8fc25e211..2b5c67c73 100644 --- a/exercises/practice/state-of-tic-tac-toe/.meta/tests.toml +++ b/exercises/practice/state-of-tic-tac-toe/.meta/tests.toml @@ -98,4 +98,4 @@ description = "Invalid boards -> Invalid board: X won and O kept playing" reimplements = "b1dc8b13-46c4-47db-a96d-aa90eedc4e8d" [4801cda2-f5b7-4c36-8317-3cdd167ac22c] -description = "Invalid boards -> Invalid board: players kept playing after a win" +description = "Invalid boards -> Invalid board: players kept playing after a win" \ No newline at end of file diff --git a/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java b/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java index 823c0636a..6a60694e8 100644 --- a/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java +++ b/exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java @@ -95,7 +95,7 @@ public void testFinishedGameWhereXWonViaMiddleRowVictory() { @Disabled("Remove to run test") @Test - @DisplayName("Finished game where X won via middle row victory") + @DisplayName("Finished game where X won via bottom row victory") public void testFinishedGameWhereXWonViaBottomRowVictory() { assertThat( @@ -207,7 +207,7 @@ public void testDraw() { @Test @DisplayName("Another draw") public void testAnotherDraw() { - + assertThat( stateOfTicTacToe.determineState(new String[]{"XXO", "OXX", "XOO"}) ).isEqualTo(GameState.DRAW); @@ -289,7 +289,7 @@ public void testInvalidBoardPlayersKeptPlayingAfterAWin() { public void testInvalidBoardOKeptPlayingAfterXWins() { assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"XXX", "OO ", "O "})) + .isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"OO ", "XXX", " O "})) .withMessage("Impossible board: game should have ended after the game was won"); } @@ -299,7 +299,7 @@ public void testInvalidBoardOKeptPlayingAfterXWins() { public void testInvalidBoardXKeptPlayingAfterOWins() { assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"OOO", "XX ", "XX "})) + .isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"XX ", "OOO", " XX"})) .withMessage("Impossible board: game should have ended after the game was won"); } -} +} \ No newline at end of file