Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ public GameState determineState(String[] board) {
}
}

// 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"
);
}

// 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"
);
}

if (xWin > 0 || oWin > 0) {
return GameState.WIN;
}
Expand Down Expand Up @@ -82,7 +96,6 @@ private List<String> 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 {
Expand All @@ -108,4 +121,4 @@ private int count(char mark, String[] board) {

return result;
}
}
}
2 changes: 1 addition & 1 deletion exercises/practice/state-of-tic-tac-toe/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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[]{"OO ", "XXX", " 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[]{"XX ", "OOO", " XX"}))
.withMessage("Impossible board: game should have ended after the game was won");
}
}