From 7ba8c04990bcf2404e15bcd7b1cf6d3197644dc2 Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Sat, 11 Jul 2026 08:19:01 +1000 Subject: [PATCH] sync state-of-tic-tac-toe --- .../state-of-tic-tac-toe/.meta/example.v | 33 ++++++++++--------- .../state-of-tic-tac-toe/.meta/tests.toml | 6 ++++ .../practice/state-of-tic-tac-toe/run_test.v | 26 +++++++++++++++ 3 files changed, 50 insertions(+), 15 deletions(-) diff --git a/exercises/practice/state-of-tic-tac-toe/.meta/example.v b/exercises/practice/state-of-tic-tac-toe/.meta/example.v index 8958b6a..5b0913f 100644 --- a/exercises/practice/state-of-tic-tac-toe/.meta/example.v +++ b/exercises/practice/state-of-tic-tac-toe/.meta/example.v @@ -46,24 +46,27 @@ fn gamestate(board []string) !State { } } - if count_o > count_x { - return error('Wrong turn order: O started') - } - - if count_x > count_o + 1 { - return error('Wrong turn order: X went twice') - } - mut win_x := is_win(bitset_x) mut win_o := is_win(bitset_o) - if win_x || win_o { - if win_x && win_o { - return error('Impossible board: game should have ended after the game was won') + return match true { + count_o > count_x { + error('Wrong turn order: O started') + } + count_x > count_o + 1 { + error('Wrong turn order: X went twice') + } + (win_x && count_o == count_x) || (win_o && count_x == count_o + 1) || (win_x && win_o) { + error('Impossible board: game should have ended after the game was won') + } + win_x || win_o { + .win + } + count_x + count_o == 9 { + .draw + } + else { + .ongoing } - - return .win } - - return if count_x + count_o == 9 { .draw } else { .ongoing } } 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 a9a0251..d46c3be 100644 --- a/exercises/practice/state-of-tic-tac-toe/.meta/tests.toml +++ b/exercises/practice/state-of-tic-tac-toe/.meta/tests.toml @@ -92,3 +92,9 @@ reimplements = "b1dc8b13-46c4-47db-a96d-aa90eedc4e8d" [4801cda2-f5b7-4c36-8317-3cdd167ac22c] description = "Invalid boards -> Invalid board: players kept playing after a win" + +[5a84757a-fc86-4328-aec9-a5759e6ed35d] +description = "Invalid boards -> Invalid board: O kept playing after X wins" + +[cf25543d-583a-4656-b9ab-f82dc00a4a02] +description = "Invalid boards -> Invalid board: X kept playing after O wins" diff --git a/exercises/practice/state-of-tic-tac-toe/run_test.v b/exercises/practice/state-of-tic-tac-toe/run_test.v index c5179f0..5f23b6e 100644 --- a/exercises/practice/state-of-tic-tac-toe/run_test.v +++ b/exercises/practice/state-of-tic-tac-toe/run_test.v @@ -258,3 +258,29 @@ fn test_invalid_boards__invalid_board__players_kept_playing_after_a_win() { assert err.msg() == 'Impossible board: game should have ended after the game was won' } } + +fn test_invalid_boards__invalid_board__o_kept_playing_after_x_wins() { + board := [ + 'OO ', + 'XXX', + ' O ', + ] + if res := gamestate(board) { + assert false, 'Invalid board: O kept playing after X wins should return an error' + } else { + assert err.msg() == 'Impossible board: game should have ended after the game was won' + } +} + +fn test_invalid_boards__invalid_board__x_kept_playing_after_o_wins() { + board := [ + 'XX ', + 'OOO', + ' XX', + ] + if res := gamestate(board) { + assert false, 'Invalid board: X kept playing after O wins should return an error' + } else { + assert err.msg() == 'Impossible board: game should have ended after the game was won' + } +}