90 lines
3.2 KiB
Nim
90 lines
3.2 KiB
Nim
# Copyright 2024 Mattia Giambirtone & All Contributors
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
import bitboards
|
|
import magics
|
|
import pieces
|
|
import zobrist
|
|
|
|
|
|
type
|
|
Position* = object
|
|
## A chess position
|
|
|
|
# Castling availability. This just keeps track
|
|
# of whether the king or the rooks on either side
|
|
# moved, the actual checks for the legality of castling
|
|
# are done elsewhere
|
|
castlingAvailability*: array[PieceColor.White..PieceColor.Black, tuple[queen, king: bool]]
|
|
# Number of half-moves that were performed
|
|
# to reach this position starting from the
|
|
# root of the tree
|
|
plyFromRoot*: uint16
|
|
# Number of half moves since
|
|
# last piece capture or pawn movement.
|
|
# Used for the 50-move rule
|
|
halfMoveClock*: uint16
|
|
# Full move counter. Increments
|
|
# every 2 ply (half-moves)
|
|
fullMoveCount*: uint16
|
|
# 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[PieceColor.White..PieceColor.Black, array[PieceKind.Bishop..PieceKind.Rook, 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
|
|
# Zobrist hash of this position
|
|
zobristKey*: ZobristKey
|
|
# Cached result of drawByRepetition()
|
|
repetitionDraw*: bool
|
|
|
|
|
|
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 getBitboard*(self: Position, kind: PieceKind, color: PieceColor): Bitboard =
|
|
## Returns the positional bitboard for the given piece kind and color
|
|
return self.pieces[color][kind]
|
|
|
|
|
|
func getBitboard*(self: Position, piece: Piece): Bitboard =
|
|
## Returns the positional bitboard for the given piece type
|
|
return self.getBitboard(piece.kind, piece.color)
|
|
|
|
|
|
func getOccupancyFor*(self: Position, color: PieceColor): Bitboard =
|
|
## Get the occupancy bitboard for every piece of the given color
|
|
result = Bitboard(0)
|
|
for b in self.pieces[color]:
|
|
result = result or b
|
|
|
|
|
|
func getOccupancy*(self: Position): Bitboard {.inline.} =
|
|
## Get the occupancy bitboard for every piece on
|
|
## the chessboard
|
|
result = self.getOccupancyFor(Black) or self.getOccupancyFor(White)
|