diff options
| author | Aargh Rai <aargh.rai+git@gmail.com> | 2026-02-24 22:49:43 +0530 |
|---|---|---|
| committer | Aargh Rai <aargh.rai+git@gmail.com> | 2026-02-24 22:49:43 +0530 |
| commit | 63fa6656749d1cdb6a54a001950048a9660f0d73 (patch) | |
| tree | 39c7e0586c2ad761d65f56bdfd6c96f6bbfdccb9 /src | |
| parent | 9cd36deffadc4f5ab83f08c2999407b31354027e (diff) | |
pawn forward movement works?
Diffstat (limited to 'src')
| -rw-r--r-- | src/bitboard.c | 64 | ||||
| -rw-r--r-- | src/bitboard.h | 32 | ||||
| -rw-r--r-- | src/main.c | 4 | ||||
| -rw-r--r-- | src/search.h | 9 | ||||
| -rw-r--r-- | src/search/moves.c | 13 | ||||
| -rw-r--r-- | src/search/pawn_moves.c | 47 |
6 files changed, 150 insertions, 19 deletions
diff --git a/src/bitboard.c b/src/bitboard.c index 05b370b..ce994c5 100644 --- a/src/bitboard.c +++ b/src/bitboard.c @@ -1,25 +1,71 @@ #include "bitboard.h" +#include <stdio.h> +#include <inttypes.h> +#include <assert.h> -position starting_position() { - return (position) { +position_t position_starting() { + return (position_t) { .bitboards = { + 16ULL, + 8ULL, + 129ULL, + 36ULL, + 66ULL, + 65280ULL, 1152921504606846976ULL, 576460752303423488ULL, 9295429630892703744ULL, - 4755801206503243776ULL, 2594073385365405696ULL, + 4755801206503243776ULL, 71776119061217280ULL, - 16ULL, - 8ULL, - 129ULL, - 66ULL, - 36ULL, - 65280ULL }, .castling = (1 << WHITE_SHORT_CASTLE) | (1 << WHITE_LONG_CASTLE) | (1 << BLACK_SHORT_CASTLE) | (1 << BLACK_LONG_CASTLE), + .turn = WHITE_TURN, }; } + +void assert_valid_position(position_t position) { + assert(position.castling <= + ( + (1 << WHITE_SHORT_CASTLE) | + (1 << WHITE_LONG_CASTLE) | + (1 << BLACK_SHORT_CASTLE) | + (1 << BLACK_LONG_CASTLE) + ) + ); + assert(position.turn == WHITE_TURN || position.turn == BLACK_TURN); +} + +bitboard_t whites(position_t position) { + return + position.bitboards[WHITE_KING] | + position.bitboards[WHITE_QUEEN] | + position.bitboards[WHITE_ROOK] | + position.bitboards[WHITE_BISHOP] | + position.bitboards[WHITE_KNIGHT] | + position.bitboards[WHITE_PAWN]; +} + +bitboard_t blacks(position_t position) { + return + position.bitboards[BLACK_KING] | + position.bitboards[BLACK_QUEEN] | + position.bitboards[BLACK_ROOK] | + position.bitboards[BLACK_BISHOP] | + position.bitboards[BLACK_KNIGHT] | + position.bitboards[BLACK_PAWN]; +} + +void print_bitboard(bitboard_t bitboard) { + printf("Bitboard(%" PRIu64 ")\n", bitboard); + for (int i = 7; i >= 0; i--) { + for (int j = 0; j < 8; j++) { + printf("%" PRIu64 "", (bitboard >> (8 * i + j)) & 1); + } + printf("\n"); + } +} diff --git a/src/bitboard.h b/src/bitboard.h index b92d67f..a816ea6 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -28,11 +28,39 @@ enum { BLACK_LONG_CASTLE, }; +enum { + WHITE_TURN, + BLACK_TURN, +}; + +#define BITMASK_RANK_A 255ULL +#define BITMASK_RANK_B 65280ULL +#define BITMASK_RANK_C 16711680ULL +#define BITMASK_RANK_D 4278190080ULL +#define BITMASK_RANK_E 1095216660480ULL +#define BITMASK_RANK_F 280375465082880ULL +#define BITMASK_RANK_G 71776119061217280ULL +#define BITMASK_RANK_H 18374686479671623680ULL +#define BITMASK_COLM_1 72340172838076673ULL +#define BITMASK_COLM_2 144680345676153346ULL +#define BITMASK_COLM_3 289360691352306692ULL +#define BITMASK_COLM_4 578721382704613384ULL +#define BITMASK_COLM_5 1157442765409226768ULL +#define BITMASK_COLM_6 2314885530818453536ULL +#define BITMASK_COLM_7 4629771061636907072ULL +#define BITMASK_COLM_8 9259542123273814144ULL + typedef struct { bitboard_t bitboards[PIECE_TYPE_COUNT]; u8 castling; -} position; + u8 turn; +} position_t; + +position_t position_starting(); +void assert_valid_position(position_t position); +bitboard_t whites(position_t position); +bitboard_t blacks(position_t position); -position starting_position(); +void print_bitboard(bitboard_t bitboard); #endif // !BITBOARD_H @@ -1,7 +1,11 @@ #include <stdio.h> #include "bitboard.h" +#include "search.h" int main() { + moves_t moves = moves_init(); + position_t position = position_starting(); + get_pawn_moves(&moves, position); printf("Hello World\n"); return 0; } diff --git a/src/search.h b/src/search.h index 153b865..f613294 100644 --- a/src/search.h +++ b/src/search.h @@ -2,6 +2,7 @@ #define SEARCH_H #include "ints.h" +#include "bitboard.h" typedef u8 square_t; @@ -16,9 +17,11 @@ typedef struct { u32 capacity; } moves_t; -moves_t init_moves(); -moves_t init_moves_wcapacity(u32 capacity); -moves_t empty_moves(); +moves_t moves_init(); +moves_t moves_init_wcapacity(u32 capacity); +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); + #endif // !SEARCH_H diff --git a/src/search/moves.c b/src/search/moves.c index 57797e7..7089886 100644 --- a/src/search/moves.c +++ b/src/search/moves.c @@ -1,11 +1,12 @@ #include "../search.h" +#include <assert.h> #include <stdlib.h> -moves_t init_moves() { - return init_moves_wcapacity(16); +moves_t moves_init() { + return moves_init_wcapacity(16); } -moves_t init_moves_wcapacity(u32 capacity) { +moves_t moves_init_wcapacity(u32 capacity) { move_t* moves = malloc(capacity * sizeof(*moves)); return (moves_t) { .moves = moves, @@ -14,7 +15,7 @@ moves_t init_moves_wcapacity(u32 capacity) { }; } -moves_t empty_moves() { +moves_t moves_empty() { return (moves_t) { .moves = 0, .capacity = 0, @@ -28,7 +29,9 @@ int min(int a, int b) { } void add_move(moves_t* moves, square_t from, square_t to) { - if (moves->moves == 0) return; + assert(moves->moves != 0); + assert(from != to); + if (moves->length + 1 >= moves->capacity) { moves->capacity += min(32, moves->capacity); moves->moves = realloc( diff --git a/src/search/pawn_moves.c b/src/search/pawn_moves.c index e69de29..58eac83 100644 --- a/src/search/pawn_moves.c +++ b/src/search/pawn_moves.c @@ -0,0 +1,47 @@ +#include "../search.h" +#include <assert.h> + +void get_white_pawn_moves(moves_t* moves, position_t position) { + bitboard_t enemy_bitboard = blacks(position); + bitboard_t friendly_pieces = + position.bitboards[WHITE_KING] | + position.bitboards[WHITE_QUEEN] | + position.bitboards[WHITE_ROOK] | + position.bitboards[WHITE_BISHOP] | + position.bitboards[WHITE_KNIGHT]; + bitboard_t friendly_pawns = position.bitboards[WHITE_PAWN]; + bitboard_t y = friendly_pieces | enemy_bitboard; + bitboard_t x = ~((friendly_pawns << 8) & y) & (friendly_pawns << 8); + bitboard_t first_move_x = x | BITMASK_RANK_C; + + first_move_x |= (first_move_x << 8) & (~y); + x |= first_move_x; + + print_bitboard(x); +} +void get_black_pawn_moves(moves_t* moves, position_t position) { + bitboard_t enemy_bitboard = whites(position); + bitboard_t friendly_pieces = + position.bitboards[BLACK_KING] | + position.bitboards[BLACK_QUEEN] | + position.bitboards[BLACK_ROOK] | + position.bitboards[BLACK_BISHOP] | + position.bitboards[BLACK_KNIGHT]; + bitboard_t friendly_pawns = position.bitboards[BLACK_PAWN]; + bitboard_t y = friendly_pieces | enemy_bitboard; + bitboard_t x = ~((friendly_pawns << 8) & y) & (friendly_pawns << 8); + bitboard_t first_move_x = x | BITMASK_RANK_F; + + first_move_x |= (first_move_x >> 8) & (~y); + x |= first_move_x; +} + +void get_pawn_moves(moves_t* moves, position_t position) { + assert_valid_position(position); + + if (position.turn == WHITE_TURN) { + get_white_pawn_moves(moves, position); + } else { + get_black_pawn_moves(moves, position); + } +} |
