File tree Expand file tree Collapse file tree
exercises/practice/state-of-tic-tac-toe Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -46,24 +46,27 @@ fn gamestate(board []string) !State {
4646 }
4747 }
4848
49- if count_o > count_x {
50- return error ('Wrong turn order: O started' )
51- }
52-
53- if count_x > count_o + 1 {
54- return error ('Wrong turn order: X went twice' )
55- }
56-
5749 mut win_x := is_win (bitset_x)
5850 mut win_o := is_win (bitset_o)
5951
60- if win_x || win_o {
61- if win_x && win_o {
62- return error ('Impossible board: game should have ended after the game was won' )
52+ return match true {
53+ count_o > count_x {
54+ error ('Wrong turn order: O started' )
55+ }
56+ count_x > count_o + 1 {
57+ error ('Wrong turn order: X went twice' )
58+ }
59+ (win_x && count_o == count_x) || (win_o && count_x == count_o + 1 ) || (win_x && win_o) {
60+ error ('Impossible board: game should have ended after the game was won' )
61+ }
62+ win_x || win_o {
63+ .win
64+ }
65+ count_x + count_o == 9 {
66+ .draw
67+ }
68+ else {
69+ .ongoing
6370 }
64-
65- return .win
6671 }
67-
68- return if count_x + count_o == 9 { .draw } else { .ongoing }
6972}
Original file line number Diff line number Diff line change @@ -92,3 +92,9 @@ reimplements = "b1dc8b13-46c4-47db-a96d-aa90eedc4e8d"
9292
9393[4801cda2-f5b7-4c36-8317-3cdd167ac22c ]
9494description = " Invalid boards -> Invalid board: players kept playing after a win"
95+
96+ [5a84757a-fc86-4328-aec9-a5759e6ed35d ]
97+ description = " Invalid boards -> Invalid board: O kept playing after X wins"
98+
99+ [cf25543d-583a-4656-b9ab-f82dc00a4a02 ]
100+ description = " Invalid boards -> Invalid board: X kept playing after O wins"
Original file line number Diff line number Diff line change @@ -258,3 +258,29 @@ fn test_invalid_boards__invalid_board__players_kept_playing_after_a_win() {
258258 assert err.msg () == 'Impossible board: game should have ended after the game was won'
259259 }
260260}
261+
262+ fn test_invalid_boards__invalid_board__o_kept_playing_after_x_wins () {
263+ board := [
264+ 'OO ' ,
265+ 'XXX' ,
266+ ' O ' ,
267+ ]
268+ if res := gamestate (board) {
269+ assert false , 'Invalid board: O kept playing after X wins should return an error'
270+ } else {
271+ assert err.msg () == 'Impossible board: game should have ended after the game was won'
272+ }
273+ }
274+
275+ fn test_invalid_boards__invalid_board__x_kept_playing_after_o_wins () {
276+ board := [
277+ 'XX ' ,
278+ 'OOO' ,
279+ ' XX' ,
280+ ]
281+ if res := gamestate (board) {
282+ assert false , 'Invalid board: X kept playing after O wins should return an error'
283+ } else {
284+ assert err.msg () == 'Impossible board: game should have ended after the game was won'
285+ }
286+ }
You can’t perform that action at this time.
0 commit comments