add-fen-loader #5
@@ -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);
|
||||
void create_knight_attack_cache();
|
||||
void create_pawn_attack_cache();
|
||||
void create_king_attack_cache();
|
||||
void print_board();
|
||||
1
engine/include/fen.h
Normal file
1
engine/include/fen.h
Normal file
@@ -0,0 +1 @@
|
||||
int load_fen();
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "bitboard.h"
|
||||
#include <stdio.h>
|
||||
|
||||
uint64_t PAWN_ATTACKS[2][64];
|
||||
uint64_t KNIGHT_ATTACKS[64];
|
||||
@@ -59,3 +60,44 @@ 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');
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
33
engine/src/test.c
Normal file
33
engine/src/test.c
Normal file
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
A quick test script to debug and visually inspect functions
|
||||
and operations.
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#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;
|
||||
}
|
||||
22
makefile
22
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
|
||||
|
||||
Reference in New Issue
Block a user