deduplicate game states at end of leg
This commit is contained in:
parent
6e1a1c8c96
commit
2aa0e63987
101
main.nim
101
main.nim
@ -1,4 +1,4 @@
|
|||||||
import math, options, tables, sequtils, sets, sugar
|
import math, hashes, options, tables, sequtils, sets
|
||||||
import combinators
|
import combinators
|
||||||
|
|
||||||
|
|
||||||
@ -7,8 +7,8 @@ type
|
|||||||
cRed, cGreen, cBlue, cYellow, cPurple
|
cRed, cGreen, cBlue, cYellow, cPurple
|
||||||
|
|
||||||
Tile = enum
|
Tile = enum
|
||||||
tForward = -1,
|
tBackward = -1,
|
||||||
tBackward = 1
|
tForward = 1
|
||||||
|
|
||||||
Square = object
|
Square = object
|
||||||
camels: seq[Color]
|
camels: seq[Color]
|
||||||
@ -18,17 +18,41 @@ type
|
|||||||
|
|
||||||
ScoreSet = array[Color, int]
|
ScoreSet = array[Color, int]
|
||||||
|
|
||||||
|
LegResults = tuple[scores: ScoreSet, endStates: HashSet[Board]]
|
||||||
|
|
||||||
Board = object
|
Board = object
|
||||||
squares: array[1..16, Square]
|
squares: array[1..16, Square]
|
||||||
camels: array[Color, range[1..16]]
|
camels: array[Color, range[1..16]]
|
||||||
leader: Color
|
leader: Option[Color]
|
||||||
gameOver: bool
|
gameOver: bool
|
||||||
|
|
||||||
|
|
||||||
|
const allDice = @[cRed, cGreen, cBlue, cYellow, cPurple]
|
||||||
|
|
||||||
|
|
||||||
|
proc update(scores: var ScoreSet, toAdd: ScoreSet) =
|
||||||
|
for i, s in toAdd:
|
||||||
|
scores[i] += s
|
||||||
|
|
||||||
|
|
||||||
proc `[]`[T](b: var Board, idx: T): var Square =
|
proc `[]`[T](b: var Board, idx: T): var Square =
|
||||||
b.squares[idx]
|
b.squares[idx]
|
||||||
|
|
||||||
|
|
||||||
|
proc hash(b: Board): Hash =
|
||||||
|
var h: Hash = 0
|
||||||
|
# there could be a tile anywhere so we have to check all squares
|
||||||
|
for i, sq in b.squares:
|
||||||
|
if sq.camels.len > 0 or sq.tile.isSome:
|
||||||
|
h = h !& i
|
||||||
|
if sq.tile.isSome:
|
||||||
|
h = h !& int(sq.tile.get) * 10 # so it isn't confused with a camel
|
||||||
|
else:
|
||||||
|
for c in sq.camels:
|
||||||
|
h = h !& int(c)
|
||||||
|
result = !$h
|
||||||
|
|
||||||
|
|
||||||
proc display(b: Board, start, stop: int) =
|
proc display(b: Board, start, stop: int) =
|
||||||
for i in start..stop:
|
for i in start..stop:
|
||||||
let sq = b.squares[i]
|
let sq = b.squares[i]
|
||||||
@ -40,31 +64,36 @@ proc display(b: Board, start, stop: int) =
|
|||||||
echo ""
|
echo ""
|
||||||
|
|
||||||
|
|
||||||
proc setLeader(b: var Board) =
|
proc setState(b: var Board;
|
||||||
b.leader = b[b.camels.max].camels[^1] # top camel in the last currently-occupied space
|
camels: openArray[tuple[c: Color, p: int]];
|
||||||
|
tiles: openArray[tuple[t: Tile, p: int]]) =
|
||||||
|
for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking
|
||||||
proc setState[T](b: var Board, state: T) =
|
|
||||||
for (color, dest) in state: # note that `state` is ordered, as this determines stacking
|
|
||||||
b[dest].camels.add(color)
|
b[dest].camels.add(color)
|
||||||
b.camels[color] = dest
|
b.camels[color] = dest
|
||||||
b.setLeader
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
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]
|
||||||
var prepend = false
|
|
||||||
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
|
||||||
b.leader = color
|
b.leader = some(b[startPos].camels[^1])
|
||||||
b.gameOver = true
|
b.gameOver = true
|
||||||
return
|
return
|
||||||
if b[endPos].tile.isSome:
|
|
||||||
endPos += int(b[endPos].tile.get)
|
var prepend = false
|
||||||
if b[endPos].tile.get == tBackward: prepend = true # if moving backward, we will prepend camel to the seq
|
if b[endPos].tile.isSome: # adjust position (and possibly stacking) to account for tile
|
||||||
|
let t = b[endPos].tile.get
|
||||||
|
endPos += int(t)
|
||||||
|
if t == tBackward: prepend = true
|
||||||
|
|
||||||
for i, c in b[startPos].camels:
|
for i, c in b[startPos].camels:
|
||||||
if c == color:
|
if c == color:
|
||||||
@ -77,40 +106,50 @@ proc advance(b: var Board, die: Die) =
|
|||||||
b[startPos].camels[i .. ^1] = @[]
|
b[startPos].camels[i .. ^1] = @[]
|
||||||
for moved in subStack:
|
for moved in subStack:
|
||||||
b.camels[moved] = endPos
|
b.camels[moved] = endPos
|
||||||
b.setLeader
|
# if we are stacking on or moving past the previous leader
|
||||||
|
if endPos >= b.camels[b.leader.get]:
|
||||||
|
b.leader = some(b[endPos].camels[^1])
|
||||||
break # breaking the outer loop here, not the inner - but only conditionally! gah!
|
break # breaking the outer loop here, not the inner - but only conditionally! gah!
|
||||||
|
|
||||||
|
|
||||||
type CamelPositions = seq[tuple[square: int, camels: seq[Color]]]
|
proc simulateLeg(b: Board, diceRemaining: seq[Color]): LegResults =
|
||||||
|
var scores: ScoreSet
|
||||||
proc simulateLeg(b: Board, diceRemaining: seq[Color]): ScoreSet =
|
|
||||||
var endStates: HashSet[Board]
|
var endStates: HashSet[Board]
|
||||||
for future in possibleFutures(diceRemaining):
|
for future in possibleFutures(diceRemaining):
|
||||||
var prediction = b # make a copy
|
var prediction = b # make a copy
|
||||||
for dieRoll in future:
|
for dieRoll in future:
|
||||||
prediction.advance(dieRoll)
|
prediction.advance(dieRoll)
|
||||||
inc result[prediction.leader]
|
inc scores[prediction.leader.get]
|
||||||
# deduplicate results
|
# deduplicate results
|
||||||
endStates.incl(prediction) # need to add hash implementation for Board
|
endStates.incl(prediction)
|
||||||
#[
|
result = (scores, endStates)
|
||||||
var positions: CamelPositions
|
|
||||||
for i in prediction.camels.min .. prediction.camels.max:
|
|
||||||
positions.add((i, prediction.squares[i].camels))
|
proc simulateGame(b: Board, diceRemaining: seq[Color]): ScoreSet =
|
||||||
endStates.incl(positions)
|
# currently just simulates two legs, not a full game
|
||||||
]#
|
var (scores, startStates) = b.simulateLeg(diceRemaining)
|
||||||
echo "Distinct end states: ", endStates.len
|
var endStates: HashSet[Board]
|
||||||
|
|
||||||
|
for s in startStates:
|
||||||
|
let (nextScores, nextStates) = s.simulateLeg(@[cRed, cGreen, cBlue, cYellow, cPurple])
|
||||||
|
scores.update(nextScores)
|
||||||
|
endStates.incl(nextStates)
|
||||||
|
stdout.write("simulated: " & $scores.sum & "\r")
|
||||||
|
|
||||||
|
echo "\nDistinct end states: ", endStates.len
|
||||||
|
result = scores
|
||||||
|
|
||||||
|
|
||||||
var b: Board
|
var b: Board
|
||||||
b.display(1, 5)
|
b.display(1, 5)
|
||||||
|
|
||||||
b.setState({cGreen: 4, cYellow: 3, cPurple: 4, cBlue: 3, cRed: 2})
|
b.setState({cGreen: 4, cYellow: 3, cPurple: 4, cBlue: 3, cRed: 2}, @[])
|
||||||
b.display(1, 5)
|
b.display(1, 5)
|
||||||
|
|
||||||
b.advance((cRed, 2))
|
b.advance((cRed, 2))
|
||||||
b.display(1, 5)
|
b.display(1, 5)
|
||||||
|
|
||||||
let r = b.simulateLeg(@[cRed, cGreen, cBlue, cYellow, cPurple])
|
let r = b.simulateGame(allDice)
|
||||||
let total = r.sum
|
let total = r.sum
|
||||||
for i, c in r:
|
for i, c in r:
|
||||||
echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")"
|
echo Color(i), ": ", (100 * c / total).round(2), "% (", c, " / ", total, ")"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user