import hashes, options import fixedseq type Color* = enum cRed, cGreen, cBlue, cYellow, cPurple ColorStack* = FixedSeq[5, Color, int8] proc `$`*(s: ColorStack): string = result.add("St@[") for i, color in s: result.add($color) if i < s.high: result.add(", ") result.add("]") type Die* = tuple[color: Color, value: int] Tile* = enum tBackward = -1, tForward = 1 Square* = object camels: ColorStack tile: Option[Tile] Board* = object squares*: array[1..16, Square] camels*: array[Color, range[1..16]] diceRolled*: array[Color, bool] leader*: Option[Color] gameOver*: bool initialized: bool const allDice = @[cRed, cGreen, cBlue, cYellow, cPurple] 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.asInt: h = h !& c result = !$h proc init*(b: var Board) = for sq in b.squares.mitems: sq.camels.initFixedSeq b.initialized = true proc display*(b: Board, start, stop: int) = for i in start..stop: let sq = b.squares[i] let lead = $i & ": " if sq.tile.isSome: echo lead, sq.tile.get else: echo lead, sq.camels echo "" 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 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 resetDice*(b: var Board) = var d: array[Color, bool] b.diceRolled = d proc advance*(b: var Board, die: Die) = let (color, roll) = die startPos = b.camels[color] var endPos = startPos + roll if endPos > 16: # camel has passed the finish line b.leader = some(b[startPos].camels[^1]) b.gameOver = true return 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 let stackStart = b[startPos].camels.find(color) if prepend: b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart) let stackLen = b[startPos].camels.len - stackStart for i in 0 ..< stackLen: # we know how many camels we added to the bottom, so set the position for each of those b.camels[b[endPos].camels[i]] = endPos else: let dstPrevHigh = b[endPos].camels.high b[startPos].camels.moveSubstack(b[endPos].camels, stackStart) # the camels that have moved start immediately after the previous high camel for i in (dstPrevHigh + 1) .. b[endPos].camels.high: b.camels[b[endPos].camels[i]] = endPos # 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]) b.diceRolled[color] = true