working with recursion

This commit is contained in:
Joseph Montanaro 2021-03-19 17:47:18 -07:00
parent 86ef069848
commit c11e4e4696

View File

@ -1,4 +1,4 @@
import algorithm, options, sequtils, sugar import algorithm, math, options, tables, sequtils, sugar
type type
@ -15,13 +15,16 @@ type
Die = tuple[color: Color, value: range[1..3]] Die = tuple[color: Color, value: range[1..3]]
ScoreSet = array[Color, int]
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]]
leader: Color leader: Color
gameOver: bool
proc `[]`(b: var Board, idx: range[1..16]): var Square = proc `[]`[T](b: var Board, idx: T): var Square =
b.squares[idx] b.squares[idx]
@ -33,17 +36,18 @@ proc display(b: Board, start, stop: int) =
echo lead, sq.tile.get echo lead, sq.tile.get
else: else:
echo lead, sq.camels echo lead, sq.camels
echo "\n" echo ""
proc preRoll(b: var Board) = proc setLeader(b: var Board) =
let colors = [cGreen, cYellow, cPurple, cBlue, cRed] b.leader = b[b.camels.max].camels[^1] # top camel in the last currently-occupied space
var rolls = [range[1..16]3, 2, 3, 2, 1]
for (color, roll) in zip(colors, rolls):
let dest = roll + 1 proc setState[T](b: var Board, state: T) =
for (color, dest) in state: # note that `state` is ordered, as this determines stacking
b[dest].camels.add(color) b[dest].camels.add(color)
b.camels[color] = dest b.camels[color] = dest
b.setLeader
proc advance(b: var Board, die: Die) = proc advance(b: var Board, die: Die) =
@ -52,6 +56,11 @@ proc advance(b: var Board, die: Die) =
startPos = b.camels[color] startPos = b.camels[color]
var prepend = false var prepend = false
var endPos = startPos + roll var endPos = startPos + roll
if endPos > 16: # camel has passed the finish line
b.leader = color
b.gameOver = true
return
if b[endPos].tile.isSome: if b[endPos].tile.isSome:
endPos += int(b[endPos].tile.get) endPos += int(b[endPos].tile.get)
if b[endPos].tile.get == tBackward: prepend = true # if moving backward, we will prepend camel to the seq if b[endPos].tile.get == tBackward: prepend = true # if moving backward, we will prepend camel to the seq
@ -67,13 +76,12 @@ proc advance(b: var Board, die: Die) =
b[startPos].camels[i .. ^1] = @[] b[startPos].camels[i .. ^1] = @[]
for moved in subStack: for moved in subStack:
b.camels[moved] = endPos b.camels[moved] = endPos
b.leader = b[b.camels.max][^1] # top camel in the last currently-occupied space b.setLeader
break # breaking the outer loop here, not the inner - but only conditionally! gah!
break # breaking the outer loop here, not the inner
iterator allPermutations[T](x: seq[T]): seq[T] = iterator allPermutations[T](x: seq[T]): seq[T] =
# returns all permutations of a given array. Order is wonky but we don't care. # returns all permutations of a given seq. Order is wonky but we don't care.
var workingCopy = x var workingCopy = x
yield workingCopy yield workingCopy
while workingCopy.nextPermutation: # this mutates workingCopy while workingCopy.nextPermutation: # this mutates workingCopy
@ -83,9 +91,53 @@ iterator allPermutations[T](x: seq[T]): seq[T] =
yield workingCopy yield workingCopy
proc update(scores: var ScoreSet, toAdd: ScoreSet) =
for i, s in toAdd:
scores[i] += s
proc simOrdering(b: Board, ordering: seq[Color]): ScoreSet =
for color in ordering:
for roll in 1..3:
let d = (color, range[1..3](roll))
let nextBoardState = b.dup(advance(d)) # make a copy instead of mutating
# only continue recursing if this is not the last die AND the game is not over
if ordering.len > 1 and not nextBoardState.gameOver:
let nextResult = simOrdering(nextBoardState, ordering[1 .. ^1])
result.update(nextResult)
else:
inc result[nextBoardState.leader]
iterator possibleFutures(dice: seq[Color]): seq[Die]
# iterate over all possible sequences of die rolls. Each outcome
# is returned as a 5-sequence of (color, number) tuples.
yield (cRed, range[1..3]3)
proc sumLegResults(b: Board, diceRemaining: seq[Color]): ScoreSet =
var count = 0
for perm in diceRemaining.allPermutations:
let permResult = simOrdering(b, perm)
result.update(permResult)
let total = result.sum
stdout.write("simulated: " & $total & "\r")
stdout.flushFile
count += 1
echo ""
echo count
var b: Board var b: Board
b.display(1, 5) b.display(1, 5)
b.preRoll
let c = b.dup(advance((cRed, range[1..3]3))) b.setState({cGreen: 4, cYellow: 3, cPurple: 4, cBlue: 3, cRed: 2})
b.display(1, 5) b.display(1, 5)
c.display(1, 5)
b.advance((cRed, range[1..3](2)))
b.display(1, 5)
let r = b.sumLegResults(@[cRed, cGreen, cBlue, cYellow, cPurple])
let total = r.sum
for i, c in r:
echo Color(i), ": ", (c / total).round(4) * 100, "% (", c, " / ", total, ")"