allow simulating more than 2 legs ahead

This commit is contained in:
Joseph Montanaro 2021-03-20 23:56:07 -07:00
parent 2aa0e63987
commit e0f83cdca1

View File

@ -1,4 +1,4 @@
import math, hashes, options, tables, sequtils, sets import math, hashes, options, tables, sequtils, sets, sugar
import combinators import combinators
@ -23,6 +23,7 @@ type
Board = object Board = object
squares: array[1..16, Square] squares: array[1..16, Square]
camels: array[Color, range[1..16]] camels: array[Color, range[1..16]]
diceRolled: array[Color, bool]
leader: Option[Color] leader: Option[Color]
gameOver: bool gameOver: bool
@ -110,11 +111,17 @@ proc advance(b: var Board, die: Die) =
if endPos >= b.camels[b.leader.get]: if endPos >= b.camels[b.leader.get]:
b.leader = some(b[endPos].camels[^1]) b.leader = some(b[endPos].camels[^1])
break # breaking the outer loop here, not the inner - but only conditionally! gah! break # breaking the outer loop here, not the inner - but only conditionally! gah!
b.diceRolled[color] = true
proc simulateLeg(b: Board, diceRemaining: seq[Color]): LegResults = proc projectLeg(b: Board): LegResults =
var scores: ScoreSet var scores: ScoreSet
var endStates: HashSet[Board] var endStates: HashSet[Board]
let diceRemaining = collect(newSeq):
for i, c in b.diceRolled:
if not c: i
for future in possibleFutures(diceRemaining): for future in possibleFutures(diceRemaining):
var prediction = b # make a copy var prediction = b # make a copy
for dieRoll in future: for dieRoll in future:
@ -125,31 +132,27 @@ proc simulateLeg(b: Board, diceRemaining: seq[Color]): LegResults =
result = (scores, endStates) result = (scores, endStates)
proc simulateGame(b: Board, diceRemaining: seq[Color]): ScoreSet = proc projectOutcomes(b: Board, maxDepth = 1): ScoreSet =
# currently just simulates two legs, not a full game var outcomeStack = @[ [b].toHashSet ]
var (scores, startStates) = b.simulateLeg(diceRemaining) for depth in 1..maxDepth:
var endStates: HashSet[Board] var endStates: HashSet[Board]
for o in outcomeStack[^1]:
let projection = o.projectLeg
result.update(projection[0])
endStates.incl(projection[1])
stdout.write("simulated: " & $result.sum & "\r")
for s in startStates: outcomeStack.add(endStates)
let (nextScores, nextStates) = s.simulateLeg(@[cRed, cGreen, cBlue, cYellow, cPurple]) echo "\nDistinct end states: ", outcomeStack.mapIt(it.len).sum
scores.update(nextScores)
endStates.incl(nextStates)
stdout.write("simulated: " & $scores.sum & "\r")
echo "\nDistinct end states: ", endStates.len
result = scores
var b: Board var b: Board
b.display(1, 5) b.display(1, 5)
b.setState({cGreen: 4, cYellow: 3, cPurple: 4, cBlue: 3, cRed: 2}, @[]) b.setState({cGreen: 4, cYellow: 3, cPurple: 4, cBlue: 3, cRed: 4}, @[])
b.display(1, 5) b.display(1, 5)
b.advance((cRed, 2)) let r = b.projectOutcomes(2)
b.display(1, 5)
let r = b.simulateGame(allDice)
let total = r.sum let total = r.sum
for i, c in r: for i, c in r:
echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")" echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")"