From 9395e857db95481f6e2494d3a841632be0ff97c8 Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Sat, 18 Jul 2026 14:02:18 +0530 Subject: fixed moved gen & adding messages on asserts --- src/engine/moves.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) (limited to 'src/engine') diff --git a/src/engine/moves.c b/src/engine/moves.c index 0c923a1..6f96109 100644 --- a/src/engine/moves.c +++ b/src/engine/moves.c @@ -76,7 +76,7 @@ int find_piece_on_square(position_t* p, int square) { if ((p->bitboards[BLACK_BISHOP] >> square) & 1) return BLACK_BISHOP; if ((p->bitboards[BLACK_KNIGHT] >> square) & 1) return BLACK_KNIGHT; if ((p->bitboards[BLACK_PAWN] >> square) & 1) return BLACK_PAWN; - assert(0); + assert(0 && "Failed to find the piece on the square"); } void position_make_move(position_t* position, move_t move) { @@ -140,24 +140,40 @@ void position_make_move(position_t* position, move_t move) { } if (move.flags & MOVE_PROMOTE_Q) { + if (move.flags & MOVE_CAPTURE) { + int to_remove_piece_type = find_piece_on_square(position, move.to); + position->bitboards[to_remove_piece_type] &= ~((bitboard_t)1 << move.to); + } int q_type = position->turn == WHITE_TURN ? WHITE_QUEEN : BLACK_QUEEN; position->bitboards[q_type] |= (bitboard_t)1 << move.to; position->turn = !position->turn; return; } if (move.flags & MOVE_PROMOTE_R) { + if (move.flags & MOVE_CAPTURE) { + int to_remove_piece_type = find_piece_on_square(position, move.to); + position->bitboards[to_remove_piece_type] &= ~((bitboard_t)1 << move.to); + } int q_type = position->turn == WHITE_TURN ? WHITE_ROOK : BLACK_ROOK; position->bitboards[q_type] |= (bitboard_t)1 << move.to; position->turn = !position->turn; return; } if (move.flags & MOVE_PROMOTE_B) { + if (move.flags & MOVE_CAPTURE) { + int to_remove_piece_type = find_piece_on_square(position, move.to); + position->bitboards[to_remove_piece_type] &= ~((bitboard_t)1 << move.to); + } int q_type = position->turn == WHITE_TURN ? WHITE_BISHOP : BLACK_BISHOP; position->bitboards[q_type] |= (bitboard_t)1 << move.to; position->turn = !position->turn; return; } if (move.flags & MOVE_PROMOTE_N) { + if (move.flags & MOVE_CAPTURE) { + int to_remove_piece_type = find_piece_on_square(position, move.to); + position->bitboards[to_remove_piece_type] &= ~((bitboard_t)1 << move.to); + } int q_type = position->turn == WHITE_TURN ? WHITE_KNIGHT : BLACK_KNIGHT; position->bitboards[q_type] |= (bitboard_t)1 << move.to; position->turn = !position->turn; @@ -206,6 +222,7 @@ int count_positions(position_t position, int depth) { moves_t moves; moves_init(&moves); get_legal_moves(&moves, position); + int count = 0; for (int i = 0; i < moves.length; i++) { position_t copy = position; @@ -221,6 +238,7 @@ void perft_divide(position_t position, int depth) { moves_t moves; moves_init(&moves); get_legal_moves(&moves, position); + int nodes = 0; for (int i = 0; i < moves.length; i++) { position_t copy = position; @@ -281,12 +299,11 @@ bool test_perft_position3() { d = count_positions(position, 3); if (d != 2812) return false; d = count_positions(position, 4); if (d != 43238) return false; d = count_positions(position, 5); if (d != 674624) return false; - perft_divide(position, 6); - d = count_positions(position, 6); - printf("%d\n", d); - if (d != 11030083) return false; - d = count_positions(position, 7); if (d != 178633661) return false; - d = count_positions(position, 8); if (d != 3009794393) return false; + d = count_positions(position, 6); if (d != 11030083) return false; + + // my move gen too slow to verify this + // d = count_positions(position, 7); if (d != 178633661) return false; + // d = count_positions(position, 8); if (d != 3009794393) return false; return true; } -- cgit v1.2.3