diff options
| author | Aargh Rai <aargh.rai+git@gmail.com> | 2026-02-20 17:04:46 +0530 |
|---|---|---|
| committer | Aargh Rai <aargh.rai+git@gmail.com> | 2026-02-20 17:04:46 +0530 |
| commit | 9cd36deffadc4f5ab83f08c2999407b31354027e (patch) | |
| tree | e298606fb938ac6aea26c408c3cd29ddf4b17f9c | |
| parent | fd57b964e50a4cb3385976a473bc287d682bcbdb (diff) | |
some moves dynamic array setup & starting position
| -rw-r--r-- | CMakeLists.txt | 3 | ||||
| -rw-r--r-- | bitboards.html | 2 | ||||
| -rw-r--r-- | src/bitboard.c | 25 | ||||
| -rw-r--r-- | src/bitboard.h | 38 | ||||
| -rw-r--r-- | src/ints.h | 19 | ||||
| -rw-r--r-- | src/main.c | 1 | ||||
| -rw-r--r-- | src/search.h | 24 | ||||
| -rw-r--r-- | src/search/moves.c | 44 | ||||
| -rw-r--r-- | src/search/pawn_moves.c | 0 |
9 files changed, 155 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 8691b53..33ad32f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,9 @@ project(Gacrux LANGUAGES C) set(SOURCES src/main.c + src/bitboard.c + src/search/moves.c + src/search/pawn_moves.c ) add_executable(gacrux ${SOURCES}) diff --git a/bitboards.html b/bitboards.html index 94006a7..b9db958 100644 --- a/bitboards.html +++ b/bitboards.html @@ -100,7 +100,7 @@ const label = document.createElement("div"); label.classList.add("horizontal_label"); board.appendChild(label); - for (let i = 0; i < 8; i++) { + for (let i = 1; i < 9; i++) { const label = document.createElement("div"); label.classList.add("horizontal_label"); label.textContent = i; diff --git a/src/bitboard.c b/src/bitboard.c new file mode 100644 index 0000000..05b370b --- /dev/null +++ b/src/bitboard.c @@ -0,0 +1,25 @@ +#include "bitboard.h" + +position starting_position() { + return (position) { + .bitboards = { + 1152921504606846976ULL, + 576460752303423488ULL, + 9295429630892703744ULL, + 4755801206503243776ULL, + 2594073385365405696ULL, + 71776119061217280ULL, + 16ULL, + 8ULL, + 129ULL, + 66ULL, + 36ULL, + 65280ULL + }, + .castling = + (1 << WHITE_SHORT_CASTLE) | + (1 << WHITE_LONG_CASTLE) | + (1 << BLACK_SHORT_CASTLE) | + (1 << BLACK_LONG_CASTLE), + }; +} diff --git a/src/bitboard.h b/src/bitboard.h new file mode 100644 index 0000000..b92d67f --- /dev/null +++ b/src/bitboard.h @@ -0,0 +1,38 @@ +#ifndef BITBOARD_H +#define BITBOARD_H + +#include "ints.h" + +typedef u64 bitboard_t; + +enum { + WHITE_KING, + WHITE_QUEEN, + WHITE_ROOK, + WHITE_BISHOP, + WHITE_KNIGHT, + WHITE_PAWN, + BLACK_KING, + BLACK_QUEEN, + BLACK_ROOK, + BLACK_BISHOP, + BLACK_KNIGHT, + BLACK_PAWN, + PIECE_TYPE_COUNT, +}; + +enum { + WHITE_SHORT_CASTLE, + WHITE_LONG_CASTLE, + BLACK_SHORT_CASTLE, + BLACK_LONG_CASTLE, +}; + +typedef struct { + bitboard_t bitboards[PIECE_TYPE_COUNT]; + u8 castling; +} position; + +position starting_position(); + +#endif // !BITBOARD_H diff --git a/src/ints.h b/src/ints.h new file mode 100644 index 0000000..b134fe6 --- /dev/null +++ b/src/ints.h @@ -0,0 +1,19 @@ +#ifndef INTS_H +#define INTS_H + +#include <stdint.h> + +typedef uint8_t u8; +typedef uint16_t u16; +typedef uint32_t u32; +typedef uint64_t u64; +typedef __uint128_t u128; +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; +typedef __int128_t i128; +typedef float f32; +typedef double f64; + +#endif // !INTS_H @@ -1,4 +1,5 @@ #include <stdio.h> +#include "bitboard.h" int main() { printf("Hello World\n"); diff --git a/src/search.h b/src/search.h new file mode 100644 index 0000000..153b865 --- /dev/null +++ b/src/search.h @@ -0,0 +1,24 @@ +#ifndef SEARCH_H +#define SEARCH_H + +#include "ints.h" + +typedef u8 square_t; + +typedef struct { + square_t from; + square_t to; +} move_t; + +typedef struct { + move_t* moves; + u32 length; + u32 capacity; +} moves_t; + +moves_t init_moves(); +moves_t init_moves_wcapacity(u32 capacity); +moves_t empty_moves(); +void add_move(moves_t* moves, square_t from, square_t to); + +#endif // !SEARCH_H diff --git a/src/search/moves.c b/src/search/moves.c new file mode 100644 index 0000000..57797e7 --- /dev/null +++ b/src/search/moves.c @@ -0,0 +1,44 @@ +#include "../search.h" +#include <stdlib.h> + +moves_t init_moves() { + return init_moves_wcapacity(16); +} + +moves_t init_moves_wcapacity(u32 capacity) { + move_t* moves = malloc(capacity * sizeof(*moves)); + return (moves_t) { + .moves = moves, + .capacity = capacity, + .length = 0, + }; +} + +moves_t empty_moves() { + return (moves_t) { + .moves = 0, + .capacity = 0, + .length = 0, + }; +} + +int min(int a, int b) { + if (a > b) return b; + return a; +} + +void add_move(moves_t* moves, square_t from, square_t to) { + if (moves->moves == 0) return; + if (moves->length + 1 >= moves->capacity) { + moves->capacity += min(32, moves->capacity); + moves->moves = realloc( + moves->moves, + moves->capacity * sizeof(*moves->moves) + ); + } + moves->moves[moves->length] = (move_t){ + .from = from, + .to = to + }; + moves->length++; +} diff --git a/src/search/pawn_moves.c b/src/search/pawn_moves.c new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/search/pawn_moves.c |
