1 Commits

Author SHA1 Message Date
8abe9fdd63 store camel stack positions (also slower apparently) 2021-07-19 12:13:35 -07:00
3 changed files with 19 additions and 85 deletions

View File

@ -1,5 +1,5 @@
import algorithm, random, sugar import algorithm, random, sugar
import fastrand, fixedseq, game import fixedseq, game
proc nextPermutation(x: var FixedSeq): bool = proc nextPermutation(x: var FixedSeq): bool =
@ -93,4 +93,4 @@ proc randomFuture*(dice: FixedSeq, r: var Rand): FixedSeq[5, Die, int8] =
result.initFixedSeq result.initFixedSeq
let order = dice.dup(shuffle(r)) let order = dice.dup(shuffle(r))
for i, color in order: for i, color in order:
result.add((color, r.fastRand(1..3))) result.add((color, r.rand(1..3)))

View File

@ -1,74 +0,0 @@
import random, math
import times, std/monotimes, strformat, strutils
proc formatNum(n: SomeNumber): string =
let s = $(n.round)
let t = s[0 .. s.len - 3]
var count = 1
for i in countdown(t.high, 0):
result.insert($t[i], 0)
if count mod 3 == 0 and i != 0:
result.insert(",", 0)
count += 1
proc formatRate(n: Natural, d: Duration): string =
result = formatNum(1_000_000'f64 * n.float64 / d.inMicroseconds.float64)
const upperBound = uint64(uint32.high)
proc fastRand*[T: Natural](r: var Rand, x: T): T =
# Nim ranges are usually inclusive, but this algorithm is exclusive
let x = x.uint64 + 1
let num = if x <= upperBound:
((r.next shr 32) * x.uint64) shr 32
else:
r.next mod x.uint64
result = T(num)
proc fastRand*(r: var Rand; x, y: Natural): Natural =
let lim = (y - x)
result = fastRand(r, lim) + x
proc fastRand*[T](r: var Rand, slice: HSlice[T, T]): T =
let n = fastRand(r, slice.a.Natural, slice.b.Natural)
result = T(n)
proc testFastRand(num = 1_000_000_000): Duration =
var r = initRand(rand(int64))
let start = getMonoTime()
for i in 1 .. num:
discard r.fastRand(5)
result = getMonoTime() - start
# echo "fastrand execution rate: ", 1000 * num / dur.inMilliseconds.int, " generated per second."
proc testStdRand(num = 1_000_000_000): Duration =
var r = initRand(rand(int64))
let start = getMonoTime()
for i in 1 .. num:
discard r.rand(4)
result = getMonoTime() - start
# echo "std rand execution rate: ", 1000 * num / dur.inMilliseconds.int, " generated per second."
when isMainModule:
randomize()
var r = initRand(rand(int64))
let runs = 100_000_000
var totals: array[5..9, int]
for i in 1 .. runs:
let n = r.fastRand(5..9)
totals[n] += 1
echo totals
# let fr = testFastRand(runs)
# echo "fastrand execution rate: ", formatNum(1_000_000 * runs / fr.inMicroseconds.int)
# let sr = testStdRand(runs)
# echo "standard execution rate: ", formatNum(1_000_000 * runs / sr.inMicroseconds.int)

View File

@ -53,9 +53,13 @@ type
camels*: ColorStack camels*: ColorStack
tile*: Option[Tile] tile*: Option[Tile]
CamelPos* = object
square*: range[1..16]
stackIdx*: int8
Board* = object Board* = object
squares*: array[1..16, Square] squares*: array[1..16, Square]
camels*: array[Color, range[1..16]] camels*: array[Color, CamelPos]
diceRolled*: array[Color, bool] diceRolled*: array[Color, bool]
leader*: Option[Color] leader*: Option[Color]
gameOver*: bool gameOver*: bool
@ -103,13 +107,17 @@ proc setState*(b: var Board;
tiles: openArray[tuple[t: Tile, p: int]]) = tiles: openArray[tuple[t: Tile, p: int]]) =
for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking
b[dest].camels.add(color) b[dest].camels.add(color)
b.camels[color] = dest let height = b[dest].camels.high
b.camels[color] = CamelPos(square: dest, stackIdx: height)
for (tile, dest) in tiles: for (tile, dest) in tiles:
b[dest].tile = some(tile) b[dest].tile = some(tile)
let leadCamel = b[max(b.camels)].camels[^1] # top camel in the last currently-occupied space for sq in b.squares:
b.leader = some(leadCamel) if sq.camels.len > 0:
let squareLeader = sq.camels[^1]
if b.leader.isNone or b.leader.get != squareLeader:
b.leader = some(squareLeader)
proc diceRemaining*(b: Board): ColorStack = proc diceRemaining*(b: Board): ColorStack =
@ -126,7 +134,7 @@ proc resetDice*(b: var Board) =
proc advance*(b: var Board, die: Die) = proc advance*(b: var Board, die: Die) =
let let
(color, roll) = die (color, roll) = die
startPos = b.camels[color] startPos = b.camels[color].square
var endPos = startPos + roll var endPos = startPos + roll
if endPos > 16: # camel has passed the finish line if endPos > 16: # camel has passed the finish line
@ -144,18 +152,18 @@ proc advance*(b: var Board, die: Die) =
if prepend: if prepend:
b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart) b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart)
let stackLen = b[startPos].camels.len - stackStart let stackLen = b[startPos].camels.len - stackStart
for i in 0 ..< stackLen: for i in 0'i8 ..< stackLen:
# we know how many camels we added to the bottom, so set the position for each of those # we know how many camels we added to the bottom, so set the position for each of those
b.camels[b[endPos].camels[i]] = endPos b.camels[b[endPos].camels[i]] = CamelPos(square: endPos, stackIdx: i) # replace with cast later?
else: else:
let dstPrevHigh = b[endPos].camels.high let dstPrevHigh = b[endPos].camels.high
b[startPos].camels.moveSubstack(b[endPos].camels, stackStart) b[startPos].camels.moveSubstack(b[endPos].camels, stackStart)
# the camels that have moved start immediately after the previous high camel # the camels that have moved start immediately after the previous high camel
for i in (dstPrevHigh + 1) .. b[endPos].camels.high: for i in (dstPrevHigh + 1) .. b[endPos].camels.high:
b.camels[b[endPos].camels[i]] = endPos b.camels[b[endPos].camels[i]] = CamelPos(square: endPos, stackIdx: i)
# if we are stacking on or moving past the previous leader # if we are stacking on or moving past the previous leader
if endPos >= b.camels[b.leader.get]: if endPos >= b.camels[b.leader.get].square:
b.leader = some(b[endPos].camels[^1]) b.leader = some(b[endPos].camels[^1])
b.diceRolled[color] = true b.diceRolled[color] = true