summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAargh Rai <aargh.rai+git@gmail.com>2026-02-20 17:04:46 +0530
committerAargh Rai <aargh.rai+git@gmail.com>2026-02-20 17:04:46 +0530
commit9cd36deffadc4f5ab83f08c2999407b31354027e (patch)
treee298606fb938ac6aea26c408c3cd29ddf4b17f9c /src
parentfd57b964e50a4cb3385976a473bc287d682bcbdb (diff)
some moves dynamic array setup & starting position
Diffstat (limited to 'src')
-rw-r--r--src/bitboard.c25
-rw-r--r--src/bitboard.h38
-rw-r--r--src/ints.h19
-rw-r--r--src/main.c1
-rw-r--r--src/search.h24
-rw-r--r--src/search/moves.c44
-rw-r--r--src/search/pawn_moves.c0
7 files changed, 151 insertions, 0 deletions
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
diff --git a/src/main.c b/src/main.c
index 2328803..297ef20 100644
--- a/src/main.c
+++ b/src/main.c
@@ -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