From 239e99c79fcbf637bc1fe91c827dd144c7648b32 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Wed, 24 Mar 2021 18:14:57 -0700 Subject: [PATCH] randomize starting state --- game.nim | 4 ++-- main.nim | 41 ++++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/game.nim b/game.nim index 693d796..ed5b2a9 100644 --- a/game.nim +++ b/game.nim @@ -9,7 +9,7 @@ type ColorStack* = FixedSeq[5, Color, int8] -proc initColorStack: ColorStack = +proc initColorStack*: ColorStack = result.initFixedSeq @@ -18,7 +18,7 @@ proc getAllColors: ColorStack = for c in Color.low .. Color.high: result[i] = c -const allColors = getAllColors() # compile-time evaluation +const allColors* = getAllColors() # compile-time evaluation proc `$`*(s: ColorStack): string = diff --git a/main.nim b/main.nim index 4d45357..199cd40 100644 --- a/main.nim +++ b/main.nim @@ -66,33 +66,40 @@ proc randomGame(b: Board, r: var Rand): Color = proc randomGames(b: Board, count: SomeInteger): ScoreSet = randomize() var r = initRand(rand(int64)) - for i in 0 ..< count: + for i in 1 .. count: let winner = b.randomGame(r) inc result[winner] - if i mod 100_000 == 0: + if i mod 100_000 == 0 or i == count - 1: stdout.write("simulated: " & $i & "\r") echo "" var b: Board b.init + +randomize() +var r = initRand(rand(int64)) + +var rolls: array[5, tuple[c: Color, p: int]] +for i, roll in randomFuture(b.diceRemaining, r): + rolls[i] = (roll[0], roll[1] + 1) + +b.setState(rolls, @[]) b.display(1, 5) -b.setState({cGreen: 4, cYellow: 3, cPurple: 4, cBlue: 3, cRed: 5}, @[]) -b.display(1, 5) +# block outer: +# while true: +# for roll in randomFuture(b.diceRemaining, r): +# b.advance(roll) +# if b.gameOver: +# echo "last roll: ", roll +# break outer +# b.resetDice -# b.advance((cRed, 1)) -# b.display(1, 5) +# echo "winner: ", b.leader.get +# b.display(min(b.camels) - 1, 16) -# var s: ColorStack -# s.initFixedSeq -# for i in 0..4: -# s.add(Color(i)) - -# echo s -# echo s[2] - -let r = b.randomGames(10_000_000) -let total = r.sum -for i, c in r: +let scores = b.randomGames(10_000_000) +let total = scores.sum +for i, c in scores: echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")"