|
|
@ -53,11 +53,15 @@ type
|
|
|
|
camels*: ColorStack
|
|
|
|
camels*: ColorStack
|
|
|
|
tile*: Option[Tile]
|
|
|
|
tile*: Option[Tile]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CamelPos* = object
|
|
|
|
|
|
|
|
square*: range[1..16]
|
|
|
|
|
|
|
|
stackIdx*: int8
|
|
|
|
|
|
|
|
|
|
|
|
Board* = object
|
|
|
|
Board* = object
|
|
|
|
squares*: array[1..16, Square]
|
|
|
|
squares*: array[1..16, Square]
|
|
|
|
camels*: array[Color, range[1..16]]
|
|
|
|
camels*: array[Color, CamelPos]
|
|
|
|
diceRolled*: array[Color, bool]
|
|
|
|
diceRolled*: array[Color, bool]
|
|
|
|
winner*: Option[Color]
|
|
|
|
leader*: Option[Color]
|
|
|
|
gameOver*: bool
|
|
|
|
gameOver*: bool
|
|
|
|
initialized: bool
|
|
|
|
initialized: bool
|
|
|
|
|
|
|
|
|
|
|
@ -66,10 +70,6 @@ type
|
|
|
|
template `[]`*[T](b: var Board, idx: T): var Square =
|
|
|
|
template `[]`*[T](b: var Board, idx: T): var Square =
|
|
|
|
b.squares[idx]
|
|
|
|
b.squares[idx]
|
|
|
|
|
|
|
|
|
|
|
|
# apparently we need separate ones for mutable and non-mutable
|
|
|
|
|
|
|
|
template `[]`*[T](b: Board, idx: T): Square =
|
|
|
|
|
|
|
|
b.squares[idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc hash*(b: Board): Hash =
|
|
|
|
proc hash*(b: Board): Hash =
|
|
|
|
var h: Hash = 0
|
|
|
|
var h: Hash = 0
|
|
|
@ -91,11 +91,6 @@ proc init*(b: var Board) =
|
|
|
|
b.initialized = true
|
|
|
|
b.initialized = true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc leader*(b: Board): Color =
|
|
|
|
|
|
|
|
let leadSquare = max(b.camels)
|
|
|
|
|
|
|
|
result = b[leadSquare].camels[^1]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc display*(b: Board, start, stop: int) =
|
|
|
|
proc display*(b: Board, start, stop: int) =
|
|
|
|
for i in start..stop:
|
|
|
|
for i in start..stop:
|
|
|
|
let sq = b.squares[i]
|
|
|
|
let sq = b.squares[i]
|
|
|
@ -112,11 +107,18 @@ proc setState*(b: var Board;
|
|
|
|
tiles: openArray[tuple[t: Tile, p: int]]) =
|
|
|
|
tiles: openArray[tuple[t: Tile, p: int]]) =
|
|
|
|
for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking
|
|
|
|
for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking
|
|
|
|
b[dest].camels.add(color)
|
|
|
|
b[dest].camels.add(color)
|
|
|
|
b.camels[color] = dest
|
|
|
|
let height = b[dest].camels.high
|
|
|
|
|
|
|
|
b.camels[color] = CamelPos(square: dest, stackIdx: height)
|
|
|
|
|
|
|
|
|
|
|
|
for (tile, dest) in tiles:
|
|
|
|
for (tile, dest) in tiles:
|
|
|
|
b[dest].tile = some(tile)
|
|
|
|
b[dest].tile = some(tile)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for sq in b.squares:
|
|
|
|
|
|
|
|
if sq.camels.len > 0:
|
|
|
|
|
|
|
|
let squareLeader = sq.camels[^1]
|
|
|
|
|
|
|
|
if b.leader.isNone or b.leader.get != squareLeader:
|
|
|
|
|
|
|
|
b.leader = some(squareLeader)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc diceRemaining*(b: Board): ColorStack =
|
|
|
|
proc diceRemaining*(b: Board): ColorStack =
|
|
|
|
result.initFixedSeq
|
|
|
|
result.initFixedSeq
|
|
|
@ -125,18 +127,18 @@ proc diceRemaining*(b: Board): ColorStack =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc resetDice*(b: var Board) =
|
|
|
|
proc resetDice*(b: var Board) =
|
|
|
|
for c in Color:
|
|
|
|
for c, rolled in b.diceRolled:
|
|
|
|
b.diceRolled[c] = false
|
|
|
|
b.diceRolled[c] = false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
proc advance*(b: var Board, die: Die) =
|
|
|
|
proc advance*(b: var Board, die: Die) =
|
|
|
|
let
|
|
|
|
let
|
|
|
|
(color, roll) = die
|
|
|
|
(color, roll) = die
|
|
|
|
startPos = b.camels[color]
|
|
|
|
startPos = b.camels[color].square
|
|
|
|
var endPos = startPos + roll
|
|
|
|
var endPos = startPos + roll
|
|
|
|
|
|
|
|
|
|
|
|
if endPos > 16: # camel has passed the finish line
|
|
|
|
if endPos > 16: # camel has passed the finish line
|
|
|
|
b.winner = some(b[startPos].camels[^1])
|
|
|
|
b.leader = some(b[startPos].camels[^1])
|
|
|
|
b.gameOver = true
|
|
|
|
b.gameOver = true
|
|
|
|
return
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
@ -150,14 +152,18 @@ proc advance*(b: var Board, die: Die) =
|
|
|
|
if prepend:
|
|
|
|
if prepend:
|
|
|
|
b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart)
|
|
|
|
b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart)
|
|
|
|
let stackLen = b[startPos].camels.len - stackStart
|
|
|
|
let stackLen = b[startPos].camels.len - stackStart
|
|
|
|
for i in 0 ..< stackLen:
|
|
|
|
for i in 0'i8 ..< stackLen:
|
|
|
|
# we know how many camels we added to the bottom, so set the position for each of those
|
|
|
|
# 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
|
|
|
|
b.camels[b[endPos].camels[i]] = CamelPos(square: endPos, stackIdx: i) # replace with cast later?
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
let dstPrevHigh = b[endPos].camels.high
|
|
|
|
let dstPrevHigh = b[endPos].camels.high
|
|
|
|
b[startPos].camels.moveSubstack(b[endPos].camels, stackStart)
|
|
|
|
b[startPos].camels.moveSubstack(b[endPos].camels, stackStart)
|
|
|
|
# the camels that have moved start immediately after the previous high camel
|
|
|
|
# the camels that have moved start immediately after the previous high camel
|
|
|
|
for i in (dstPrevHigh + 1) .. b[endPos].camels.high:
|
|
|
|
for i in (dstPrevHigh + 1) .. b[endPos].camels.high:
|
|
|
|
b.camels[b[endPos].camels[i]] = endPos
|
|
|
|
b.camels[b[endPos].camels[i]] = CamelPos(square: endPos, stackIdx: i)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# if we are stacking on or moving past the previous leader
|
|
|
|
|
|
|
|
if endPos >= b.camels[b.leader.get].square:
|
|
|
|
|
|
|
|
b.leader = some(b[endPos].camels[^1])
|
|
|
|
|
|
|
|
|
|
|
|
b.diceRolled[color] = true
|
|
|
|
b.diceRolled[color] = true
|