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
24 changes: 12 additions & 12 deletions exercises/practice/state-of-tic-tac-toe/.meta/example.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@ namespace eval StateOfTicTacToe {
proc gamestate {board} {
lassign [parse $board] X O

switch [list [won $X] [won $O]] {
{true true} {error "Impossible board: game should have ended after the game was won"}
set xBits [bitCount $X]
set oBits [bitCount $O]
set xWon [won $X]
set oWon [won $O]

{true false} -
{false true} {return "win"}
if {($xWon && $oBits >= $xBits) || ($oWon && $xBits >= $oBits + 1)} {
error "Impossible board: game should have ended after the game was won"
}
if {$xWon || $oWon} then {return "win"}

default {
set xBits [bitCount $X]
set oBits [bitCount $O]
if {$xBits - $oBits > 1} then {error "Wrong turn order: X went twice"}
if {$xBits - $oBits < 0} then {error "Wrong turn order: O started"}
return [expr {$xBits + $oBits == 9 ? "draw" : "ongoing"}]
}
}
if {$xBits - $oBits > 1} then {error "Wrong turn order: X went twice"}
if {$xBits - $oBits < 0} then {error "Wrong turn order: O started"}

return [expr {$xBits + $oBits == 9 ? "draw" : "ongoing"}]
}

proc parse {board} {
Expand Down
20 changes: 20 additions & 0 deletions exercises/practice/state-of-tic-tac-toe/.meta/template.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{header}}

{% for idx, case in cases -%}
{% if idx > 0 -%}
skip {{slug}}-{{idx + 1}}
{% endif -%}
test {{slug}}-{{idx + 1}} {{ case["description"]|tclstring }} -body {
gamestate { {% for row in case["input"]["board"] %}
{{ row|tclstring }}{% endfor %}
}
{% if case["expect_error"] -%}
} -returnCodes error -result "{{ case["expect_error_msg"] }}"

{% else -%}
} -returnCodes ok -result "{{ case["expected"] }}"

{% endif -%}
{% endfor -%}

{{footer}}
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 @@ -99,3 +99,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"
Loading
Loading