reuse dice for subsequent legs in randomGame

This commit is contained in:
Joseph Montanaro 2021-11-05 10:27:09 -07:00
parent d9efaf33e7
commit 63df46eee6
3 changed files with 12 additions and 12 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.exe *.exe
profile_results.txt profile_results.txt
callgrind.out.*

View File

@ -1,4 +1,4 @@
import algorithm, random, sugar import algorithm
import fixedseq, game import fixedseq, game
@ -87,10 +87,3 @@ iterator possibleFutures*(dice: FixedSeq): auto =
for i in 0 .. dice.high: for i in 0 .. dice.high:
f.add((perm[i], digits[i])) f.add((perm[i], digits[i]))
yield f 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)))

View File

@ -69,12 +69,18 @@ proc getLegScores*(b: Board): ScoreSet =
proc randomGame*(b: Board, r: var Rand): Color = proc randomGame*(b: Board, r: var Rand): Color =
var projection = b var projection = b
var dice = projection.diceRemaining
while true: while true:
for roll in randomFuture(projection.diceRemaining, r): dice.shuffle(r)
projection.advance(roll) for color in dice:
projection.advance((color, r.rand(1..3)))
if projection.gameOver: if projection.gameOver:
return projection.winner.get return projection.winner.get
# if we started with <5 dice, we need to reset for the next full leg
if dice.len < 5:
projection.resetDice() projection.resetDice()
dice = projection.diceRemaining
proc randomGamesWorker(b: Board, count: Natural, r: var Rand): ScoreSet = proc randomGamesWorker(b: Board, count: Natural, r: var Rand): ScoreSet =