Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
extends SceneTree
2026-11-08 17:49:35 -06:00
## Keeps the simulator (BoardTester, and its CLI tools/simulate_board.gd) working: runs it on a small inline board with
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
## a choice, a loop, a check, a bad effect, an unreachable table and a sheet.
## Run: godot --headless --path . -s res://tests/test_simulate.gd
var _failures : = 0
func _init ( ) - > void :
var b : = _board ( )
2026-11-08 17:49:35 -06:00
var r : Dictionary = await BoardTester . simulate ( b , " start " , { " runs " : 60 , " seed " : 3 , " track " : [ " gold " , " @name " ] } )
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
_eq ( r [ " runs " ] , 60 , " 60 runs " )
_eq ( r [ " completed " ] , 60 , " every run completes " )
_check ( ( r [ " halted " ] as Dictionary ) . is_empty ( ) , " nothing halted: %s " % str ( r [ " halted " ] ) )
var a : Dictionary = r [ " actions " ]
_check ( a [ " max " ] > = a [ " p95 " ] and a [ " p95 " ] > = 4 and a [ " mean " ] > 4.0 , " action stats are sensible: %s " % str ( a ) )
var errs : Array = r [ " errors " ]
_check ( errs . size ( ) == 1 and str ( errs [ 0 ] [ " table " ] ) . contains ( " pick " ) and str ( errs [ 0 ] [ " entry " ] ) == " Broken " , " the bad effect is reported with its table and entry: %s " % str ( errs ) )
_eq ( r [ " unreached " ] , [ " island " ] , " the island table is never reached " )
var uc : Dictionary = r [ " unchosen " ]
_check ( not uc . has ( " check " ) , " an entry that can ' t come up (13 on 2d6) is not reported as unchosen: %s " % str ( uc ) )
_check ( not uc . has ( " pick " ) , " every pick option was chosen at some point " )
var sheet : = str ( r [ " sample_sheet " ] )
_check ( sheet . begins_with ( " Hero: " ) and sheet . contains ( " Gold " ) , " a sample sheet is kept: %s " % sheet )
2026-11-08 17:49:35 -06:00
_check ( not BoardTester . is_clean ( r ) , " a run with an effect error is not clean " )
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
# Each run starts with empty variables: gold never grows past one run's worth.
var gold : = int ( ( r [ " sample_vars " ] . get ( " gold " , { } ) as Dictionary ) . get ( " value " , - 1 ) )
_check ( gold < = 18 , " variables start clean for every run (gold %d ) " % gold )
var mk : Array = r [ " unset_markers " ]
_check ( mk . size ( ) == 1 and mk [ 0 ] [ " entry " ] == " (prompt) " and str ( mk [ 0 ] [ " text " ] ) . contains ( " @nothing " ) , " an unset variable in a prompt is reported: %s " % str ( mk ) )
var tg : Dictionary = r [ " track " ] [ " gold " ]
_check ( int ( tg [ " n " ] ) > 0 and float ( tg [ " max " ] ) < = 18.0 and float ( tg [ " min " ] ) > = 0.0 , " --track summarises a number variable: %s " % str ( tg ) )
_eq ( r [ " track " ] [ " name " ] [ " n " ] , 0 , " --track skips text variables " )
var lg : Dictionary = r [ " longest " ]
_check ( int ( lg [ " actions " ] ) == int ( a [ " max " ] ) and not ( lg [ " top " ] as Array ) . is_empty ( ) , " the longest run ' s busiest tables are kept: %s " % str ( lg ) )
# A preference picks the matching option every time.
2026-11-08 17:49:35 -06:00
var r2 : Dictionary = await BoardTester . simulate ( b , " start " , { " runs " : 20 , " seed " : 4 , " prefer " : " ^Mine$ " } )
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
_check ( ( r2 [ " unchosen " ] as Dictionary ) . has ( " pick " ) , " --prefer always picks the matching option " )
2026-11-10 18:24:23 -06:00
_check ( int ( ( r2 [ " table_hits " ] as Dictionary ) . get ( " mine " , 0 ) ) == 20 * 3 , " the preferred option ' s thread ran on every pass " )
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
# The action limit is reported as a halt reason.
2026-11-08 17:49:35 -06:00
var r3 : Dictionary = await BoardTester . simulate ( b , " start " , { " runs " : 5 , " seed " : 5 , " max_actions " : 4 } )
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
_eq ( ( r3 [ " halted " ] as Dictionary ) . get ( " Action limit " , 0 ) , 5 , " the action limit is a halt reason " )
2026-11-08 17:49:35 -06:00
var text : = BoardTester . format_report ( r )
Add a headless board simulator tool and its test
tools/simulate_board.gd rolls a collection N times with a random (or
regex-preferred) player answering every choice card, each run on fresh
board variables, and reports completions, halt reasons, action stats,
effect/expression errors with table and entry, dead ends, unset or
failed markers in text and prompts, unreached tables, never-chosen
entries, tracked variable ranges, the longest run's busiest tables and
a sample final sheet. tests/test_simulate.gd runs it on an inline board.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 14:15:21 -06:00
_check ( text . contains ( " Runs: 60 " ) and text . contains ( " Tables never reached: 1 " ) and text . contains ( " Sample final sheet " ) and text . contains ( " @gold: min " ) , " the report prints: \n %s " % text )
print ( " %s : %d failure(s) " % [ " PASS " if _failures == 0 else " FAIL " , _failures ] )
quit ( 1 if _failures else 0 )
func _board ( ) - > TowerBoard :
var b : = TowerBoard . new ( )
b . id = " sim-test "
var start : = RollTable . new ( " start " , " Start " )
var e : = TableEntry . new ( " e1 " , " Begin " )
e . effects = " reset @gold; @name = \" Hero \" "
e . add_pipe ( " loop " )
e . add_pipe ( " sheet " )
start . entries . append ( e )
var loop : = RollTable . create ( RollTable . KIND_LOOP , " loop " , " Three times " , " times " )
loop . loop_expr = " 3 "
loop . entries [ 0 ] . add_pipe ( " pick " )
var pick : = RollTable . create ( RollTable . KIND_CHOICE , " pick " , " Pick " , " pick " )
pick . prompt = " Which? {@nothing} "
pick . entries [ 0 ] . text = " Mine "
pick . entries [ 0 ] . effects = " @gold += 1d6 "
pick . entries [ 0 ] . add_pipe ( " mine " )
pick . entries [ 1 ] . text = " Check "
pick . entries [ 1 ] . add_pipe ( " check " )
pick . entries [ 2 ] . text = " Broken "
pick . entries [ 2 ] . effects = " @gold += nope( "
var mine : = RollTable . new ( " mine " , " Mine " )
mine . entries . append ( TableEntry . new ( " e1 " , " Dig " ) )
var check : = RollTable . new ( " check " , " Check " )
check . dice = " 2d6 "
for spec in [ [ 2 , 7 , " Fail " ] , [ 8 , 12 , " Pass " ] , [ 13 , 13 , " Impossible " ] ] :
var ce : = TableEntry . new ( " c %d " % spec [ 0 ] , spec [ 2 ] )
ce . has_range = true
ce . range_lo = spec [ 0 ]
ce . range_hi = spec [ 1 ]
check . entries . append ( ce )
var island : = RollTable . new ( " island " , " Island " )
island . entries . append ( TableEntry . new ( " e1 " , " Alone " ) )
var sheet : = RollTable . create ( RollTable . KIND_SHEET , " sheet " , " Sheet " )
sheet . entries [ 0 ] . text = " {@name}: Gold {@gold} "
var gold : = RollTable . create ( RollTable . KIND_VARIABLE , " v_gold " , " Gold " , " number " )
gold . var_name = " gold "
for t in [ start , loop , pick , mine , check , island , sheet , gold ] :
b . tables . append ( t )
return b
func _eq ( got : Variant , want : Variant , what : String ) - > void :
_check ( str ( got ) == str ( want ) , " %s \n got: %s \n want: %s " % [ what , str ( got ) , str ( want ) ] )
func _check ( ok : bool , what : String ) - > void :
if not ok :
_failures += 1
print ( " ✗ " , what )