summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-02-25 23:22:10 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-02-25 23:22:10 +0530
commitb2f0457deaf8fd7c336e5370212df9202d6f3eeb (patch)
tree42105b64d453c3aa59ec8890df1c0160513bf0ee /src
parent4c051c50de1c1284c438de87881f52a6180fdf8e (diff)
knights DONE?
Diffstat (limited to 'src')
-rw-r--r--src/main.c41
-rw-r--r--src/search.h1
-rw-r--r--src/search/knight_moves.c99
3 files changed, 113 insertions, 28 deletions
diff --git a/src/main.c b/src/main.c
index c2a2a52..537bf32 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,35 +6,20 @@ int main() {
moves_t moves = moves_init();
position_t position = position_starting();
-// position.bitboards[WHITE_KING] = 16ULL;
-// position.bitboards[WHITE_QUEEN] = 8ULL;
-// position.bitboards[WHITE_ROOK] = 129ULL;
-// position.bitboards[WHITE_BISHOP] = 36ULL;
-// position.bitboards[WHITE_KNIGHT] = 66ULL;
-// position.bitboards[WHITE_PAWN] = 9261338880ULL;
-// 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_PAWN] = 43628777082716160ULL;
-position.bitboards[WHITE_KING] = 16ULL;
-position.bitboards[WHITE_QUEEN] = 274877906944ULL;
-position.bitboards[WHITE_ROOK] = 33554560ULL;
-position.bitboards[WHITE_BISHOP] = 36ULL;
-position.bitboards[WHITE_KNIGHT] = 66ULL;
-position.bitboards[WHITE_PAWN] = 18016057038217216ULL;
-position.bitboards[BLACK_KING] = 1152921504606846976ULL;
-position.bitboards[BLACK_QUEEN] = 576460752303423488ULL;
-position.bitboards[BLACK_ROOK] = 9295429630892703744ULL;
-position.bitboards[BLACK_BISHOP] = 288300744895889408ULL;
-position.bitboards[BLACK_KNIGHT] = 4755801206503243776ULL;
-position.bitboards[BLACK_PAWN] = 7459109431427072ULL;
+ position.bitboards[WHITE_KING] = 16ULL;
+ position.bitboards[WHITE_QUEEN] = 8ULL;
+ position.bitboards[WHITE_ROOK] = 129ULL;
+ position.bitboards[WHITE_BISHOP] = 36ULL;
+ position.bitboards[WHITE_KNIGHT] = 262208ULL;
+ position.bitboards[WHITE_PAWN] = 65280ULL;
+ 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_PAWN] = 69524353607270400ULL;
- position.passantable_column = 3;
- position.turn = BLACK_TURN;
-
- get_pawn_moves(&moves, position);
+ get_knight_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.h b/src/search.h
index f613294..377b3f0 100644
--- a/src/search.h
+++ b/src/search.h
@@ -23,5 +23,6 @@ moves_t moves_empty();
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);
#endif // !SEARCH_H
diff --git a/src/search/knight_moves.c b/src/search/knight_moves.c
new file mode 100644
index 0000000..55f8eec
--- /dev/null
+++ b/src/search/knight_moves.c
@@ -0,0 +1,99 @@
+#include "../search.h"
+
+bitboard_t knight_moves[64] = {
+ 132096ULL,
+ 329728ULL,
+ 659712ULL,
+ 659712ULL >> 1,
+ 659712ULL >> 2,
+ 659712ULL >> 3,
+ 10489856ULL,
+ 4202496ULL,
+
+ 33816580ULL,
+ 84410376ULL,
+ 168886289ULL,
+ 168886289ULL >> 1,
+ 168886289ULL >> 2,
+ 168886289ULL >> 3,
+ 2685403152ULL,
+ 1075839008ULL,
+
+ 8657044482ULL,
+ 21609056261ULL,
+ 43234889994ULL,
+ 43234889994ULL >> 1,
+ 43234889994ULL >> 2,
+ 43234889994ULL >> 3,
+ 687463207072ULL,
+ 275414786112ULL,
+
+ 8657044482ULL >> 8,
+ 21609056261ULL >> 8,
+ 43234889994ULL >> 8,
+ 43234889994ULL >> (8 + 1),
+ 43234889994ULL >> (8 + 2),
+ 43234889994ULL >> (8 + 3),
+ 687463207072ULL >> 8,
+ 275414786112ULL >> 8,
+
+ 8657044482ULL >> 16,
+ 21609056261ULL >> 16,
+ 43234889994ULL >> 16,
+ 43234889994ULL >> (16 + 1),
+ 43234889994ULL >> (16 + 2),
+ 43234889994ULL >> (16 + 3),
+ 687463207072ULL >> 16,
+ 275414786112ULL >> 16,
+
+ 8657044482ULL >> 24,
+ 21609056261ULL >> 24,
+ 43234889994ULL >> 24,
+ 43234889994ULL >> (24 + 1),
+ 43234889994ULL >> (24 + 2),
+ 43234889994ULL >> (24 + 3),
+ 687463207072ULL >> 24,
+ 275414786112ULL >> 24,
+
+ 288234782788157440ULL,
+ 576469569871282176ULL,
+ 1224997833292120064ULL,
+ 1224997833292120064ULL >> 1,
+ 1224997833292120064ULL >> 2,
+ 1224997833292120064ULL >> 3,
+ 1152939783987658752ULL,
+ 2305878468463689728ULL,
+
+ 1128098930098176ULL,
+ 2257297371824128ULL,
+ 4796069720358912ULL,
+ 4796069720358912ULL >> 1,
+ 4796069720358912ULL >> 2,
+ 4796069720358912ULL >> 3,
+ 4679521487814656ULL,
+ 9077567998918656ULL,
+};
+
+void get_knight_moves(moves_t* moves, position_t position) {
+ bitboard_t friendly_knights;
+ bitboard_t friendly_pieces;
+ if (position.turn == WHITE_TURN) {
+ friendly_pieces = whites(position);
+ friendly_knights = position.bitboards[WHITE_KNIGHT];
+ } else {
+ friendly_pieces = blacks(position);
+ friendly_knights = position.bitboards[BLACK_KNIGHT];
+ }
+
+ while (friendly_knights) {
+ int from = __builtin_ctzll(friendly_knights);
+ friendly_knights &= friendly_knights - 1;
+ bitboard_t ATTACK_MASK = knight_moves[from];
+ ATTACK_MASK &= ~friendly_pieces;
+ while (ATTACK_MASK) {
+ int to = __builtin_ctzll(ATTACK_MASK);
+ ATTACK_MASK &= ATTACK_MASK - 1;
+ add_move(moves, from, to);
+ }
+ }
+}