CPG/Chess/nimfish/nimfishpkg/position.nim

68 lines
2.4 KiB
Nim

import bitboards
import magics
import pieces
type
Position* = object
## A chess position
# Castling metadata. Updated on every move
castlingRights*: array[64, uint8]
# Castling availability. This just keeps track
# of whether the king or the rooks on either side
# moved and is only useful for printing the correct
# FEN for a given position
castlingAvailability*: tuple[white, black: tuple[queen, king: bool]]
# Number of half-moves that were performed
# to reach this position starting from the
# root of the tree
plyFromRoot*: int8
# Number of half moves since
# last piece capture or pawn movement.
# Used for the 50-move rule
halfMoveClock*: int8
# Full move counter. Increments
# every 2 ply (half-moves)
fullMoveCount*: int8
# En passant target square (see https://en.wikipedia.org/wiki/En_passant)
enPassantSquare*: Square
# The side to move
sideToMove*: PieceColor
# Positional bitboards for all pieces
pieces*: array[2, array[6, Bitboard]]
# Pieces pinned for the current side to move
diagonalPins*: Bitboard # Pinned diagonally (by a queen or bishop)
orthogonalPins*: Bitboard # Pinned orthogonally (by a queen or rook)
# Pieces checking the current side to move
checkers*: Bitboard
func getKingStartingSquare*(color: PieceColor): Square {.inline.} =
## Retrieves the starting square of the king
## for the given color
case color:
of White:
return "e1".toSquare()
of Black:
return "e8".toSquare()
else:
discard
func `[]`*(self: array[2, array[6, Bitboard]], color: PieceColor): ptr array[6, Bitboard] {.inline.} = addr self[color.int]
func `[]`*(self: array[6, Bitboard], kind: PieceKind): ptr Bitboard {.inline.} = addr self[kind.int]
func `[]=`*(self: var array[6, Bitboard], kind: PieceKind, bitboard: Bitboard) {.inline.} = self[kind.int] = bitboard
func getBitboard*(self: Position, kind: PieceKind, color: PieceColor): Bitboard =
## Returns the positional bitboard for the given piece kind and color
return self.pieces[color.int][kind.int]
func getBitboard*(self: Position, piece: Piece): Bitboard =
## Returns the positional bitboard for the given piece type
return self.getBitboard(piece.kind, piece.color)