From 38e9e23dea4a2203b520f34b8737a4d1f02d9124 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Mon, 19 Jul 2021 13:20:45 -0700 Subject: [PATCH] no need to set winner until game is over --- config.nims | 2 +- game.nim | 22 ++++++++++++---------- simulation.nim | 4 ++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/config.nims b/config.nims index c3b9fc0..cf6a8b0 100644 --- a/config.nims +++ b/config.nims @@ -2,4 +2,4 @@ --d: release --opt: speed --passC: -flto ---passL: -flto \ No newline at end of file +--passL: -flto diff --git a/game.nim b/game.nim index f6e8d8a..0cbfa8c 100644 --- a/game.nim +++ b/game.nim @@ -57,7 +57,7 @@ type squares*: array[1..16, Square] camels*: array[Color, range[1..16]] diceRolled*: array[Color, bool] - leader*: Option[Color] + winner*: Option[Color] gameOver*: bool initialized: bool @@ -66,6 +66,10 @@ type template `[]`*[T](b: var Board, idx: T): var Square = 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 = var h: Hash = 0 @@ -87,6 +91,11 @@ proc init*(b: var Board) = 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) = for i in start..stop: let sq = b.squares[i] @@ -108,9 +117,6 @@ proc setState*(b: var Board; 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 diceRemaining*(b: Board): ColorStack = result.initFixedSeq @@ -119,7 +125,7 @@ proc diceRemaining*(b: Board): ColorStack = proc resetDice*(b: var Board) = - for c, rolled in b.diceRolled: + for c in Color: b.diceRolled[c] = false @@ -130,7 +136,7 @@ proc advance*(b: var Board, die: Die) = var endPos = startPos + roll if endPos > 16: # camel has passed the finish line - b.leader = some(b[startPos].camels[^1]) + b.winner = some(b[startPos].camels[^1]) b.gameOver = true return @@ -154,8 +160,4 @@ proc advance*(b: var Board, die: Die) = 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 \ No newline at end of file diff --git a/simulation.nim b/simulation.nim index 96d6fc1..f67232b 100644 --- a/simulation.nim +++ b/simulation.nim @@ -52,7 +52,7 @@ iterator legEndStates(b: Board): Board = proc getLegScores*(b: Board): ScoreSet = for prediction in b.legEndStates: - inc result[prediction.leader.get] + inc result[prediction.winner.get] # ===================== @@ -65,7 +65,7 @@ proc randomGame*(b: Board, r: var Rand): Color = for roll in randomFuture(projection.diceRemaining, r): projection.advance(roll) if projection.gameOver: - return projection.leader.get + return projection.winner.get projection.resetDice()