randomize starting state

This commit is contained in:
Joseph Montanaro 2021-03-24 18:14:57 -07:00
parent f05ab2cfa2
commit 239e99c79f
2 changed files with 26 additions and 19 deletions

View File

@ -9,7 +9,7 @@ type
ColorStack* = FixedSeq[5, Color, int8] ColorStack* = FixedSeq[5, Color, int8]
proc initColorStack: ColorStack = proc initColorStack*: ColorStack =
result.initFixedSeq result.initFixedSeq
@ -18,7 +18,7 @@ proc getAllColors: ColorStack =
for c in Color.low .. Color.high: for c in Color.low .. Color.high:
result[i] = c result[i] = c
const allColors = getAllColors() # compile-time evaluation const allColors* = getAllColors() # compile-time evaluation
proc `$`*(s: ColorStack): string = proc `$`*(s: ColorStack): string =

View File

@ -66,33 +66,40 @@ proc randomGame(b: Board, r: var Rand): Color =
proc randomGames(b: Board, count: SomeInteger): ScoreSet = proc randomGames(b: Board, count: SomeInteger): ScoreSet =
randomize() randomize()
var r = initRand(rand(int64)) var r = initRand(rand(int64))
for i in 0 ..< count: for i in 1 .. count:
let winner = b.randomGame(r) let winner = b.randomGame(r)
inc result[winner] inc result[winner]
if i mod 100_000 == 0: if i mod 100_000 == 0 or i == count - 1:
stdout.write("simulated: " & $i & "\r") stdout.write("simulated: " & $i & "\r")
echo "" echo ""
var b: Board var b: Board
b.init 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.display(1, 5)
b.setState({cGreen: 4, cYellow: 3, cPurple: 4, cBlue: 3, cRed: 5}, @[]) # block outer:
b.display(1, 5) # 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)) # echo "winner: ", b.leader.get
# b.display(1, 5) # b.display(min(b.camels) - 1, 16)
# var s: ColorStack let scores = b.randomGames(10_000_000)
# s.initFixedSeq let total = scores.sum
# for i in 0..4: for i, c in scores:
# 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:
echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")" echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")"