Refactoring, cleanup, improve modularity

This commit is contained in:
Mattia Giambirtone 2024-04-20 14:51:50 +02:00
parent f5135ef69e
commit 4a9deb517a
1 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,68 @@
import std/strutils
import std/strformat
import bitboards
import magics
import pieces
import moves
type
Position* = object
## A chess position
# Castling metadata. Updated on every move
castlingRights*: array[64, uint8]
# 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)