diff --git a/main.nim b/main.nim index 326c536..e8273ca 100644 --- a/main.nim +++ b/main.nim @@ -1,4 +1,4 @@ -import math, options, tables, sequtils, sets, sugar +import math, hashes, options, tables, sequtils, sets import combinators @@ -7,8 +7,8 @@ type cRed, cGreen, cBlue, cYellow, cPurple Tile = enum - tForward = -1, - tBackward = 1 + tBackward = -1, + tForward = 1 Square = object camels: seq[Color] @@ -18,17 +18,41 @@ type ScoreSet = array[Color, int] + LegResults = tuple[scores: ScoreSet, endStates: HashSet[Board]] + Board = object squares: array[1..16, Square] camels: array[Color, range[1..16]] - leader: Color + leader: Option[Color] gameOver: bool +const allDice = @[cRed, cGreen, cBlue, cYellow, cPurple] + + +proc update(scores: var ScoreSet, toAdd: ScoreSet) = + for i, s in toAdd: + scores[i] += s + + proc `[]`[T](b: var Board, idx: T): var Square = b.squares[idx] +proc hash(b: Board): Hash = + var h: Hash = 0 + # there could be a tile anywhere so we have to check all squares + for i, sq in b.squares: + if sq.camels.len > 0 or sq.tile.isSome: + h = h !& i + if sq.tile.isSome: + h = h !& int(sq.tile.get) * 10 # so it isn't confused with a camel + else: + for c in sq.camels: + h = h !& int(c) + result = !$h + + proc display(b: Board, start, stop: int) = for i in start..stop: let sq = b.squares[i] @@ -40,31 +64,36 @@ proc display(b: Board, start, stop: int) = echo "" -proc setLeader(b: var Board) = - b.leader = b[b.camels.max].camels[^1] # top camel in the last currently-occupied space - - -proc setState[T](b: var Board, state: T) = - for (color, dest) in state: # note that `state` is ordered, as this determines stacking +proc setState(b: var Board; + camels: openArray[tuple[c: Color, p: int]]; + tiles: openArray[tuple[t: Tile, p: int]]) = + for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking b[dest].camels.add(color) b.camels[color] = dest - b.setLeader + + for (tile, dest) in tiles: + b[dest].tile = some(tile) + + let leadCamel = b[max(b.camels)].camels[^1] # top camel in the last currently-occupied space + b.leader = some(leadCamel) proc advance(b: var Board, die: Die) = let (color, roll) = die startPos = b.camels[color] - var prepend = false var endPos = startPos + roll if endPos > 16: # camel has passed the finish line - b.leader = color + b.leader = some(b[startPos].camels[^1]) b.gameOver = true return - if b[endPos].tile.isSome: - endPos += int(b[endPos].tile.get) - if b[endPos].tile.get == tBackward: prepend = true # if moving backward, we will prepend camel to the seq + + var prepend = false + if b[endPos].tile.isSome: # adjust position (and possibly stacking) to account for tile + let t = b[endPos].tile.get + endPos += int(t) + if t == tBackward: prepend = true for i, c in b[startPos].camels: if c == color: @@ -77,40 +106,50 @@ proc advance(b: var Board, die: Die) = b[startPos].camels[i .. ^1] = @[] for moved in subStack: b.camels[moved] = endPos - b.setLeader + # if we are stacking on or moving past the previous leader + if endPos >= b.camels[b.leader.get]: + b.leader = some(b[endPos].camels[^1]) break # breaking the outer loop here, not the inner - but only conditionally! gah! -type CamelPositions = seq[tuple[square: int, camels: seq[Color]]] - -proc simulateLeg(b: Board, diceRemaining: seq[Color]): ScoreSet = +proc simulateLeg(b: Board, diceRemaining: seq[Color]): LegResults = + var scores: ScoreSet var endStates: HashSet[Board] for future in possibleFutures(diceRemaining): var prediction = b # make a copy for dieRoll in future: prediction.advance(dieRoll) - inc result[prediction.leader] + inc scores[prediction.leader.get] # deduplicate results - endStates.incl(prediction) # need to add hash implementation for Board - #[ - var positions: CamelPositions - for i in prediction.camels.min .. prediction.camels.max: - positions.add((i, prediction.squares[i].camels)) - endStates.incl(positions) - ]# - echo "Distinct end states: ", endStates.len + endStates.incl(prediction) + result = (scores, endStates) + + +proc simulateGame(b: Board, diceRemaining: seq[Color]): ScoreSet = + # currently just simulates two legs, not a full game + var (scores, startStates) = b.simulateLeg(diceRemaining) + var endStates: HashSet[Board] + + for s in startStates: + let (nextScores, nextStates) = s.simulateLeg(@[cRed, cGreen, cBlue, cYellow, cPurple]) + scores.update(nextScores) + endStates.incl(nextStates) + stdout.write("simulated: " & $scores.sum & "\r") + + echo "\nDistinct end states: ", endStates.len + result = scores var b: Board 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: 2}, @[]) b.display(1, 5) b.advance((cRed, 2)) b.display(1, 5) -let r = b.simulateLeg(@[cRed, cGreen, cBlue, cYellow, cPurple]) +let r = b.simulateGame(allDice) let total = r.sum for i, c in r: echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")"