Skip to content
Merged
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
33 changes: 18 additions & 15 deletions exercises/practice/state-of-tic-tac-toe/.meta/example.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
6 changes: 6 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
26 changes: 26 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/run_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
}
Loading