124 lines
5.3 KiB
Nim
124 lines
5.3 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.
|
|
|
|
## Miscellaneous stuff
|
|
|
|
import board
|
|
|
|
|
|
import std/strformat
|
|
|
|
|
|
proc testPiece(piece: Piece, kind: PieceKind, color: PieceColor) =
|
|
doAssert piece.kind == kind and piece.color == color, &"expected piece of kind {kind} and color {color}, got {piece.kind} / {piece.color} instead"
|
|
|
|
proc testPieceCount(board: Chessboard, kind: PieceKind, color: PieceColor, count: int) =
|
|
let pieces = board.countPieces(kind, color)
|
|
doAssert pieces == count, &"expected {count} pieces of kind {kind} and color {color}, got {pieces} instead"
|
|
|
|
proc testPieceBitboard(bitboard: Bitboard, squares: seq[Square]) =
|
|
var i = 0
|
|
for square in bitboard:
|
|
doAssert squares[i] == square, &"squares[{i}] != bitboard[i]: {squares[i]} != {square}"
|
|
inc(i)
|
|
if i != squares.len():
|
|
doAssert false, &"bitboard.len() ({i}) != squares.len() ({squares.len()})"
|
|
|
|
|
|
proc basicTests* =
|
|
var b = newDefaultChessboard()
|
|
# Ensure correct number of pieces
|
|
testPieceCount(b, Pawn, White, 8)
|
|
testPieceCount(b, Pawn, Black, 8)
|
|
testPieceCount(b, Knight, White, 2)
|
|
testPieceCount(b, Knight, Black, 2)
|
|
testPieceCount(b, Bishop, White, 2)
|
|
testPieceCount(b, Bishop, Black, 2)
|
|
testPieceCount(b, Rook, White, 2)
|
|
testPieceCount(b, Rook, Black, 2)
|
|
testPieceCount(b, Queen, White, 1)
|
|
testPieceCount(b, Queen, Black, 1)
|
|
testPieceCount(b, King, White, 1)
|
|
testPieceCount(b, King, Black, 1)
|
|
|
|
# Ensure pieces are in the correct squares. This is testing the FEN
|
|
# parser
|
|
|
|
# Pawns
|
|
for loc in ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2"]:
|
|
testPiece(b.getPiece(loc), Pawn, White)
|
|
for loc in ["a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"]:
|
|
testPiece(b.getPiece(loc), Pawn, Black)
|
|
# Rooks
|
|
testPiece(b.getPiece("a1"), Rook, White)
|
|
testPiece(b.getPiece("h1"), Rook, White)
|
|
testPiece(b.getPiece("a8"), Rook, Black)
|
|
testPiece(b.getPiece("h8"), Rook, Black)
|
|
# Knights
|
|
testPiece(b.getPiece("b1"), Knight, White)
|
|
testPiece(b.getPiece("g1"), Knight, White)
|
|
testPiece(b.getPiece("b8"), Knight, Black)
|
|
testPiece(b.getPiece("g8"), Knight, Black)
|
|
# Bishops
|
|
testPiece(b.getPiece("c1"), Bishop, White)
|
|
testPiece(b.getPiece("f1"), Bishop, White)
|
|
testPiece(b.getPiece("c8"), Bishop, Black)
|
|
testPiece(b.getPiece("f8"), Bishop, Black)
|
|
# Kings
|
|
testPiece(b.getPiece("e1"), King, White)
|
|
testPiece(b.getPiece("e8"), King, Black)
|
|
# Queens
|
|
testPiece(b.getPiece("d1"), Queen, White)
|
|
testPiece(b.getPiece("d8"), Queen, Black)
|
|
|
|
# Ensure our bitboards match with the board
|
|
let
|
|
whitePawns = b.getBitboard(Pawn, White)
|
|
whiteKnights = b.getBitboard(Knight, White)
|
|
whiteBishops = b.getBitboard(Bishop, White)
|
|
whiteRooks = b.getBitboard(Rook, White)
|
|
whiteQueens = b.getBitboard(Queen, White)
|
|
whiteKing = b.getBitboard(King, White)
|
|
blackPawns = b.getBitboard(Pawn, Black)
|
|
blackKnights = b.getBitboard(Knight, Black)
|
|
blackBishops = b.getBitboard(Bishop, Black)
|
|
blackRooks = b.getBitboard(Rook, Black)
|
|
blackQueens = b.getBitboard(Queen, Black)
|
|
blackKing = b.getBitboard(King, Black)
|
|
whitePawnSquares = @[makeSquare(6'i8, 0'i8), makeSquare(6, 1), makeSquare(6, 2), makeSquare(6, 3), makeSquare(6, 4), makeSquare(6, 5), makeSquare(6, 6), makeSquare(6, 7)]
|
|
whiteKnightSquares = @[makeSquare(7'i8, 1'i8), makeSquare(7, 6)]
|
|
whiteBishopSquares = @[makeSquare(7'i8, 2'i8), makeSquare(7, 5)]
|
|
whiteRookSquares = @[makeSquare(7'i8, 0'i8), makeSquare(7, 7)]
|
|
whiteQueenSquares = @[makeSquare(7'i8, 3'i8)]
|
|
whiteKingSquares = @[makeSquare(7'i8, 4'i8)]
|
|
blackPawnSquares = @[makeSquare(1'i8, 0'i8), makeSquare(1, 1), makeSquare(1, 2), makeSquare(1, 3), makeSquare(1, 4), makeSquare(1, 5), makeSquare(1, 6), makeSquare(1, 7)]
|
|
blackKnightSquares = @[makeSquare(0'i8, 1'i8), makeSquare(0, 6)]
|
|
blackBishopSquares = @[makeSquare(0'i8, 2'i8), makeSquare(0, 5)]
|
|
blackRookSquares = @[makeSquare(0'i8, 0'i8), makeSquare(0, 7)]
|
|
blackQueenSquares = @[makeSquare(0'i8, 3'i8)]
|
|
blackKingSquares = @[makeSquare(0'i8, 4'i8)]
|
|
|
|
|
|
testPieceBitboard(whitePawns, whitePawnSquares)
|
|
testPieceBitboard(whiteKnights, whiteKnightSquares)
|
|
testPieceBitboard(whiteBishops, whiteBishopSquares)
|
|
testPieceBitboard(whiteRooks, whiteRookSquares)
|
|
testPieceBitboard(whiteQueens, whiteQueenSquares)
|
|
testPieceBitboard(whiteKing, whiteKingSquares)
|
|
testPieceBitboard(blackPawns, blackPawnSquares)
|
|
testPieceBitboard(blackKnights, blackKnightSquares)
|
|
testPieceBitboard(blackBishops, blackBishopSquares)
|
|
testPieceBitboard(blackRooks, blackRookSquares)
|
|
testPieceBitboard(blackQueens, blackQueenSquares)
|
|
testPieceBitboard(blackKing, blackKingSquares) |