From 1a52152852ff29c727673fc80e8bf8eca5ed6365 Mon Sep 17 00:00:00 2001 From: Aargh Rai Date: Thu, 16 Jul 2026 14:08:24 +0530 Subject: starting pos, move count, test pass --- src/engine/moves/king.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'src/engine/moves/king.c') diff --git a/src/engine/moves/king.c b/src/engine/moves/king.c index 706cf9c..98c6ab9 100644 --- a/src/engine/moves/king.c +++ b/src/engine/moves/king.c @@ -42,15 +42,51 @@ void get_king_moves(moves_t* moves, position_t position) { .flags = ((enemy_pieces >> to) & 1) ? MOVE_CAPTURE : 0, ); } + + bitboard_t all_occupied = friendly_pieces | enemy_pieces; + if (position.turn == WHITE_TURN) { + if ((position.castling & WHITE_SHORT_CASTLE) && + king_square == 4 && + !((all_occupied >> 5) & 1) && + !((all_occupied >> 6) & 1)) + { + add_move(moves, 4, 6, .flags = MOVE_SHORT_CASTLE); + } + if ((position.castling & WHITE_LONG_CASTLE) && + king_square == 4 && + !((all_occupied >> 3) & 1) && + !((all_occupied >> 2) & 1) && + !((all_occupied >> 1) & 1)) + { + add_move(moves, 4, 2, .flags = MOVE_LONG_CASTLE); + } + } else { + if ((position.castling & BLACK_SHORT_CASTLE) && + king_square == 60 && + !((all_occupied >> 61) & 1) && + !((all_occupied >> 62) & 1)) + { + add_move(moves, 60, 62, .flags = MOVE_SHORT_CASTLE); + } + if ((position.castling & BLACK_LONG_CASTLE) && + king_square == 60 && + !((all_occupied >> 59) & 1) && + !((all_occupied >> 58) & 1) && + !((all_occupied >> 57) & 1)) + { + add_move(moves, 60, 58, .flags = MOVE_LONG_CASTLE); + } + } } #ifdef TEST_MOD #include -#include "vec.c" bool test_white_king_corners() { - moves_t moves = moves_init(); + moves_t moves; + moves_init(&moves); position_t p = {0}; + p.bitboards[BLACK_KING] = 100; p.bitboards[WHITE_KING] = 1; p.turn = WHITE_TURN; get_king_moves(&moves, p); @@ -82,12 +118,16 @@ bool test_white_king_corners() { if (moves.moves[2].from != 56) return false; if (moves.moves[2].to != 57) return false; + moves_deinit(moves); return true; } bool test_black_king_corners() { - moves_t moves = moves_init(); + moves_t moves; + moves_init(&moves); + position_t p = {0}; + p.bitboards[WHITE_KING] = 100; p.bitboards[BLACK_KING] = 1; p.turn = BLACK_TURN; get_king_moves(&moves, p); @@ -119,6 +159,7 @@ bool test_black_king_corners() { if (moves.moves[2].from != 56) return false; if (moves.moves[2].to != 57) return false; + moves_deinit(moves); return true; } #endif -- cgit v1.2.3