From 7e021a821b49306eaa8b0688e4b91d2db25d4878 Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Tue, 26 May 2026 14:07:15 +0530 Subject: rook moves --- src/search/bishop_moves.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ src/search/king_moves.c | 12 ++++----- src/search/pawn_moves.c | 10 ++++---- src/search/rook_moves.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+), 11 deletions(-) create mode 100644 src/search/bishop_moves.c create mode 100644 src/search/rook_moves.c (limited to 'src/search') diff --git a/src/search/bishop_moves.c b/src/search/bishop_moves.c new file mode 100644 index 0000000..10b46e5 --- /dev/null +++ b/src/search/bishop_moves.c @@ -0,0 +1,63 @@ +#include "search.h" + +void get_bishop_moves(moves_t* moves, position_t position) { + assert_valid_position(position); + + bitboard_t friendly_rooks; + bitboard_t friendly_pieces; + bitboard_t enemy_pieces; + if (position.turn == WHITE_TURN) { + friendly_pieces = whites(position); + enemy_pieces = blacks(position); + friendly_rooks = position.bitboards[WHITE_ROOK]; + } else { + friendly_pieces = blacks(position); + enemy_pieces = whites(position); + friendly_rooks = position.bitboards[BLACK_ROOK]; + } + + while (friendly_rooks) { + int from = __builtin_ctzll(friendly_rooks); + friendly_rooks &= friendly_rooks - 1; + + int og_rank = from / 8; // range: [0, 7] + int og_file = from % 8; // range: [0, 7] + int rank = og_rank + 1, file = og_file + 1, index = rank * 8 + file; + + + while ((rank < 8 && file < 8) && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = ++rank * 8 + ++file; + } + + rank = og_rank - 1, file = og_file - 1, index = rank * 8 + file; + while ((rank >= 0 && file >= 0) && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = --rank * 8 + --file; + } + + rank = og_rank - 1, file = og_file + 1, index = rank * 8 + file; + while ((rank >= 0 && file < 8) && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = --rank * 8 + ++file; + } + + rank = og_rank + 1, file = og_file - 1, index = rank * 8 + file; + while ((rank < 8 && file >= 0) && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = ++rank * 8 + --file; + } + } +} diff --git a/src/search/king_moves.c b/src/search/king_moves.c index 7655fbb..3f245cc 100644 --- a/src/search/king_moves.c +++ b/src/search/king_moves.c @@ -1,10 +1,4 @@ #include "../search.h" -#include - -#ifdef TEST_MOD -#include "../bitboard.c" -#include "./moves.c" -#endif void get_king_moves(moves_t* moves, position_t position) { assert_valid_position(position); @@ -42,6 +36,11 @@ void get_king_moves(moves_t* moves, position_t position) { } } +#ifdef TEST_MOD +#include +#include "../bitboard.c" +#include "./moves.c" + bool test_empty_board() { int i = 0; while (i++ < 1000000000); @@ -51,3 +50,4 @@ bool test_empty_board() { bool test_friendly_pieces() { return false; } +#endif diff --git a/src/search/pawn_moves.c b/src/search/pawn_moves.c index fbf2bad..6b85cc2 100644 --- a/src/search/pawn_moves.c +++ b/src/search/pawn_moves.c @@ -88,7 +88,7 @@ void get_pawn_moves(moves_t* moves, position_t position) { } } - if (position.passantable_column != 0) { + if (position.passantable_file != 0) { bitboard_t PASSANT_RANK; int rank_shift; int from_shift; @@ -103,15 +103,15 @@ void get_pawn_moves(moves_t* moves, position_t position) { } int left = 0, right = 0; - if (position.passantable_column > 1) left = position.passantable_column - 1; - if (position.passantable_column < 8) right = position.passantable_column + 1; + if (position.passantable_file > 1) left = position.passantable_file - 1; + if (position.passantable_file < 8) right = position.passantable_file + 1; if (left != 0) { bitboard_t MASK = file_masks[left - 1] & friendly_pawns & PASSANT_RANK; if (MASK != 0) { add_move( moves, left + rank_shift + from_shift, - position.passantable_column + rank_shift - 1 + position.passantable_file + rank_shift - 1 ); } } @@ -121,7 +121,7 @@ void get_pawn_moves(moves_t* moves, position_t position) { add_move( moves, right + rank_shift + from_shift, - position.passantable_column + rank_shift - 1 + position.passantable_file + rank_shift - 1 ); } } diff --git a/src/search/rook_moves.c b/src/search/rook_moves.c new file mode 100644 index 0000000..749210e --- /dev/null +++ b/src/search/rook_moves.c @@ -0,0 +1,63 @@ +#include "search.h" + +void get_rook_moves(moves_t* moves, position_t position) { + assert_valid_position(position); + + bitboard_t friendly_rooks; + bitboard_t friendly_pieces; + bitboard_t enemy_pieces; + if (position.turn == WHITE_TURN) { + friendly_pieces = whites(position); + enemy_pieces = blacks(position); + friendly_rooks = position.bitboards[WHITE_ROOK]; + } else { + friendly_pieces = blacks(position); + enemy_pieces = whites(position); + friendly_rooks = position.bitboards[BLACK_ROOK]; + } + + while (friendly_rooks) { + int from = __builtin_ctzll(friendly_rooks); + friendly_rooks &= friendly_rooks - 1; + + int og_rank = from / 8; // range: [0, 7] + int og_file = from % 8; // range: [0, 7] + int rank = og_rank + 1, file = og_file, index = rank * 8 + file; + + + while (rank < 8 && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = ++rank * 8 + file; + } + + rank = og_rank - 1, file = og_file, index = rank * 8 + file; + while (rank >= 0 && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = --rank * 8 + file; + } + + rank = og_rank, file = og_file + 1, index = rank * 8 + file; + while (file < 8 && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = rank * 8 + ++file; + } + + rank = og_rank, file = og_file - 1, index = rank * 8 + file; + while (file >= 0 && !occupied_by(friendly_pieces, index)) { + add_move(moves, from, index); + if (occupied_by(enemy_pieces, index)) { + break; + } + index = rank * 8 + --file; + } + } +} -- cgit v1.2.3