diff options
| author | Aargh Rai <aargh.rai+git@gmail.com> | 2026-02-25 23:46:07 +0530 |
|---|---|---|
| committer | Aargh Rai <aargh.rai+git@gmail.com> | 2026-02-25 23:46:07 +0530 |
| commit | 22c0d29724c2b054d8b9f9ed2373103d412f02b3 (patch) | |
| tree | 83be782ad10dc0568c825de7d82c20ff16db6af5 | |
| parent | b2f0457deaf8fd7c336e5370212df9202d6f3eeb (diff) | |
king DONE
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/bitboard.c | 2 | ||||
| -rw-r--r-- | src/main.c | 27 | ||||
| -rw-r--r-- | src/search.h | 1 | ||||
| -rw-r--r-- | src/search/king_moves.c | 37 | ||||
| -rw-r--r-- | src/search/knight_moves.c | 2 | ||||
| -rw-r--r-- | src/search/pawn_moves.c | 2 |
7 files changed, 57 insertions, 15 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8581a76..a74779b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,7 @@ set(SOURCES src/search/moves.c src/search/pawn_moves.c src/search/knight_moves.c + src/search/king_moves.c ) add_executable(gacrux ${SOURCES}) diff --git a/src/bitboard.c b/src/bitboard.c index 8b9a83c..afc7867 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -40,6 +40,8 @@ void assert_valid_position(position_t position) { ); assert(position.turn == WHITE_TURN || position.turn == BLACK_TURN); assert(position.passantable_column <= 8); + assert(position.bitboards[WHITE_KING] != 0); + assert(position.bitboards[BLACK_KING] != 0); } bitboard_t whites(position_t position) { @@ -6,20 +6,21 @@ 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] = 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.bitboards[WHITE_KING] = 65536ULL; +position.bitboards[WHITE_QUEEN] = 8ULL; +position.bitboards[WHITE_ROOK] = 129ULL; +position.bitboards[WHITE_BISHOP] = 36ULL; +position.bitboards[WHITE_KNIGHT] = 66ULL; +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] = 71776119061217280ULL; + - get_knight_moves(&moves, position); + get_king_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 377b3f0..7cbec7e 100644 --- a/src/search.h +++ b/src/search.h @@ -24,5 +24,6 @@ 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); #endif // !SEARCH_H diff --git a/src/search/king_moves.c b/src/search/king_moves.c new file mode 100644 index 0000000..631b7f0 --- /dev/null +++ b/src/search/king_moves.c @@ -0,0 +1,37 @@ +#include "../search.h" + +void get_king_moves(moves_t* moves, position_t position) { + assert_valid_position(position); + + bitboard_t friendly_king; + int king_square; + bitboard_t friendly_pieces; + if (position.turn == WHITE_TURN) { + friendly_pieces = whites(position); + friendly_king = position.bitboards[WHITE_KING]; + } else { + friendly_pieces = blacks(position); + friendly_king = position.bitboards[BLACK_KING]; + } + king_square = __builtin_ctzll(friendly_king); + + bitboard_t movement; + if (king_square >= 10) { + movement = 920078ULL << (king_square - 10); + } else { + movement = 920078ULL >> -(king_square - 10); + } + if (friendly_king & BITMASK_FILE_A) { + movement &= BITMASK_FILE_A | BITMASK_FILE_B; + } else if (friendly_king & BITMASK_FILE_H) { + movement &= BITMASK_FILE_G | BITMASK_FILE_H; + } + + movement &= ~friendly_pieces; + print_bitboard(movement); + while (movement) { + int to = __builtin_ctzll(movement); + movement &= movement - 1; + add_move(moves, king_square, to); + } +} diff --git a/src/search/knight_moves.c b/src/search/knight_moves.c index 55f8eec..34cf66a 100644 --- a/src/search/knight_moves.c +++ b/src/search/knight_moves.c @@ -75,6 +75,8 @@ bitboard_t knight_moves[64] = { }; void get_knight_moves(moves_t* moves, position_t position) { + assert_valid_position(position); + bitboard_t friendly_knights; bitboard_t friendly_pieces; if (position.turn == WHITE_TURN) { diff --git a/src/search/pawn_moves.c b/src/search/pawn_moves.c index 5d94571..fbf2bad 100644 --- a/src/search/pawn_moves.c +++ b/src/search/pawn_moves.c @@ -1,6 +1,4 @@ #include "../search.h" -#include <assert.h> -#include <inttypes.h> bitboard_t get_white_pawn_forwardmoves(moves_t* moves, position_t position) { bitboard_t enemy_bitboard = blacks(position); |
