Compare commits
	
		
			3 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					f2e0698608 | ||
| cade17a9a6 | |||
| 
						 | 
					94c4240d63 | 
@@ -1,5 +1,5 @@
 | 
				
			|||||||
import algorithm, random, sugar
 | 
					import algorithm, random, sugar
 | 
				
			||||||
import faststack, fixedseq, game
 | 
					import fastrand, 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.rand(1..3)))
 | 
					    result.add((color, r.fastRand(1..3)))
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										10
									
								
								config.nims
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								config.nims
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
				
			|||||||
# --threads: on
 | 
					--threads: on
 | 
				
			||||||
# --d: release
 | 
					--d: release
 | 
				
			||||||
# --opt: speed
 | 
					--opt: speed
 | 
				
			||||||
# --passC: -flto
 | 
					--passC: -flto
 | 
				
			||||||
# --passL: -flto
 | 
					--passL: -flto
 | 
				
			||||||
							
								
								
									
										74
									
								
								fastrand.nim
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										74
									
								
								fastrand.nim
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,74 @@
 | 
				
			|||||||
 | 
					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)
 | 
				
			||||||
							
								
								
									
										204
									
								
								faststack.nim
									
									
									
									
									
								
							
							
						
						
									
										204
									
								
								faststack.nim
									
									
									
									
									
								
							@@ -1,204 +0,0 @@
 | 
				
			|||||||
import bitops, strutils, random
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc show(i: SomeInteger, bitlength = 16) =
 | 
					 | 
				
			||||||
  echo BiggestInt(i).toBin(bitlength)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type
 | 
					 | 
				
			||||||
  Color* = enum
 | 
					 | 
				
			||||||
    cRed, cGreen, cBlue, cYellow, cPurple
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  ColorStack* = object
 | 
					 | 
				
			||||||
    data: uint16
 | 
					 | 
				
			||||||
    len*: uint8
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const
 | 
					 | 
				
			||||||
  masks = [
 | 
					 | 
				
			||||||
    0'u16, # dummy value just to get the indices right
 | 
					 | 
				
			||||||
    0b0_000_000_000_000_111,
 | 
					 | 
				
			||||||
    0b0_000_000_000_111_111,
 | 
					 | 
				
			||||||
    0b0_000_000_111_111_111,
 | 
					 | 
				
			||||||
    0b0_000_111_111_111_111,
 | 
					 | 
				
			||||||
    0b0_111_111_111_111_111,
 | 
					 | 
				
			||||||
  ]
 | 
					 | 
				
			||||||
  allColors* = ColorStack(len: 5, data: 0b0_000_001_010_011_100)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
template offset(s: ColorStack, idx: Natural): uint8 =
 | 
					 | 
				
			||||||
  # Compute the bit offset for a given index. 
 | 
					 | 
				
			||||||
  # Dependent on the stack's length.
 | 
					 | 
				
			||||||
  (s.len - 1 - idx.uint8) * 3
 | 
					 | 
				
			||||||
  # items are stored from left to right but right-aligned, so we
 | 
					 | 
				
			||||||
  # need to shift right to access anything other than the last
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
template offset(s: ColorStack, idx: BackwardsIndex): uint8 =
 | 
					 | 
				
			||||||
  # for backwards index, we are still shifting right but
 | 
					 | 
				
			||||||
  # the lower the index the less we have to shift
 | 
					 | 
				
			||||||
  (idx.uint8 - 1) * 3
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc add*(s: var ColorStack, c: Color) =
 | 
					 | 
				
			||||||
  # e.g. if stack is 0b0000000000000100:
 | 
					 | 
				
			||||||
  # and color is 0b00000011
 | 
					 | 
				
			||||||
  # shift: 0b0000000000100000
 | 
					 | 
				
			||||||
  # bitor: 0b0000000000100000 and 0b00000011
 | 
					 | 
				
			||||||
  # results in 0b0000000000100011
 | 
					 | 
				
			||||||
  s.data = (s.data shl 3).bitor(cast[uint8](c))
 | 
					 | 
				
			||||||
  inc s.len
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc high*(s: ColorStack): uint8 = s.len - 1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc low*(s: ColorStack): uint8 = 0 # just... always 0, I guess
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc `[]`*(s: ColorStack, i: uint8 | BackwardsIndex): Color =
 | 
					 | 
				
			||||||
  # shift, then mask everything but the three rightmost bits
 | 
					 | 
				
			||||||
  result = Color(
 | 
					 | 
				
			||||||
    (s.data shr s.offset(i)) and masks[1]
 | 
					 | 
				
			||||||
  )
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc `[]=`*(s: var ColorStack, i: uint8 | BackwardsIndex, c: Color) =
 | 
					 | 
				
			||||||
  let offset = s.offset(i)
 | 
					 | 
				
			||||||
  s.data = (s.data and bitnot(masks[1] shl offset)) or (c.uint16 shl offset)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
iterator items*(s: ColorStack): Color =
 | 
					 | 
				
			||||||
  # s.len is unsigned so it will wrap around if we do s.len - 1 in that case
 | 
					 | 
				
			||||||
  if s.len != 0:
 | 
					 | 
				
			||||||
    for i in countdown(s.len - 1, 0'u8):
 | 
					 | 
				
			||||||
      yield Color((s.data shr (i * 3)) and masks[1])
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
iterator pairs*(s: ColorStack): (uint8, Color) =
 | 
					 | 
				
			||||||
  var count = 0'u8
 | 
					 | 
				
			||||||
  for color in s:
 | 
					 | 
				
			||||||
    yield (count, color)
 | 
					 | 
				
			||||||
    inc count
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc find*(s: ColorStack, needle: Color): int8 =
 | 
					 | 
				
			||||||
  for i in 0'u8 .. s.high:
 | 
					 | 
				
			||||||
    if s[i] == needle:
 | 
					 | 
				
			||||||
      return i.int8
 | 
					 | 
				
			||||||
  return -1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc moveSubstack*(src, dst: var ColorStack, startIdx: uint8) =
 | 
					 | 
				
			||||||
  if startIdx >= src.len:
 | 
					 | 
				
			||||||
    raise newException(IndexDefect, "index " & $startIdx & " is out of bounds.")
 | 
					 | 
				
			||||||
  # Moves a sub-stack from the top of src to the top of dst
 | 
					 | 
				
			||||||
  # shift the dst stack by the length of the substack to make room
 | 
					 | 
				
			||||||
  let nToMove = src.len - startIdx
 | 
					 | 
				
			||||||
  let shift = nToMove * 3
 | 
					 | 
				
			||||||
  dst.data = dst.data shl shift
 | 
					 | 
				
			||||||
  # then we mask the source data to present only the items 
 | 
					 | 
				
			||||||
  # being moved, and OR that with the shifted dst data
 | 
					 | 
				
			||||||
  dst.data = dst.data or (src.data and masks[nToMove])
 | 
					 | 
				
			||||||
  dst.len += nToMove
 | 
					 | 
				
			||||||
  # then we shift the source to get rid of the moved items
 | 
					 | 
				
			||||||
  src.data = src.data shr shift
 | 
					 | 
				
			||||||
  src.len -= nToMove
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc moveSubstackPre*(src, dst: var ColorStack, startIdx: uint8) =
 | 
					 | 
				
			||||||
  if startIdx >= src.len:
 | 
					 | 
				
			||||||
    raise newException(IndexDefect, "index " & $startIdx & " is out of bounds.")
 | 
					 | 
				
			||||||
  # Moves a sub-stack from the top of src to the bottom of dst
 | 
					 | 
				
			||||||
  let nToMove = src.len - startIdx
 | 
					 | 
				
			||||||
  # shift src to position the substack above its destination,
 | 
					 | 
				
			||||||
  # get rid of everything to the left of the substack,
 | 
					 | 
				
			||||||
  # and OR that with the existing dst data
 | 
					 | 
				
			||||||
  let newLen = dst.len + nToMove
 | 
					 | 
				
			||||||
  dst.data = dst.data or ( (src.data shl (dst.len * 3)) and masks[newLen] )
 | 
					 | 
				
			||||||
  dst.len = newLen
 | 
					 | 
				
			||||||
  # get rid of the substack we just moved
 | 
					 | 
				
			||||||
  src.data = src.data shr (nToMove * 3)
 | 
					 | 
				
			||||||
  src.len -= nToMove
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc swap*(s: var ColorStack, i1, i2: uint8) =
 | 
					 | 
				
			||||||
  # Swap the values at two indices in the stack
 | 
					 | 
				
			||||||
  if i1 == i2: return
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  # i1 and i2 are unsigned, so we have to watch out for underflows
 | 
					 | 
				
			||||||
  let diff = if i1 > i2:
 | 
					 | 
				
			||||||
    (i1 - i2) * 3
 | 
					 | 
				
			||||||
  else:
 | 
					 | 
				
			||||||
    (i2 - i1) * 3
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  # take masks[1] from above (rightmost position) and shift to position of i1.
 | 
					 | 
				
			||||||
  # then do the same for i2, and OR them together.
 | 
					 | 
				
			||||||
  let mask = (masks[1] shl s.offset(i1)) or (masks[1] shl s.offset(i2))
 | 
					 | 
				
			||||||
  # get rid of everything but the two values we're swapping
 | 
					 | 
				
			||||||
  let masked = s.data and mask
 | 
					 | 
				
			||||||
  # shift by the distance between values in both directions, combine, then mask
 | 
					 | 
				
			||||||
  let swapped = ((masked shl diff) or (masked shr diff)) and mask
 | 
					 | 
				
			||||||
  # finally, AND with the inverse of mask so that only the values being 
 | 
					 | 
				
			||||||
  # swapped are erased, and combine that with the swapped values
 | 
					 | 
				
			||||||
  s.data = (s.data and mask.bitnot) or swapped
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc shuffle*(r: var Rand, s: var ColorStack) =
 | 
					 | 
				
			||||||
  # Fisher-Yates shuffle
 | 
					 | 
				
			||||||
  for i in countdown(s.high, 1'u8):
 | 
					 | 
				
			||||||
    let j = r.rand(i).uint8
 | 
					 | 
				
			||||||
    if j != i:
 | 
					 | 
				
			||||||
      s.swap(i, j)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc reverse*(s: var ColorStack, first, last: uint8) =
 | 
					 | 
				
			||||||
  var x = first
 | 
					 | 
				
			||||||
  var y = last
 | 
					 | 
				
			||||||
  while x < y:
 | 
					 | 
				
			||||||
    s.swap(x, y)
 | 
					 | 
				
			||||||
    inc x
 | 
					 | 
				
			||||||
    dec y
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
iterator asInt*(s: ColorStack): int8 =
 | 
					 | 
				
			||||||
  for i in 0'u8 .. s.high:
 | 
					 | 
				
			||||||
    yield int8(s[i]) # now we do have to convert
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc `$`*(s: ColorStack): string =
 | 
					 | 
				
			||||||
  result = "St@["
 | 
					 | 
				
			||||||
  for c in s:
 | 
					 | 
				
			||||||
    if result[^1] != '[':
 | 
					 | 
				
			||||||
      result.add(", ")
 | 
					 | 
				
			||||||
    result.add($c)
 | 
					 | 
				
			||||||
  result.add("]")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc check(s: ColorStack) =
 | 
					 | 
				
			||||||
  # ensure length is accurate
 | 
					 | 
				
			||||||
  var d = s.data
 | 
					 | 
				
			||||||
  for i in 0'u8 .. 4'u8:
 | 
					 | 
				
			||||||
    if (d and masks[1]) > 4:
 | 
					 | 
				
			||||||
      raise newException(RangeDefect, "Value out of range.")
 | 
					 | 
				
			||||||
    if d > 0 and i >= s.len:
 | 
					 | 
				
			||||||
      raise newException(RangeDefect, "Invalid length.")
 | 
					 | 
				
			||||||
    else:
 | 
					 | 
				
			||||||
      d = d shr 3
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
when isMainModule:
 | 
					 | 
				
			||||||
  var one: ColorStack
 | 
					 | 
				
			||||||
  one.add(cRed)
 | 
					 | 
				
			||||||
  one.add(cGreen)
 | 
					 | 
				
			||||||
  one.add(cBlue)
 | 
					 | 
				
			||||||
  one.add(cYellow)
 | 
					 | 
				
			||||||
  one.add(cPurple)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  var two: ColorStack
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  one.moveSubstack(two, 2)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  echo one, " ", one.len
 | 
					 | 
				
			||||||
  echo two, " ", two.len
 | 
					 | 
				
			||||||
  echo two.find(cRed)
 | 
					 | 
				
			||||||
							
								
								
									
										35
									
								
								game.nim
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								game.nim
									
									
									
									
									
								
							@@ -1,26 +1,25 @@
 | 
				
			|||||||
import hashes, options
 | 
					import hashes, options
 | 
				
			||||||
import fixedseq, faststack
 | 
					import fixedseq
 | 
				
			||||||
 | 
					 | 
				
			||||||
export faststack.Color, faststack.ColorStack, faststack.allColors
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# type
 | 
					type
 | 
				
			||||||
#   Color* = enum
 | 
					  Color* = enum
 | 
				
			||||||
#     cRed, cGreen, cBlue, cYellow, cPurple
 | 
					    cRed, cGreen, cBlue, cYellow, cPurple
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#   ColorStack* = FixedSeq[5, Color, int8]
 | 
					  ColorStack* = FixedSeq[5, Color, int8]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# proc initColorStack*: ColorStack =
 | 
					proc initColorStack*: ColorStack =
 | 
				
			||||||
#   result.initFixedSeq
 | 
					  result.initFixedSeq
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# proc getAllColors: ColorStack = 
 | 
					proc getAllColors: ColorStack = 
 | 
				
			||||||
#   var i = 0'u8
 | 
					  var i = 0
 | 
				
			||||||
#   for c in Color.low .. Color.high:
 | 
					  for c in Color.low .. Color.high:
 | 
				
			||||||
#     result[i] = c
 | 
					    result[i] = c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const 
 | 
					const 
 | 
				
			||||||
 | 
					  allColors* = getAllColors()
 | 
				
			||||||
  colorNames: array[Color, string] =
 | 
					  colorNames: array[Color, string] =
 | 
				
			||||||
    ["Red", "Green", "Blue", "Yellow", "Purple"]
 | 
					    ["Red", "Green", "Blue", "Yellow", "Purple"]
 | 
				
			||||||
  colorAbbrevs: array[Color, char] = ['R', 'G', 'B', 'Y', 'P']
 | 
					  colorAbbrevs: array[Color, char] = ['R', 'G', 'B', 'Y', 'P']
 | 
				
			||||||
@@ -83,8 +82,8 @@ proc hash*(b: Board): Hash =
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
proc init*(b: var Board) =
 | 
					proc init*(b: var Board) =
 | 
				
			||||||
  # for sq in b.squares.mitems:
 | 
					  for sq in b.squares.mitems:
 | 
				
			||||||
  #   sq.camels.initFixedSeq
 | 
					    sq.camels.initFixedSeq
 | 
				
			||||||
  b.initialized = true
 | 
					  b.initialized = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -113,7 +112,7 @@ proc setState*(b: var Board;
 | 
				
			|||||||
  b.leader = some(leadCamel)
 | 
					  b.leader = some(leadCamel)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
proc diceRemaining*(b: Board): FixedSeq[5, Color, int8] =
 | 
					proc diceRemaining*(b: Board): ColorStack =
 | 
				
			||||||
  result.initFixedSeq
 | 
					  result.initFixedSeq
 | 
				
			||||||
  for color, isRolled in b.diceRolled:
 | 
					  for color, isRolled in b.diceRolled:
 | 
				
			||||||
    if not isRolled: result.add(color)
 | 
					    if not isRolled: result.add(color)
 | 
				
			||||||
@@ -141,11 +140,11 @@ proc advance*(b: var Board, die: Die) =
 | 
				
			|||||||
    endPos += int(t)
 | 
					    endPos += int(t)
 | 
				
			||||||
    if t == tBackward: prepend = true
 | 
					    if t == tBackward: prepend = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let stackStart = b[startPos].camels.find(color).uint8
 | 
					  let stackStart = b[startPos].camels.find(color)
 | 
				
			||||||
  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'u8 ..< stackLen:
 | 
					    for i in 0 ..< 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]] = endPos
 | 
				
			||||||
  else:
 | 
					  else:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,5 +1,5 @@
 | 
				
			|||||||
import cpuinfo, math, options, random, tables
 | 
					import cpuinfo, math, options, random, tables
 | 
				
			||||||
import combinators, game, faststack, fixedseq
 | 
					import combinators, game, fixedseq
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type
 | 
					type
 | 
				
			||||||
@@ -38,7 +38,7 @@ proc percents*(scores: ScoreSet): WinPercents =
 | 
				
			|||||||
# ======================
 | 
					# ======================
 | 
				
			||||||
 | 
					
 | 
				
			||||||
iterator legEndStates(b: Board): Board =
 | 
					iterator legEndStates(b: Board): Board =
 | 
				
			||||||
  var diceRemaining: FixedSeq[5, Color, int8]
 | 
					  var diceRemaining: ColorStack
 | 
				
			||||||
  diceRemaining.initFixedSeq
 | 
					  diceRemaining.initFixedSeq
 | 
				
			||||||
  for i, c in b.diceRolled:
 | 
					  for i, c in b.diceRolled:
 | 
				
			||||||
    if not c: diceRemaining.add(i)
 | 
					    if not c: diceRemaining.add(i)
 | 
				
			||||||
@@ -59,39 +59,6 @@ proc getLegScores*(b: Board): ScoreSet =
 | 
				
			|||||||
# Full-game simulations
 | 
					# Full-game simulations
 | 
				
			||||||
# =====================
 | 
					# =====================
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# get rid of this later
 | 
					 | 
				
			||||||
import strutils
 | 
					 | 
				
			||||||
proc showSpaces*(b: Board; start, stop: Natural): string =
 | 
					 | 
				
			||||||
  let numSpaces = stop - start + 1
 | 
					 | 
				
			||||||
  let width = 4 * numSpaces - 1
 | 
					 | 
				
			||||||
  var lines: array[7, string]
 | 
					 | 
				
			||||||
  # start by building up an empty board
 | 
					 | 
				
			||||||
  for i in 0 .. 6: # gotta initialize the strings
 | 
					 | 
				
			||||||
    lines[i] = newString(width)
 | 
					 | 
				
			||||||
    for c in lines[i].mitems:
 | 
					 | 
				
			||||||
      c = ' '
 | 
					 | 
				
			||||||
  # fill in the dividers
 | 
					 | 
				
			||||||
  lines[5] = repeat("=== ", numSpaces - 1)
 | 
					 | 
				
			||||||
  lines[5].add("===")
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  # now populate the board
 | 
					 | 
				
			||||||
  for sp in 0 ..< numSpaces:
 | 
					 | 
				
			||||||
    # fill in the square numbers
 | 
					 | 
				
			||||||
    let squareNum = sp + start
 | 
					 | 
				
			||||||
    let cellMid = 4 * sp + 1
 | 
					 | 
				
			||||||
    for i, chr in $squareNum:
 | 
					 | 
				
			||||||
      lines[6][cellMid + i] = chr
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # fill in the camel stacks
 | 
					 | 
				
			||||||
    for i, color in b.squares[squareNum].camels:
 | 
					 | 
				
			||||||
      let lineNum = 4 - i # lines go to 6, but bottom 2 are reserved
 | 
					 | 
				
			||||||
      let repr = '|' & color.abbrev & '|'
 | 
					 | 
				
			||||||
      for j, chr in repr:
 | 
					 | 
				
			||||||
        lines[lineNum][cellMid - 1 + j] = chr
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  result = lines.join("\n")
 | 
					 | 
				
			||||||
# get rid of this later
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
proc randomGame*(b: Board, r: var Rand): Color =
 | 
					proc randomGame*(b: Board, r: var Rand): Color =
 | 
				
			||||||
  var projection = b
 | 
					  var projection = b
 | 
				
			||||||
  while true:
 | 
					  while true:
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user