2024-04-16 15:24:31 +02:00
|
|
|
## Handling of moves
|
|
|
|
import pieces
|
|
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
MoveFlag* = enum
|
|
|
|
## An enumeration of move flags
|
|
|
|
Default = 0'u16, # No flag
|
|
|
|
EnPassant = 1, # Move is a capture with en passant
|
|
|
|
Capture = 2, # Move is a capture
|
|
|
|
DoublePush = 4, # Move is a double pawn push
|
|
|
|
# Castling metadata
|
|
|
|
CastleLong = 8,
|
|
|
|
CastleShort = 16,
|
|
|
|
# Pawn promotion metadata
|
|
|
|
PromoteToQueen = 32,
|
|
|
|
PromoteToRook = 64,
|
|
|
|
PromoteToBishop = 128,
|
|
|
|
PromoteToKnight = 256
|
|
|
|
|
|
|
|
Move* = object
|
|
|
|
## A chess move
|
|
|
|
startSquare*: Square
|
|
|
|
targetSquare*: Square
|
|
|
|
flags*: uint16
|
|
|
|
|
2024-04-16 16:29:21 +02:00
|
|
|
MoveList* = object
|
|
|
|
## A list of moves
|
|
|
|
data: array[218, Move]
|
|
|
|
len: int8
|
|
|
|
|
|
|
|
|
|
|
|
iterator items*(self: MoveList): Move =
|
|
|
|
var i = 0
|
|
|
|
while self.len > i:
|
|
|
|
yield self.data[i]
|
|
|
|
inc(i)
|
|
|
|
|
|
|
|
|
|
|
|
iterator pairs*(self: MoveList): tuple[i: int, move: Move] =
|
|
|
|
var i = 0
|
|
|
|
for item in self:
|
|
|
|
yield (i, item)
|
|
|
|
|
|
|
|
|
|
|
|
func add*(self: var MoveList, move: Move) {.inline.} =
|
|
|
|
self.data[self.len] = move
|
|
|
|
inc(self.len)
|
|
|
|
|
|
|
|
|
|
|
|
func contains*(self: MoveList, move: Move): bool {.inline.} =
|
|
|
|
for item in self:
|
|
|
|
if move == item:
|
|
|
|
return true
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
|
|
func len*(self: MoveList): int {.inline.} = self.len
|
|
|
|
|
2024-04-16 15:24:31 +02:00
|
|
|
|
|
|
|
func createMove*(startSquare, targetSquare: string, flags: seq[MoveFlag] = @[]): Move =
|
|
|
|
result = Move(startSquare: startSquare.algebraicToSquare(),
|
|
|
|
targetSquare: targetSquare.algebraicToSquare(), flags: Default.uint16)
|
|
|
|
for flag in flags:
|
|
|
|
result.flags = result.flags or flag.uint16
|
|
|
|
|
|
|
|
|
|
|
|
func createMove*(startSquare, targetSquare: Square, flags: seq[MoveFlag] = @[]): Move =
|
|
|
|
result = Move(startSquare: startSquare, targetSquare: targetSquare, flags: Default.uint16)
|
|
|
|
for flag in flags:
|
|
|
|
result.flags = result.flags or flag.uint16
|
|
|
|
|
|
|
|
|
|
|
|
func nullMove*: Move {.inline.} = createMove(nullSquare(), nullSquare())
|