Skip to content

Commit d8a4083

Browse files
state-of-tic-tac-toe: add missing test cases for invalid board states (#3153)
* state-of-tic-tac-toe: add missing test cases for invalid board states * fix: align test cases with canonical data and clean up comments * chore: sync tests.toml using configlet * fix: enable all tests and add trailing newline * fix: enable all tests and resolve checkstyle newline errors * remove comments * final commit for endlines * Update exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java Co-authored-by: Jagdish Prajapati <jagadishdrp@gmail.com> * Update exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java Co-authored-by: Jagdish Prajapati <jagadishdrp@gmail.com> * Update exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java Co-authored-by: Jagdish Prajapati <jagadishdrp@gmail.com> * rectify code --------- Co-authored-by: Jagdish Prajapati <jagadishdrp@gmail.com>
1 parent fc789fb commit d8a4083

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

exercises/practice/state-of-tic-tac-toe/.meta/src/reference/java/StateOfTicTacToe.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ public GameState determineState(String[] board) {
5151
}
5252
}
5353

54+
if (xWin > 0 && xCount != oCount + 1) {
55+
throw new IllegalArgumentException(
56+
"Impossible board: game should have ended after the game was won"
57+
);
58+
}
59+
60+
if (oWin > 0 && xCount != oCount) {
61+
throw new IllegalArgumentException(
62+
"Impossible board: game should have ended after the game was won"
63+
);
64+
}
65+
5466
if (xWin > 0 || oWin > 0) {
5567
return GameState.WIN;
5668
}
@@ -82,7 +94,6 @@ private List<String> getDiagonals(String[] board) {
8294
String[] diags = new String[2];
8395

8496
for (int i = 0; i < 3; i++) {
85-
8697
if (diags[0] == null) {
8798
diags[0] = String.valueOf(board[i].charAt(i));
8899
} else {

exercises/practice/state-of-tic-tac-toe/.meta/tests.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,9 @@ reimplements = "b1dc8b13-46c4-47db-a96d-aa90eedc4e8d"
9999

100100
[4801cda2-f5b7-4c36-8317-3cdd167ac22c]
101101
description = "Invalid boards -> Invalid board: players kept playing after a win"
102+
103+
[5a84757a-fc86-4328-aec9-a5759e6ed35d]
104+
description = "Invalid boards -> Invalid board: O kept playing after X wins"
105+
106+
[cf25543d-583a-4656-b9ab-f82dc00a4a02]
107+
description = "Invalid boards -> Invalid board: X kept playing after O wins"

exercises/practice/state-of-tic-tac-toe/src/test/java/StateOfTicTacToeTest.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public void testFinishedGameWhereXWonViaMiddleRowVictory() {
9595

9696
@Disabled("Remove to run test")
9797
@Test
98-
@DisplayName("Finished game where X won via middle row victory")
98+
@DisplayName("Finished game where X won via bottom row victory")
9999
public void testFinishedGameWhereXWonViaBottomRowVictory() {
100100

101101
assertThat(
@@ -207,7 +207,7 @@ public void testDraw() {
207207
@Test
208208
@DisplayName("Another draw")
209209
public void testAnotherDraw() {
210-
210+
211211
assertThat(
212212
stateOfTicTacToe.determineState(new String[]{"XXO", "OXX", "XOO"})
213213
).isEqualTo(GameState.DRAW);
@@ -282,4 +282,24 @@ public void testInvalidBoardPlayersKeptPlayingAfterAWin() {
282282
.isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"XXX", "OOO", "XOX"}))
283283
.withMessage("Impossible board: game should have ended after the game was won");
284284
}
285+
286+
@Disabled("Remove to run test")
287+
@Test
288+
@DisplayName("Invalid board: O kept playing after X wins")
289+
public void testInvalidBoardOKeptPlayingAfterXWins() {
290+
291+
assertThatExceptionOfType(IllegalArgumentException.class)
292+
.isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"OO ", "XXX", " O "}))
293+
.withMessage("Impossible board: game should have ended after the game was won");
294+
}
295+
296+
@Disabled("Remove to run test")
297+
@Test
298+
@DisplayName("Invalid board: X kept playing after O wins")
299+
public void testInvalidBoardXKeptPlayingAfterOWins() {
300+
301+
assertThatExceptionOfType(IllegalArgumentException.class)
302+
.isThrownBy(() -> stateOfTicTacToe.determineState(new String[]{"XX ", "OOO", " XX"}))
303+
.withMessage("Impossible board: game should have ended after the game was won");
304+
}
285305
}

0 commit comments

Comments
 (0)