96 lines
2.3 KiB
Nim
96 lines
2.3 KiB
Nim
import math, random, strformat, times
|
|
import fixedseq, game, simulation, ui
|
|
|
|
|
|
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
|
for c in Color:
|
|
let v = r.rand(1..3)
|
|
result.add((c, v))
|
|
result.shuffle
|
|
|
|
|
|
proc newRandomGame(r: var Rand): Board =
|
|
var dice: array[5, tuple[c: Color, p: int]]
|
|
for i in 0 .. 4:
|
|
dice[i] = (Color(i), r.rand(1..3))
|
|
|
|
result.init
|
|
result.setState(dice, [])
|
|
|
|
|
|
proc testGames(n: SomeInteger = 100): auto =
|
|
var r = initRand(rand(int64))
|
|
let dice = randomDice(r)
|
|
var b: Board
|
|
b.init
|
|
b.setState(dice, [])
|
|
b.display(1, 5)
|
|
|
|
let startTime = cpuTime()
|
|
let scores = b.randomGames(n)
|
|
result = cpuTime() - startTime
|
|
scores.display()
|
|
|
|
|
|
proc testLegs(n: Natural = 100): auto =
|
|
var boards: seq[Board]
|
|
var r = initRand(rand(int64))
|
|
for i in 1 .. n:
|
|
var b: Board
|
|
b.init
|
|
let dice = randomDice(r)
|
|
b.setState(dice, [])
|
|
boards.add(b)
|
|
stdout.write("Constructed: " & $i & "\r")
|
|
echo ""
|
|
|
|
echo "Running..."
|
|
let start = cpuTime()
|
|
for b in boards:
|
|
discard b.getLegScores
|
|
result = cpuTime() - start
|
|
|
|
|
|
proc testSpread(nTests, nSamples: Natural) =
|
|
var b: Board
|
|
b.init
|
|
var r = initRand(rand(int64))
|
|
let dice = randomDice(r)
|
|
b.setState(dice, [])
|
|
b.display(1, 5)
|
|
let spread = randomSpread(b, nTests, nSamples)
|
|
|
|
stdout.writeLine("Variance:")
|
|
for c in Color:
|
|
let variance = 100 * (spread.hi[c] - spread.lo[c])
|
|
stdout.writeLine(fmt"{c}: {round(variance, 2):.2f}%")
|
|
|
|
let diff = 100 * (max(spread.hi) - min(spread.lo))
|
|
stdout.writeLine(fmt"Win percentage differential: {round(diff, 2):.2f}%")
|
|
|
|
stdout.flushFile()
|
|
|
|
|
|
when isMainModule:
|
|
randomize()
|
|
var r = initRand(rand(int64))
|
|
let b = newRandomGame(r)
|
|
b.display(1, 5)
|
|
echo b.showSpaces(1, 16)
|
|
|
|
let scores = b.getLegScores
|
|
echo scores.showPercents
|
|
# let start_states = 2_000
|
|
# let executionTime = testLegs(start_states)
|
|
# echo "Execution time: ", executionTime
|
|
# echo "Leg simulations per second: ", float(start_states * 29_160) / executionTime
|
|
|
|
# for i in 1 .. 1:
|
|
# let num_games = 100_000_005
|
|
# let executionTime = testGames(num_games)
|
|
# echo "Execution time: ", executionTime
|
|
# echo "Full-game simulations per second: ", float(num_games) / executionTime
|
|
# echo ""
|
|
|
|
# testSpread(100, 1_000_000)
|