summaryrefslogtreecommitdiff
path: root/src/engine/moves.c
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-06-02 17:39:09 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-06-02 17:39:09 +0530
commit058f12c699c9183f93f280ad7eb54c8a1b25f523 (patch)
treef06f21c94666243b80a5823c3932349dba4ed3cc /src/engine/moves.c
parentdf577a30f2df1d60a0254403147fd2077a7c8b42 (diff)
added move flags
will try stockfish later dw
Diffstat (limited to 'src/engine/moves.c')
-rw-r--r--src/engine/moves.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/src/engine/moves.c b/src/engine/moves.c
index ac9fbba..d338cc2 100644
--- a/src/engine/moves.c
+++ b/src/engine/moves.c
@@ -1,3 +1,5 @@
+#include "moves.h"
+#include "bitboard.h"
#include "moves/vec.c"
#include "moves/king.c"
#include "moves/knight.c"
@@ -5,6 +7,7 @@
#include "moves/rook.c"
#include "moves/bishop.c"
#include "moves/queen.c"
+#include <assert.h>
void get_moves(moves_t* moves, position_t position) {
get_pawn_moves(moves, position);
@@ -14,3 +17,100 @@ void get_moves(moves_t* moves, position_t position) {
get_bishop_moves(moves, position);
get_queen_moves(moves, position);
}
+
+int find_piece_on_square(position_t* p, int square) {
+ if ((p->bitboards[WHITE_KING] >> square) & 1) return WHITE_KING;
+ if ((p->bitboards[WHITE_QUEEN] >> square) & 1) return WHITE_QUEEN;
+ if ((p->bitboards[WHITE_ROOK] >> square) & 1) return WHITE_ROOK;
+ if ((p->bitboards[WHITE_BISHOP] >> square) & 1) return WHITE_BISHOP;
+ if ((p->bitboards[WHITE_KNIGHT] >> square) & 1) return WHITE_KNIGHT;
+ if ((p->bitboards[WHITE_PAWN] >> square) & 1) return WHITE_PAWN;
+ if ((p->bitboards[BLACK_KING] >> square) & 1) return BLACK_KING;
+ if ((p->bitboards[BLACK_QUEEN] >> square) & 1) return BLACK_QUEEN;
+ if ((p->bitboards[BLACK_ROOK] >> square) & 1) return BLACK_ROOK;
+ if ((p->bitboards[BLACK_BISHOP] >> square) & 1) return BLACK_BISHOP;
+ if ((p->bitboards[BLACK_KNIGHT] >> square) & 1) return BLACK_KNIGHT;
+ if ((p->bitboards[BLACK_PAWN] >> square) & 1) return BLACK_PAWN;
+ assert(0);
+}
+
+void position_make_move(position_t* position, move_t* move) {
+ if (move->flags & MOVE_LONG_CASTLE) {
+ if (position->turn == WHITE_TURN) {
+ assert(position->bitboards[WHITE_KING] == 16);
+ assert((position->bitboards[WHITE_ROOK] >> 0) & 1);
+
+ position->bitboards[WHITE_KING] = 2;
+ position->bitboards[WHITE_ROOK] += 3;
+ } else if (position->turn == BLACK_TURN) {
+ assert(position->bitboards[BLACK_KING] == 1152921504606846976ULL);
+ assert((position->bitboards[BLACK_ROOK] >> 56) & 1);
+
+ position->bitboards[BLACK_KING] = 144115188075855872ULL;
+ position->bitboards[WHITE_ROOK] &= ~((bitboard_t)1 << 56);
+ position->bitboards[WHITE_ROOK] |= (bitboard_t)1 << 58;
+ }
+ return;
+ }
+ if (move->flags & MOVE_SHORT_CASTLE) {
+ if (position->turn == WHITE_TURN) {
+ assert(position->bitboards[WHITE_KING] == 16);
+ assert((position->bitboards[WHITE_ROOK] >> 7) & 1);
+
+ position->bitboards[WHITE_KING] = 64;
+ position->bitboards[WHITE_ROOK] &= ~((bitboard_t)1 << 7);
+ position->bitboards[WHITE_ROOK] |= (bitboard_t)1 << 5;
+ } else if (position->turn == BLACK_TURN) {
+ assert(position->bitboards[BLACK_KING] == 1152921504606846976ULL);
+ assert((position->bitboards[BLACK_ROOK] >> 63) & 1);
+
+ position->bitboards[WHITE_KING] = 4611686018427387904ULL;
+ position->bitboards[WHITE_ROOK] &= ~((bitboard_t)1 << 63);
+ position->bitboards[WHITE_ROOK] |= (bitboard_t)1 << 61;
+ }
+ return;
+ }
+
+ int piece_type = find_piece_on_square(position, move->from);
+ position->bitboards[piece_type] &= ~(1 << move->from);
+ if (move->flags & MOVE_PROMOTE_Q) {
+ int q_type = position->turn == WHITE_TURN ? WHITE_QUEEN : BLACK_QUEEN;
+ position->bitboards[q_type] |= 1 << move->to;
+ return;
+ }
+ if (move->flags & MOVE_PROMOTE_R) {
+ int q_type = position->turn == WHITE_TURN ? WHITE_ROOK : BLACK_ROOK;
+ position->bitboards[q_type] |= 1 << move->to;
+ return;
+ }
+ if (move->flags & MOVE_PROMOTE_B) {
+ int q_type = position->turn == WHITE_TURN ? WHITE_BISHOP : BLACK_BISHOP;
+ position->bitboards[q_type] |= 1 << move->to;
+ return;
+ }
+ if (move->flags & MOVE_PROMOTE_N) {
+ int q_type = position->turn == WHITE_TURN ? WHITE_KNIGHT : BLACK_KNIGHT;
+ position->bitboards[q_type] |= 1 << move->to;
+ return;
+ }
+ if (move->flags & MOVE_EN_PASSANT) {
+ int target_sqr;
+ if (position->turn == WHITE_TURN) target_sqr = move->to - 8;
+ else target_sqr = move->to + 8;
+
+ int to_remove_piece_type = find_piece_on_square(position, target_sqr);
+ position->bitboards[piece_type] |= 1 << move->to;
+ position->bitboards[to_remove_piece_type] &= ~(1 << target_sqr);
+ return;
+ }
+
+ if (move->flags & MOVE_CAPTURE) {
+ int to_remove_piece_type = find_piece_on_square(position, move->to);
+ position->bitboards[to_remove_piece_type] &= ~(1 << move->to);
+ }
+
+ // i can't put this above the find_piece_on_square function, because there
+ // is a possibility that piece_type would resolve to the piece that is being
+ // moved, which would mess with the ~(1 << to_sqr)
+ position->bitboards[piece_type] |= 1 << move->to;
+}