Compare commits
	
		
			3 Commits
		
	
	
		
			20d6022828
			...
			camelpos
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					8abe9fdd63 | ||
| 
						 | 
					94c4240d63 | ||
| 37991656b9 | 
@@ -4,6 +4,7 @@ This tool calculates probable outcomes for the board game CamelUp.
 | 
			
		||||
It can calculate all possible outcomes for a single game leg in about 5ms, so effectively instantaneously.
 | 
			
		||||
Full-game calculations take a little bit longer and are not exact (since it isn't practical to simulate all possible full game states.)
 | 
			
		||||
However it can easily simulate a million random games in about 80ms in the worst case, which should provide estimates accurate to within about 0.2%.
 | 
			
		||||
(Numbers from running on a Ryzen 3700X.)
 | 
			
		||||
 | 
			
		||||
```
 | 
			
		||||
Usage: 
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										5
									
								
								config.nims
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								config.nims
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,5 @@
 | 
			
		||||
--threads: on
 | 
			
		||||
--d: release
 | 
			
		||||
--opt: speed
 | 
			
		||||
--passC: -flto
 | 
			
		||||
--passL: -flto
 | 
			
		||||
							
								
								
									
										11
									
								
								cup.nim
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								cup.nim
									
									
									
									
									
								
							@@ -1,5 +1,4 @@
 | 
			
		||||
import math, options, sequtils, random, sets
 | 
			
		||||
import combinators, game, fixedseq, simulation, ui
 | 
			
		||||
import game, simulation, ui
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
when isMainModule:
 | 
			
		||||
@@ -8,11 +7,11 @@ when isMainModule:
 | 
			
		||||
  b.init
 | 
			
		||||
  b.setState(config.state, [])
 | 
			
		||||
  b.diceRolled = config.diceRolled
 | 
			
		||||
  b.display(1, 5)
 | 
			
		||||
  echo b.showSpaces(1, 16)
 | 
			
		||||
  let legScores = b.getLegScores
 | 
			
		||||
  echo "Current leg probabilities:"
 | 
			
		||||
  legScores.display
 | 
			
		||||
  echo "\nCurrent leg probabilities:"
 | 
			
		||||
  echo legScores.showPercents()
 | 
			
		||||
  
 | 
			
		||||
  let gameScores = b.randomGames(1_000_000)
 | 
			
		||||
  echo "\nFull game probabilities (1M simulations):"
 | 
			
		||||
  gameScores.display
 | 
			
		||||
  echo gameScores.showPercents()
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										26
									
								
								game.nim
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								game.nim
									
									
									
									
									
								
							@@ -53,9 +53,13 @@ type
 | 
			
		||||
    camels*: ColorStack
 | 
			
		||||
    tile*: Option[Tile]
 | 
			
		||||
 | 
			
		||||
  CamelPos* = object
 | 
			
		||||
    square*: range[1..16]
 | 
			
		||||
    stackIdx*: int8
 | 
			
		||||
 | 
			
		||||
  Board* = object
 | 
			
		||||
    squares*: array[1..16, Square]
 | 
			
		||||
    camels*: array[Color, range[1..16]]
 | 
			
		||||
    camels*: array[Color, CamelPos]
 | 
			
		||||
    diceRolled*: array[Color, bool]
 | 
			
		||||
    leader*: Option[Color]
 | 
			
		||||
    gameOver*: bool
 | 
			
		||||
@@ -103,13 +107,17 @@ proc setState*(b: var Board;
 | 
			
		||||
               tiles: openArray[tuple[t: Tile, p: int]]) =
 | 
			
		||||
  for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking
 | 
			
		||||
    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:
 | 
			
		||||
    b[dest].tile = some(tile)
 | 
			
		||||
 | 
			
		||||
  let leadCamel = b[max(b.camels)].camels[^1] # top camel in the last currently-occupied space
 | 
			
		||||
  b.leader = some(leadCamel)
 | 
			
		||||
  for sq in b.squares:
 | 
			
		||||
    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 =
 | 
			
		||||
@@ -126,7 +134,7 @@ proc resetDice*(b: var Board) =
 | 
			
		||||
proc advance*(b: var Board, die: Die) =
 | 
			
		||||
  let
 | 
			
		||||
    (color, roll) = die
 | 
			
		||||
    startPos = b.camels[color]
 | 
			
		||||
    startPos = b.camels[color].square
 | 
			
		||||
  var endPos = startPos + roll
 | 
			
		||||
  
 | 
			
		||||
  if endPos > 16: # camel has passed the finish line
 | 
			
		||||
@@ -144,18 +152,18 @@ proc advance*(b: var Board, die: Die) =
 | 
			
		||||
  if prepend:
 | 
			
		||||
    b[startPos].camels.moveSubstackPre(b[endPos].camels, 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
 | 
			
		||||
      b.camels[b[endPos].camels[i]] = endPos
 | 
			
		||||
      b.camels[b[endPos].camels[i]] = CamelPos(square: endPos, stackIdx: i) # replace with cast later?
 | 
			
		||||
  else:
 | 
			
		||||
    let dstPrevHigh = b[endPos].camels.high
 | 
			
		||||
    b[startPos].camels.moveSubstack(b[endPos].camels, stackStart)
 | 
			
		||||
    # the camels that have moved start immediately after the previous high camel
 | 
			
		||||
    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 endPos >= b.camels[b.leader.get]:
 | 
			
		||||
  if endPos >= b.camels[b.leader.get].square:
 | 
			
		||||
    b.leader = some(b[endPos].camels[^1])
 | 
			
		||||
  
 | 
			
		||||
  b.diceRolled[color] = true
 | 
			
		||||
							
								
								
									
										70
									
								
								test.nim
									
									
									
									
									
								
							
							
						
						
									
										70
									
								
								test.nim
									
									
									
									
									
								
							@@ -1,7 +1,32 @@
 | 
			
		||||
import math, random, strformat, times
 | 
			
		||||
import math, random, strformat, times, std/monotimes
 | 
			
		||||
import fixedseq, game, simulation, ui
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
type
 | 
			
		||||
  TestResults = object
 | 
			
		||||
    ops: int
 | 
			
		||||
    time: Duration
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
proc summarize(tr: TestResults) =
 | 
			
		||||
  let secs = tr.time.inMilliseconds.float / 1000
 | 
			
		||||
  stdout.write("Test completed:\n")
 | 
			
		||||
  stdout.write("  " & $tr.ops, " operations in " & $round(secs, 2) & " seconds\n")
 | 
			
		||||
  stdout.write("  " & $round(tr.ops.float / secs, 2) & " operations per second")
 | 
			
		||||
  stdout.flushFile()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
template executionTime(body: untyped): Duration =
 | 
			
		||||
  let start = getMonoTime()
 | 
			
		||||
  body
 | 
			
		||||
  getMonoTime() - start
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
proc getRand(): Rand =
 | 
			
		||||
  randomize()
 | 
			
		||||
  result = initRand(rand(int64))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] = 
 | 
			
		||||
  for c in Color:
 | 
			
		||||
    let v = r.rand(1..3)
 | 
			
		||||
@@ -18,18 +43,15 @@ proc newRandomGame(r: var Rand): Board =
 | 
			
		||||
  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 games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
 | 
			
		||||
  var r = getRand()
 | 
			
		||||
  var scores: ScoreSet
 | 
			
		||||
  for i in 1 .. nTests:
 | 
			
		||||
    let b = newRandomGame(r)
 | 
			
		||||
    let dur = executionTime:
 | 
			
		||||
        let s = b.randomGames(nSamples, parallel = parallel)
 | 
			
		||||
    result.ops += s.sum()
 | 
			
		||||
    result.time += dur
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
proc testLegs(n: Natural = 100): auto =
 | 
			
		||||
@@ -72,24 +94,4 @@ proc testSpread(nTests, nSamples: Natural) =
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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)
 | 
			
		||||
  games(10, 10_000_000).summarize()
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user