From 63df46eee68edd380386792381bebc4ba4cf0429 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Fri, 5 Nov 2021 10:27:09 -0700 Subject: [PATCH] reuse dice for subsequent legs in randomGame --- .gitignore | 3 ++- combinators.nim | 9 +-------- simulation.nim | 12 +++++++++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 4d8d0fc..1756b4b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.exe -profile_results.txt \ No newline at end of file +profile_results.txt +callgrind.out.* \ No newline at end of file diff --git a/combinators.nim b/combinators.nim index 903d1a7..57d9233 100644 --- a/combinators.nim +++ b/combinators.nim @@ -1,4 +1,4 @@ -import algorithm, random, sugar +import algorithm import fixedseq, game @@ -87,10 +87,3 @@ iterator possibleFutures*(dice: FixedSeq): auto = for i in 0 .. dice.high: f.add((perm[i], digits[i])) yield f - - -proc randomFuture*(dice: FixedSeq, r: var Rand): FixedSeq[5, Die, int8] = - result.initFixedSeq - let order = dice.dup(shuffle(r)) - for i, color in order: - result.add((color, r.rand(1..3))) diff --git a/simulation.nim b/simulation.nim index 38b4da6..425d718 100644 --- a/simulation.nim +++ b/simulation.nim @@ -69,12 +69,18 @@ proc getLegScores*(b: Board): ScoreSet = proc randomGame*(b: Board, r: var Rand): Color = var projection = b + var dice = projection.diceRemaining + while true: - for roll in randomFuture(projection.diceRemaining, r): - projection.advance(roll) + dice.shuffle(r) + for color in dice: + projection.advance((color, r.rand(1..3))) if projection.gameOver: return projection.winner.get - projection.resetDice() + # if we started with <5 dice, we need to reset for the next full leg + if dice.len < 5: + projection.resetDice() + dice = projection.diceRemaining proc randomGamesWorker(b: Board, count: Natural, r: var Rand): ScoreSet =