CPG/Chess/nimfish/nimfishpkg/eval.nim

69 lines
1.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.
## Position evaluation utilities
import board
type
Score* = int16
proc getPieceValue(kind: PieceKind): Score =
## Returns the absolute value of a piece
case kind:
of Pawn:
return Score(100)
of Bishop:
return Score(330)
of Knight:
return Score(280)
of Rook:
return Score(525)
of Queen:
return Score(950)
else:
discard
proc getPieceScore(board: Chessboard, square: Square): Score =
## Returns the value of the piece located at
## the given square
return board.getPiece(square).kind.getPieceValue()
proc evaluateMaterial(board: ChessBoard): Score =
## Returns the material evaluation of the
## current position relative to white (positive
## if in white's favor, negative otherwise)
var
whiteScore: Score
blackScore: Score
for sq in board.getOccupancyFor(White):
whiteScore += board.getPieceScore(sq)
for sq in board.getOccupancyFor(Black):
blackScore += board.getPieceScore(sq)
result = whiteScore - blackScore
if board.position.sideToMove == Black:
result *= -1
proc evaluate*(board: Chessboard): Score =
## Evaluates the current position
result = board.evaluateMaterial()