initial commit
This commit is contained in:
commit
c552834741
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.exe
|
76
main.nim
Normal file
76
main.nim
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import options, sequtils, sugar
|
||||||
|
|
||||||
|
|
||||||
|
type
|
||||||
|
Color = enum
|
||||||
|
cRed, cGreen, cBlue, cYellow, cPurple
|
||||||
|
|
||||||
|
Tile = enum
|
||||||
|
tForward = -1,
|
||||||
|
tBackward = 1
|
||||||
|
|
||||||
|
Square = object
|
||||||
|
camels: seq[Color]
|
||||||
|
tile: Option[Tile]
|
||||||
|
|
||||||
|
Die = tuple[color: Color, value: range[1..3]]
|
||||||
|
|
||||||
|
Board = object
|
||||||
|
squares: array[1..16, Square]
|
||||||
|
camels: array[Color, range[1..16]]
|
||||||
|
|
||||||
|
|
||||||
|
proc `[]`(b: var Board, idx: range[1..16]): var Square =
|
||||||
|
b.squares[idx]
|
||||||
|
|
||||||
|
|
||||||
|
proc display(b: Board, start, stop: int) =
|
||||||
|
for i in start..stop:
|
||||||
|
let sq = b.squares[i]
|
||||||
|
let lead = $i & ": "
|
||||||
|
if sq.tile.isSome:
|
||||||
|
echo lead, sq.tile.get
|
||||||
|
else:
|
||||||
|
echo lead, sq.camels
|
||||||
|
echo "\n"
|
||||||
|
|
||||||
|
|
||||||
|
proc preRoll(b: var Board) =
|
||||||
|
let colors = [cGreen, cYellow, cPurple, cBlue, cRed]
|
||||||
|
var rolls = [range[1..16]3, 2, 3, 2, 1]
|
||||||
|
|
||||||
|
for (color, roll) in zip(colors, rolls):
|
||||||
|
let dest = roll + 1
|
||||||
|
b[dest].camels.add(color)
|
||||||
|
b.camels[color] = dest
|
||||||
|
|
||||||
|
|
||||||
|
proc advance(b: var Board, die: Die) =
|
||||||
|
let
|
||||||
|
(color, roll) = die
|
||||||
|
startPos = b.camels[color]
|
||||||
|
var prepend = false
|
||||||
|
var endPos = startPos + roll
|
||||||
|
if b[endPos].tile.isSome:
|
||||||
|
endPos += int(b[endPos].tile.get)
|
||||||
|
if b[endPos].tile.get == tBackward: prepend = true # if moving backward, we will prepend camel to the seq
|
||||||
|
|
||||||
|
for i, c in b[startPos].camels:
|
||||||
|
if c == color:
|
||||||
|
let subStack = b[startPos].camels[i .. ^1]
|
||||||
|
b[endPos].camels.add(subStack)
|
||||||
|
if prepend:
|
||||||
|
# figure out how to prepend a sequence to another sequence
|
||||||
|
else:
|
||||||
|
b[startPos].camels[i .. ^1] = @[]
|
||||||
|
for moved in subStack:
|
||||||
|
b.camels[moved] = endPos
|
||||||
|
break # breaking the outer loop here, not the inner
|
||||||
|
|
||||||
|
|
||||||
|
var b: Board
|
||||||
|
# b.display(1, 5)
|
||||||
|
b.preRoll
|
||||||
|
let c = b.dup(advance((cRed, range[1..3]3)))
|
||||||
|
b.display(1, 5)
|
||||||
|
c.display(1, 5)
|
Loading…
x
Reference in New Issue
Block a user