summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bitboard.c20
-rw-r--r--src/bitboard.h6
-rw-r--r--src/main.c9
-rw-r--r--src/search.c2
-rw-r--r--src/search.h3
-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
9 files changed, 160 insertions, 28 deletions
diff --git a/src/bitboard.c b/src/bitboard.c
index afc7867..4746020 100644
--- a/src/bitboard.c
+++ b/src/bitboard.c
@@ -20,26 +20,26 @@ position_t position_starting() {
71776119061217280ULL,
},
.castling =
- (1 << WHITE_SHORT_CASTLE) |
- (1 << WHITE_LONG_CASTLE) |
- (1 << BLACK_SHORT_CASTLE) |
- (1 << BLACK_LONG_CASTLE),
+ ((bitboard_t)1 << WHITE_SHORT_CASTLE) |
+ ((bitboard_t)1 << WHITE_LONG_CASTLE) |
+ ((bitboard_t)1 << BLACK_SHORT_CASTLE) |
+ ((bitboard_t)1 << BLACK_LONG_CASTLE),
.turn = WHITE_TURN,
- .passantable_column = 0,
+ .passantable_file = 0,
};
}
void assert_valid_position(position_t position) {
assert(position.castling <=
(
- (1 << WHITE_SHORT_CASTLE) |
- (1 << WHITE_LONG_CASTLE) |
- (1 << BLACK_SHORT_CASTLE) |
- (1 << BLACK_LONG_CASTLE)
+ ((bitboard_t)1 << WHITE_SHORT_CASTLE) |
+ ((bitboard_t)1 << WHITE_LONG_CASTLE) |
+ ((bitboard_t)1 << BLACK_SHORT_CASTLE) |
+ ((bitboard_t)1 << BLACK_LONG_CASTLE)
)
);
assert(position.turn == WHITE_TURN || position.turn == BLACK_TURN);
- assert(position.passantable_column <= 8);
+ assert(position.passantable_file <= 8);
assert(position.bitboards[WHITE_KING] != 0);
assert(position.bitboards[BLACK_KING] != 0);
}
diff --git a/src/bitboard.h b/src/bitboard.h
index a3735d1..912cad0 100644
--- a/src/bitboard.h
+++ b/src/bitboard.h
@@ -50,11 +50,13 @@ enum {
#define BITMASK_FILE_G 4629771061636907072ULL
#define BITMASK_FILE_H 9259542123273814144ULL
+#define occupied_by(bitboard, index) (bitboard & ((bitboard_t)1 << index))
+
typedef struct {
bitboard_t bitboards[PIECE_TYPE_COUNT];
u8 castling;
u8 turn;
- u8 passantable_column;
+ u8 passantable_file;
} position_t;
position_t position_starting();
@@ -64,4 +66,4 @@ bitboard_t blacks(position_t position);
void print_bitboard(bitboard_t bitboard);
-#endif // !BITBOARD_H
+#endif // BITBOARD_H
diff --git a/src/main.c b/src/main.c
index 7f7812f..b5d57eb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,9 +6,9 @@ int main() {
moves_t moves = moves_init();
position_t position = position_starting();
-position.bitboards[WHITE_KING] = 65536ULL;
+position.bitboards[WHITE_KING] = 16ULL;
position.bitboards[WHITE_QUEEN] = 8ULL;
-position.bitboards[WHITE_ROOK] = 129ULL;
+position.bitboards[WHITE_ROOK] = 16777344ULL;
position.bitboards[WHITE_BISHOP] = 36ULL;
position.bitboards[WHITE_KNIGHT] = 66ULL;
position.bitboards[WHITE_PAWN] = 65280ULL;
@@ -16,11 +16,10 @@ position.bitboards[BLACK_KING] = 1152921504606846976ULL;
position.bitboards[BLACK_QUEEN] = 576460752303423488ULL;
position.bitboards[BLACK_ROOK] = 9295429630892703744ULL;
position.bitboards[BLACK_BISHOP] = 2594073385365405696ULL;
-position.bitboards[BLACK_KNIGHT] = 4755801206503243776ULL;
+position.bitboards[BLACK_KNIGHT] = 4611687117939015680ULL;
position.bitboards[BLACK_PAWN] = 71776119061217280ULL;
-
- get_king_moves(&moves, position);
+ get_rook_moves(&moves, position);
for (int i = 0; i < moves.length; i++) {
printf("%d %d | ", (moves.moves[i].from), (moves.moves[i].to));
diff --git a/src/search.c b/src/search.c
index 9c7b6ff..03b5eb5 100644
--- a/src/search.c
+++ b/src/search.c
@@ -2,3 +2,5 @@
#include "search/king_moves.c"
#include "search/knight_moves.c"
#include "search/pawn_moves.c"
+#include "search/rook_moves.c"
+#include "search/bishop_moves.c"
diff --git a/src/search.h b/src/search.h
index 7cbec7e..14f876f 100644
--- a/src/search.h
+++ b/src/search.h
@@ -25,5 +25,8 @@ void add_move(moves_t* moves, square_t from, square_t to);
void get_pawn_moves(moves_t* moves, position_t position);
void get_knight_moves(moves_t* moves, position_t position);
void get_king_moves(moves_t* moves, position_t position);
+void get_rook_moves(moves_t* moves, position_t position);
+void get_bishop_moves(moves_t* moves, position_t position);
+void get_queen_moves(moves_t* moves, position_t position);
#endif // !SEARCH_H
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;
+ }
+ }
+}