summaryrefslogtreecommitdiff
path: root/src/search
diff options
context:
space:
mode:
Diffstat (limited to 'src/search')
-rw-r--r--src/search/bishop_moves.c63
-rw-r--r--src/search/king_moves.c12
-rw-r--r--src/search/pawn_moves.c10
-rw-r--r--src/search/rook_moves.c63
4 files changed, 137 insertions, 11 deletions
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 <stdbool.h>
-
-#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 <stdbool.h>
+#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;
+ }
+ }
+}