summaryrefslogtreecommitdiff
path: root/src/engine/moves/king.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine/moves/king.c')
-rw-r--r--src/engine/moves/king.c47
1 files changed, 44 insertions, 3 deletions
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 <stdbool.h>
-#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