CPG/Chess/nimfish/nimfishpkg/position.nim

79 lines
2.9 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
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*: 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)