diff --git a/engine/include/bitboard.h b/engine/include/bitboard.h index 7e1f246..c2ac730 100644 --- a/engine/include/bitboard.h +++ b/engine/include/bitboard.h @@ -52,6 +52,7 @@ struct Board { int fullmove_number; }; -void create_knight_attack_cache(void); -void create_pawn_attack_cache(void); -void create_king_attack_cache(void); \ No newline at end of file +void create_knight_attack_cache(); +void create_pawn_attack_cache(); +void create_king_attack_cache(); +void print_board(); \ No newline at end of file diff --git a/engine/include/fen.h b/engine/include/fen.h new file mode 100644 index 0000000..1decd7c --- /dev/null +++ b/engine/include/fen.h @@ -0,0 +1 @@ +int load_fen(); diff --git a/engine/src/bitboard.c b/engine/src/bitboard.c index 8d97cb0..f34239c 100644 --- a/engine/src/bitboard.c +++ b/engine/src/bitboard.c @@ -1,4 +1,5 @@ #include "bitboard.h" +#include uint64_t PAWN_ATTACKS[2][64]; uint64_t KNIGHT_ATTACKS[64]; @@ -58,4 +59,45 @@ void create_king_attack_cache(void) { KING_ATTACKS[sq] = mask; } +} + +static int first_set_index(uint64_t bb) { + for (int i = 0; i < 64; ++i) { + if ((bb >> i) & 1ULL) return i; + } + return -1; +} + +static int pop_lsb_index(uint64_t *bb) { + if (*bb == 0) return -1; + int idx = first_set_index(*bb); + // Clears bit. + *bb &= ~(1ULL << idx); + return idx; +} + +void print_board(const struct Board *b) { + static const char PIECE_CH[12] = { + 'P','N','B','R','Q','K', + 'p','n','b','r','q','k' + }; + + char grid[64]; + for (int i = 0; i < 64; ++i) grid[i] = '.'; + + for (int p = 0; p < 12; ++p) { + uint64_t bb = b->pieces[p]; + while (bb) { + int sq = pop_lsb_index(&bb); + grid[sq] = PIECE_CH[p]; + } + } + + for (int r = 7; r >= 0; --r) { + for (int f = 0; f < 8; ++f) { + putchar(grid[r * 8 + f]); + if (f < 7) putchar(' '); + } + putchar('\n'); + } } \ No newline at end of file diff --git a/engine/src/fen.c b/engine/src/fen.c index bef7b6a..2485ca9 100644 --- a/engine/src/fen.c +++ b/engine/src/fen.c @@ -21,6 +21,10 @@ int char_to_piece_index(char c) { } int load_fen(struct Board *board, const char *fen) { + // Reset the board first. Weird behavior when not zeroed. + memset(board, 0, sizeof *board); + board->ep_square = -1; + int rank = 7, file = 0; const char *ptr = fen; diff --git a/engine/src/test.c b/engine/src/test.c new file mode 100644 index 0000000..47e5514 --- /dev/null +++ b/engine/src/test.c @@ -0,0 +1,33 @@ +/** + A quick test script to debug and visually inspect functions + and operations. +*/ + + +#include +#include +#include "bitboard.h" +#include "fen.h" + + +static int popcount64(uint64_t x){ int c=0; while(x){ x&=x-1; ++c; } return c; } +static const char NAME[12] = {'P','N','B','R','Q','K','p','n','b','r','q','k'}; + + +int main(void) { + const char *fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + + struct Board b; + + int rc = load_fen(&b, fen); + + for (int i=0;i<12;i++) printf("%c: %d\n", NAME[i], popcount64(b.pieces[i])); + + if (rc != 0) { + fprintf(stderr, "load_fen failed (rc=%d) for FEN:\n%s\n", rc, fen); + return 1; + } + + print_board(&b); + return 0; +} \ No newline at end of file diff --git a/makefile b/makefile index b8f210a..6169d94 100644 --- a/makefile +++ b/makefile @@ -7,11 +7,17 @@ INCDIR := engine/include BUILDDIR := build LIBNAME := libchess -SRC := $(wildcard $(SRCDIR)/*.c) +# Exclude test.c from the shared lib build +SRC := $(filter-out $(SRCDIR)/test.c,$(wildcard $(SRCDIR)/*.c)) OBJ := $(patsubst $(SRCDIR)/%.c,$(BUILDDIR)/%.o,$(SRC)) LIB := $(BUILDDIR)/$(LIBNAME).so -.PHONY: all clean test +# ---- test executable (engine/src/test.c) ---- +TESTSRC := $(SRCDIR)/test.c +TESTOBJ := $(BUILDDIR)/test.o +TESTBIN := $(BUILDDIR)/print_board + +.PHONY: all clean test test-exe run all: $(LIB) @@ -24,6 +30,17 @@ $(BUILDDIR)/%.o: $(SRCDIR)/%.c | $(BUILDDIR) $(LIB): $(OBJ) $(CC) $(LDFLAGS) -o $@ $^ +c-test-exe: $(TESTBIN) + +$(TESTOBJ): $(TESTSRC) | $(BUILDDIR) + $(CC) -std=c11 -Wall -Wextra -O2 -I$(INCDIR) -c $< -o $@ + +$(TESTBIN): $(TESTOBJ) $(LIB) + $(CC) -O2 -o $@ $(TESTOBJ) -L$(BUILDDIR) -lchess -Wl,-rpath,'$$ORIGIN' + +run-c-test: test-exe + LD_LIBRARY_PATH=$(BUILDDIR) $(TESTBIN) $(FEN) + clean: @echo "Cleaning build and Python caches..." @rm -rf $(BUILDDIR) @@ -31,7 +48,6 @@ clean: @find . -type d -name ".pytest_cache" -exec rm -rf {} + @find . -type f \( -name "*.pyc" -o -name "*.pyo" \) -delete -# Always rebuild, and always clean afterward (even if tests fail) test: @$(MAKE) clean @$(MAKE) all