From 6f7cfbfcde39a57a59db17882339fa26055b75a0 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 14 Sep 2022 05:27:30 +0300 Subject: feat: precompute king attack tables --- src/main.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c index 5f19585..c8c82cf 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,16 @@ int main() { start_test_section("Bitboards"); unit_test(pop_count(0b01110) == 3, "Pop count of 01110 is 3"); } - + { + start_test_section("Default FEN string"); + Board board = parse_FEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); + unit_test(pop_count(board.pieces[PAWN] | board.pieces[PAWN | BLACK]) == 16, "There are 16 pawns total"); + unit_test(pop_count(board.pieces[ROOK]) == 2, "There are 2 white rooks"); + // TODO + unit_test(board.side == WHITE, "Side to move is white"); + unit_test(board.castling_rights == 0, "Both sides can castle"); + unit_test(board.en_passant_square == 0, "No en passant move is avaialble"); + } { start_test_section("Test knight attacks"); Bitboard attacks[64]; @@ -19,25 +28,33 @@ int main() { } unit_test(max_attacks == 8, "Max amount of knight attacks should be 8"); + unit_test( + attacks[b7] == ((BIT << d8) | (BIT << d6) | (BIT << c5) | (BIT << a5)), + "Knight on b7 attacks only d8, d6, c5, a5" + ); + } + { + start_test_section("Test king attacks"); + Bitboard attacks[64]; + precompute_king_attack_table(attacks); + + int max_attacks = 0; + for (int i = 0; i < 64; i++) { + int attack_count = pop_count(attacks[i]); + if (attack_count > max_attacks) max_attacks = attack_count; + } + unit_test(max_attacks == 8, "Max amount of king attacks should be 8"); + { U64 bit = 1; unit_test( - attacks[b7] == ((bit << d8) | (bit << d6) | (bit << c5) | (bit << a5)), - "Knight on b7 attacks only d8, d6, c5, a5" + attacks[h2] == ((BIT << h1) | (BIT << g1) | (BIT << g2) | (BIT << g3) | (BIT << h3)), + "King on h2 attacks only h1, g1, g2, g3, h3" ); } } - { - start_test_section("Default FEN string"); - Board board = parse_FEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"); - unit_test(pop_count(board.pieces[PAWN] | board.pieces[PAWN | BLACK]) == 16, "There are 16 pawns total"); - unit_test(pop_count(board.pieces[ROOK]) == 2, "There are 2 white rooks"); - // TODO - unit_test(board.side == WHITE, "Side to move is white"); - unit_test(board.castling_rights == 0, "Everyone can castle"); - unit_test(board.en_passant_square == 0, "No en passant move is avaialble"); - } + report(); return 0; -- cgit v1.2.3