2 Commits

Author SHA1 Message Date
38e9e23dea no need to set winner until game is over 2021-07-19 13:21:14 -07:00
94c4240d63 some improvements to benchmark script 2021-07-19 11:08:13 -07:00
4 changed files with 51 additions and 47 deletions

View File

@ -57,7 +57,7 @@ type
squares*: array[1..16, Square] squares*: array[1..16, Square]
camels*: array[Color, range[1..16]] camels*: array[Color, range[1..16]]
diceRolled*: array[Color, bool] diceRolled*: array[Color, bool]
leader*: Option[Color] winner*: Option[Color]
gameOver*: bool gameOver*: bool
initialized: bool initialized: bool
@ -66,6 +66,10 @@ 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
@ -87,6 +91,11 @@ 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]
@ -108,9 +117,6 @@ proc setState*(b: var Board;
for (tile, dest) in tiles: for (tile, dest) in tiles:
b[dest].tile = some(tile) 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 = proc diceRemaining*(b: Board): ColorStack =
result.initFixedSeq result.initFixedSeq
@ -119,7 +125,7 @@ proc diceRemaining*(b: Board): ColorStack =
proc resetDice*(b: var Board) = proc resetDice*(b: var Board) =
for c, rolled in b.diceRolled: for c in Color:
b.diceRolled[c] = false b.diceRolled[c] = false
@ -130,7 +136,7 @@ proc advance*(b: var Board, die: Die) =
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.leader = some(b[startPos].camels[^1]) b.winner = some(b[startPos].camels[^1])
b.gameOver = true b.gameOver = true
return return
@ -154,8 +160,4 @@ proc advance*(b: var Board, die: Die) =
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]] = 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 b.diceRolled[color] = true

View File

@ -52,7 +52,7 @@ iterator legEndStates(b: Board): Board =
proc getLegScores*(b: Board): ScoreSet = proc getLegScores*(b: Board): ScoreSet =
for prediction in b.legEndStates: 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): for roll in randomFuture(projection.diceRemaining, r):
projection.advance(roll) projection.advance(roll)
if projection.gameOver: if projection.gameOver:
return projection.leader.get return projection.winner.get
projection.resetDice() projection.resetDice()

View File

@ -1,7 +1,32 @@
import math, random, strformat, times import math, random, strformat, times, std/monotimes
import fixedseq, game, simulation, ui import fixedseq, game, simulation, ui
type
TestResults = object
ops: int
time: Duration
proc summarize(tr: TestResults) =
let secs = tr.time.inMilliseconds.float / 1000
stdout.write("Test completed:\n")
stdout.write(" " & $tr.ops, " operations in " & $round(secs, 2) & " seconds\n")
stdout.write(" " & $round(tr.ops.float / secs, 2) & " operations per second")
stdout.flushFile()
template executionTime(body: untyped): Duration =
let start = getMonoTime()
body
getMonoTime() - start
proc getRand(): Rand =
randomize()
result = initRand(rand(int64))
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] = proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
for c in Color: for c in Color:
let v = r.rand(1..3) let v = r.rand(1..3)
@ -18,18 +43,15 @@ proc newRandomGame(r: var Rand): Board =
result.setState(dice, []) result.setState(dice, [])
proc testGames(n: SomeInteger = 100): auto = proc games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
var r = initRand(rand(int64)) var r = getRand()
let dice = randomDice(r) var scores: ScoreSet
var b: Board for i in 1 .. nTests:
b.init let b = newRandomGame(r)
b.setState(dice, []) let dur = executionTime:
b.display(1, 5) let s = b.randomGames(nSamples, parallel = parallel)
result.ops += s.sum()
let startTime = cpuTime() result.time += dur
let scores = b.randomGames(n)
result = cpuTime() - startTime
scores.display()
proc testLegs(n: Natural = 100): auto = proc testLegs(n: Natural = 100): auto =
@ -72,24 +94,4 @@ proc testSpread(nTests, nSamples: Natural) =
when isMainModule: when isMainModule:
randomize() games(10, 10_000_000).summarize()
var r = initRand(rand(int64))
let b = newRandomGame(r)
b.display(1, 5)
echo b.showSpaces(1, 16)
let scores = b.getLegScores
echo scores.showPercents
# let start_states = 2_000
# let executionTime = testLegs(start_states)
# echo "Execution time: ", executionTime
# echo "Leg simulations per second: ", float(start_states * 29_160) / executionTime
# for i in 1 .. 1:
# let num_games = 100_000_005
# let executionTime = testGames(num_games)
# echo "Execution time: ", executionTime
# echo "Full-game simulations per second: ", float(num_games) / executionTime
# echo ""
# testSpread(100, 1_000_000)