summaryrefslogtreecommitdiff
path: root/src/engine/moves/rook.c
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-07-16 15:30:08 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-07-18 14:03:55 +0530
commitf1c6f59ae02a9d9db50171a69032dc2409a0e6c6 (patch)
tree3c5d358f62a4d5925e8b768bcc3e79e9a7c9cf2c /src/engine/moves/rook.c
parent1a52152852ff29c727673fc80e8bf8eca5ed6365 (diff)
magic bitboards via AI
Diffstat (limited to 'src/engine/moves/rook.c')
-rw-r--r--src/engine/moves/rook.c50
1 files changed, 6 insertions, 44 deletions
diff --git a/src/engine/moves/rook.c b/src/engine/moves/rook.c
index abbc784..0938de9 100644
--- a/src/engine/moves/rook.c
+++ b/src/engine/moves/rook.c
@@ -7,53 +7,15 @@ void __forloop_rook_moves_gen(
bitboard_t friendly_pieces,
bitboard_t enemy_pieces
) {
+ bitboard_t all_occupied = friendly_pieces | enemy_pieces;
while (friendly_type) {
int from = __builtin_ctzll(friendly_type);
friendly_type &= friendly_type - 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;
-
-
- bitboard_t occupied;
- while (rank < 8 && !occupied_by(friendly_pieces, index)) {
- occupied = occupied_by(enemy_pieces, index);
- add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
- if (occupied) {
- break;
- }
- index = ++rank * 8 + file;
- }
-
- rank = og_rank - 1, file = og_file, index = rank * 8 + file;
- while (rank >= 0 && !occupied_by(friendly_pieces, index)) {
- occupied = occupied_by(enemy_pieces, index);
- add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
- if (occupied) {
- break;
- }
- index = --rank * 8 + file;
- }
-
- rank = og_rank, file = og_file + 1, index = rank * 8 + file;
- while (file < 8 && !occupied_by(friendly_pieces, index)) {
- occupied = occupied_by(enemy_pieces, index);
- add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
- if (occupied) {
- break;
- }
- index = rank * 8 + ++file;
- }
-
- rank = og_rank, file = og_file - 1, index = rank * 8 + file;
- while (file >= 0 && !occupied_by(friendly_pieces, index)) {
- occupied = occupied_by(enemy_pieces, index);
- add_move(moves, from, index, .flags = occupied ? MOVE_CAPTURE : 0);
- if (occupied) {
- break;
- }
- index = rank * 8 + --file;
+ bitboard_t attacks = get_rook_attacks(from, all_occupied) & ~friendly_pieces;
+ while (attacks) {
+ int to = __builtin_ctzll(attacks);
+ attacks &= attacks - 1;
+ add_move(moves, from, to, .flags = ((enemy_pieces >> to) & 1) ? MOVE_CAPTURE : 0);
}
}
}