diff --git a/main.nim b/main.nim index 199cd40..a301253 100644 --- a/main.nim +++ b/main.nim @@ -5,6 +5,10 @@ import combinators, game, fixedseq type ScoreSet* = array[Color, int] + ScoreSpread = object + lo: array[Color, float] + hi: array[Color, float] + LegResults* = tuple[scores: ScoreSet, endStates: HashSet[Board]] @@ -69,9 +73,24 @@ proc randomGames(b: Board, count: SomeInteger): ScoreSet = for i in 1 .. count: let winner = b.randomGame(r) inc result[winner] - if i mod 100_000 == 0 or i == count - 1: - stdout.write("simulated: " & $i & "\r") - echo "" + # if i mod 100_000 == 0 or i == count - 1: + # stdout.write("simulating " & count & "random games: " & $i & "\r") + # echo "" + + +proc randomSpread(b: Board, nTests: SomeInteger, nSamples: SomeInteger): ScoreSpread = + for s in result.lo.mitems: + s = 1 + + for i in 0 ..< nTests: + let scores = b.randomGames(nSamples) + let total = scores.sum + for color, score in scores: + let pct = score / total + if pct < result.lo[color]: + result.lo[color] = pct + if pct > result.hi[color]: + result.hi[color] = pct var b: Board @@ -85,6 +104,7 @@ for i, roll in randomFuture(b.diceRemaining, r): rolls[i] = (roll[0], roll[1] + 1) b.setState(rolls, @[]) +echo "Starting state:" b.display(1, 5) # block outer: @@ -99,7 +119,16 @@ b.display(1, 5) # echo "winner: ", b.leader.get # b.display(min(b.camels) - 1, 16) -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, ")" +# 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, ")" + +for i in 0..3: + let samples = 10 ^ (i + 3) + echo "Simulating ", samples, " games 100 times" + let spread = b.randomSpread(100, samples) + for color, lo in spread.lo: + stdout.write($color & ": " & $lo.round(4) & "-" & $spread.hi[color].round(4) & ", ") + stdout.flushFile + echo "\n"