Add Pseudo Move Generator #12
@@ -1,33 +1,116 @@
|
|||||||
/**
|
// engine/src/test_pawns.c
|
||||||
A quick test script to debug and visually inspect functions
|
|
||||||
and operations.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "bitboard.h"
|
#include "bitboard.h"
|
||||||
#include "fen.h"
|
#include "fen.h"
|
||||||
|
|
||||||
|
enum { MAX_MOVES = 256 };
|
||||||
|
|
||||||
static int popcount64(uint64_t x){ int c=0; while(x){ x&=x-1; ++c; } return c; }
|
void sq_to_coord(int sq, char out[3]) {
|
||||||
static const char NAME[12] = {'P','N','B','R','Q','K','p','n','b','r','q','k'};
|
out[0] = 'a' + (sq % 8);
|
||||||
|
out[1] = '1' + (sq / 8);
|
||||||
|
out[2] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_move(struct Move *m) {
|
||||||
|
char f[3], t[3];
|
||||||
|
sq_to_coord(m->from, f);
|
||||||
|
sq_to_coord(m->to, t);
|
||||||
|
|
||||||
|
printf("%s%s", f, t);
|
||||||
|
if (m->flags) {
|
||||||
|
printf(" [");
|
||||||
|
int first = 1;
|
||||||
|
if (m->flags & MF_CAPTURE) { printf("%sCAP", first?"":"|"); first = 0; }
|
||||||
|
if (m->flags & MF_DOUBLE_PUSH) { printf("%sDP", first?"":"|"); first = 0; }
|
||||||
|
if (m->flags & MF_ENPASSANT) { printf("%sEP", first?"":"|"); first = 0; }
|
||||||
|
if (m->flags & MF_PROMO) { printf("%sPROMO->%c", first?"":"|", (char)(m->promo ? "PNBRQKpnbrqk"[m->promo] : 'Q')); }
|
||||||
|
printf("]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_moves(const char *title, struct Move *moves, int count) {
|
||||||
|
printf("%s (%d):\n", title, count);
|
||||||
|
for (int i = 0; i < count; ++i) { print_move(&moves[i]); putchar('\n'); }
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const char *fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
// change this FEN to target specific cases you want to test
|
||||||
|
//const char *fen_startpos = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||||
|
const char *fen_startpos = "8/8/8/8/8/8/8/8 w KQkq - 0 1";
|
||||||
|
const char *fen = fen_startpos;
|
||||||
|
|
||||||
struct Board b;
|
struct Board b;
|
||||||
|
memset(&b, 0, sizeof(b));
|
||||||
|
|
||||||
int rc = load_fen(&b, fen);
|
if (load_fen(&b, fen) != 0) {
|
||||||
|
fprintf(stderr, "load_fen failed\n");
|
||||||
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;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
print_board(&b);
|
struct Move moves[MAX_MOVES];
|
||||||
|
int count;
|
||||||
|
|
||||||
|
/*
|
||||||
|
// --- WHITE pawns: quiet pushes (single + double, no promos) ---
|
||||||
|
count = 0;
|
||||||
|
gen_white_pawn_quiet_pushes(&b, moves, &count);
|
||||||
|
dump_moves("white quiet pushes", moves, count);
|
||||||
|
|
||||||
|
|
||||||
|
// --- BLACK pawns: quiet pushes ---
|
||||||
|
gen_black_pawn_quiet_pushes(&b, moves, &count);
|
||||||
|
dump_moves("black quiet pushes", moves, count);
|
||||||
|
|
||||||
|
printf("COUNT: %d", count);
|
||||||
|
*/
|
||||||
|
|
||||||
|
create_knight_attack_cache();
|
||||||
|
//create_king_attack_cache();
|
||||||
|
//create_pawn_attack_cache();
|
||||||
|
|
||||||
|
int n = gen_pseudo_moves(&b, moves, false);
|
||||||
|
printf("COUNT: %d", n);
|
||||||
|
|
||||||
|
/*
|
||||||
|
// --- WHITE pawns: quiet push promotions (queen-only) ---
|
||||||
|
count = 0;
|
||||||
|
gen_white_pawn_push_promotions_q(&b, moves, &count);
|
||||||
|
dump_moves("white push promotions (Q only)", moves, count);
|
||||||
|
|
||||||
|
// --- WHITE pawns: captures (non-promo) + en passant ---
|
||||||
|
count = 0;
|
||||||
|
gen_white_pawn_captures(&b, moves, &count);
|
||||||
|
dump_moves("white captures (non-promo + EP)", moves, count);
|
||||||
|
|
||||||
|
// --- WHITE pawns: capture promotions (queen-only) ---
|
||||||
|
count = 0;
|
||||||
|
gen_white_pawn_capture_promotions_q(&b, moves, &count);
|
||||||
|
dump_moves("white capture promotions (Q only)", moves, count);
|
||||||
|
|
||||||
|
// Flip side to move if you want to test black without changing FEN side
|
||||||
|
b.side_to_move = BLACK;
|
||||||
|
|
||||||
|
// --- BLACK pawns: quiet pushes ---
|
||||||
|
count = 0;
|
||||||
|
gen_black_pawn_quiet_pushes(&b, moves, &count);
|
||||||
|
dump_moves("black quiet pushes", moves, count);
|
||||||
|
|
||||||
|
// --- BLACK pawns: quiet push promotions (queen-only) ---
|
||||||
|
count = 0;
|
||||||
|
gen_black_pawn_push_promotions_q(&b, moves, &count);
|
||||||
|
dump_moves("black push promotions (q only)", moves, count);
|
||||||
|
|
||||||
|
// --- BLACK pawns: captures (non-promo) + en passant ---
|
||||||
|
count = 0;
|
||||||
|
gen_black_pawn_captures(&b, moves, &count);
|
||||||
|
dump_moves("black captures (non-promo + EP)", moves, count);
|
||||||
|
|
||||||
|
// --- BLACK pawns: capture promotions (queen-only) ---
|
||||||
|
count = 0;
|
||||||
|
gen_black_pawn_capture_promotions_q(&b, moves, &count);
|
||||||
|
dump_moves("black capture promotions (q only)", moves, count);
|
||||||
|
*/
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
13
makefile
13
makefile
@@ -17,7 +17,7 @@ TESTSRC := $(SRCDIR)/test.c
|
|||||||
TESTOBJ := $(BUILDDIR)/test.o
|
TESTOBJ := $(BUILDDIR)/test.o
|
||||||
TESTBIN := $(BUILDDIR)/print_board
|
TESTBIN := $(BUILDDIR)/print_board
|
||||||
|
|
||||||
.PHONY: all clean test test-exe run
|
.PHONY: all clean test test-exe run-c-test
|
||||||
|
|
||||||
all: $(LIB)
|
all: $(LIB)
|
||||||
|
|
||||||
@@ -27,18 +27,19 @@ $(BUILDDIR):
|
|||||||
$(BUILDDIR)/%.o: $(SRCDIR)/%.c | $(BUILDDIR)
|
$(BUILDDIR)/%.o: $(SRCDIR)/%.c | $(BUILDDIR)
|
||||||
$(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@
|
$(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@
|
||||||
|
|
||||||
$(LIB): $(OBJ)
|
$(LIB): $(OBJ) | $(BUILDDIR)
|
||||||
$(CC) $(LDFLAGS) -o $@ $^
|
$(CC) $(LDFLAGS) -o $@ $^
|
||||||
|
|
||||||
c-test-exe: $(TESTBIN)
|
# ---- test exe rules ----
|
||||||
|
test-exe: $(TESTBIN)
|
||||||
|
|
||||||
$(TESTOBJ): $(TESTSRC) | $(BUILDDIR)
|
$(TESTOBJ): $(TESTSRC) | $(BUILDDIR)
|
||||||
$(CC) -std=c11 -Wall -Wextra -O2 -I$(INCDIR) -c $< -o $@
|
$(CC) $(CFLAGS) -I$(INCDIR) -c $< -o $@
|
||||||
|
|
||||||
$(TESTBIN): $(TESTOBJ) $(LIB)
|
$(TESTBIN): $(TESTOBJ) $(LIB) | $(BUILDDIR)
|
||||||
$(CC) -O2 -o $@ $(TESTOBJ) -L$(BUILDDIR) -lchess -Wl,-rpath,'$$ORIGIN'
|
$(CC) -O2 -o $@ $(TESTOBJ) -L$(BUILDDIR) -lchess -Wl,-rpath,'$$ORIGIN'
|
||||||
|
|
||||||
run-c-test: test-exe
|
run-c-test: $(TESTBIN)
|
||||||
LD_LIBRARY_PATH=$(BUILDDIR) $(TESTBIN) $(FEN)
|
LD_LIBRARY_PATH=$(BUILDDIR) $(TESTBIN) $(FEN)
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
|
|||||||
@@ -7,8 +7,12 @@ from test.chess_ffi import gen_white_pawn_captures
|
|||||||
from test.chess_ffi import gen_knight_moves
|
from test.chess_ffi import gen_knight_moves
|
||||||
from test.chess_ffi import gen_bishop_moves
|
from test.chess_ffi import gen_bishop_moves
|
||||||
from test.chess_ffi import gen_rook_moves
|
from test.chess_ffi import gen_rook_moves
|
||||||
|
from test.chess_ffi import gen_queen_moves
|
||||||
|
from test.chess_ffi import gen_king_moves
|
||||||
from test.chess_ffi import Move
|
from test.chess_ffi import Move
|
||||||
from test.chess_ffi import Board
|
from test.chess_ffi import Board
|
||||||
|
from test.chess_ffi import BLACK
|
||||||
|
from test.chess_ffi import WHITE
|
||||||
|
|
||||||
|
|
||||||
MAX_MOVES = 256
|
MAX_MOVES = 256
|
||||||
@@ -61,7 +65,6 @@ class TestPseudoMoveGeneration(ChessLibTestBase):
|
|||||||
self.run_subtests(captures_only, gen_knight_moves, captures_only=True)
|
self.run_subtests(captures_only, gen_knight_moves, captures_only=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def test_bishop_move_gen(self):
|
def test_bishop_move_gen(self):
|
||||||
cases_bishop_all = [
|
cases_bishop_all = [
|
||||||
("8/8/8/8/3B4/8/8/8 w - - 0 1", 13, "center d4: 13 moves"),
|
("8/8/8/8/3B4/8/8/8 w - - 0 1", 13, "center d4: 13 moves"),
|
||||||
@@ -135,11 +138,105 @@ class TestPseudoMoveGeneration(ChessLibTestBase):
|
|||||||
|
|
||||||
|
|
||||||
def test_queen_move_gen(self):
|
def test_queen_move_gen(self):
|
||||||
pass
|
cases_queen_all = [
|
||||||
|
("8/8/8/8/3Q4/8/8/8 w - - 0 1", 27, "center d4: rook(14)+bishop(13)"),
|
||||||
|
|
||||||
|
("8/8/8/8/Q7/8/8/8 w - - 0 1", 21, "edge a4: 14+7"),
|
||||||
|
("8/8/8/8/8/8/8/Q7 w - - 0 1", 21, "corner a1: 14+7"),
|
||||||
|
("8/8/8/8/8/8/8/1Q6 w - - 0 1", 21, "b1: 14+7"),
|
||||||
|
|
||||||
|
# all 8 adjacent squares are friendly ⇒ blocked immediately on all rays
|
||||||
|
("8/8/8/2PPP3/2PQP3/2PPP3/8/8 w - - 0 1", 0, "d4 with white pawns on c3,d3,e3,c4,e4,c5,d5,e5"),
|
||||||
|
|
||||||
|
# all 8 adjacent squares are enemies ⇒ exactly 8 captures (one per ray), no further squares
|
||||||
|
("8/8/8/2p1p3/2pQp3/2p1p3/8/8 w - - 0 1", 13, "d4 with black pawns adjacent on all rays"),
|
||||||
|
|
||||||
|
# only horizontal blocked by own pieces at c4/e4; vertical+diagonals open
|
||||||
|
("8/8/8/8/2PQP3/8/8/8 w - - 0 1", 20, "d4, own at c4,e4: rook loses 7→7 (up+down only), bishop 13 ⇒ 20"),
|
||||||
|
|
||||||
|
# two queens on a1 & h1 block each other along rank 1
|
||||||
|
("8/8/8/8/8/8/8/Q6Q w - - 0 1", 40, "a1 & h1: each 21→20 due to mutual block ⇒ 40 total"),
|
||||||
|
]
|
||||||
|
|
||||||
|
cases_queen_caps = [
|
||||||
|
("8/8/8/8/3Q4/8/8/8 w - - 0 1", 0, "no enemies to capture"),
|
||||||
|
|
||||||
|
# adjacent enemies only → 6 captures
|
||||||
|
("8/8/8/2p1p3/2pQp3/2p1p3/8/8 w - - 0 1", 6, "adjacent caps on all rays"),
|
||||||
|
|
||||||
|
# adjacent enemies only → 8 captures
|
||||||
|
("8/8/8/2ppp3/2pQp3/2ppp3/8/8 w - - 0 1", 8, "adjacent caps on all rays"),
|
||||||
|
|
||||||
|
# far targets on two rook rays (d8 and h4) + friendly blocking b1
|
||||||
|
("3r4/8/8/8/1P1Q3r/8/8/8 w - - 0 1", 2, "captures on d8 and h4; b4 friend blocks left; no other enemies"),
|
||||||
|
|
||||||
|
# mixed: captures on two diagonals only
|
||||||
|
("7p/p7/8/8/3Q4/8/8/p5p1 w - - 0 1", 4, "diagonal targets a1,a7,g1,h8 (4 captures)"),
|
||||||
|
]
|
||||||
|
self.run_subtests(cases_queen_all, gen_queen_moves, captures_only=False)
|
||||||
|
self.run_subtests(cases_queen_caps, gen_queen_moves, captures_only=True)
|
||||||
|
|
||||||
|
|
||||||
def test_king_move_gen(self):
|
def test_king_move_gen(self):
|
||||||
pass
|
cases_king_all = [
|
||||||
|
("8/8/8/8/3K4/8/8/8 w - - 0 1", 8, "center d4: 8 moves"),
|
||||||
|
("8/8/8/8/K7/8/8/8 w - - 0 1", 5, "edge a4: 5 moves"),
|
||||||
|
("8/8/8/8/8/8/8/K7 w - - 0 1", 3, "corner a1: 3 moves"),
|
||||||
|
|
||||||
|
# all eight adjacent squares are WHITE pieces → 0
|
||||||
|
("8/8/8/2PPP3/2PKP3/2PPP3/8/8 w - - 0 1", 0, "surrounded by own"),
|
||||||
|
|
||||||
|
# all eight adjacent squares are BLACK pieces → 8 (all captures)
|
||||||
|
("8/8/8/2ppp3/2pKp3/2ppp3/8/8 w - - 0 1", 8, "surrounded by enemies"),
|
||||||
|
|
||||||
|
# mixed: own block east/west; captures NE & SE; rest quiet
|
||||||
|
("8/8/8/2p5/2PKP3/4p3/8/8 w - - 0 1", 6, "d4: caps c5,e3; quiet d5,d3,c3,e5"),
|
||||||
|
]
|
||||||
|
|
||||||
|
cases_king_caps = [
|
||||||
|
("8/8/8/8/3K4/8/8/8 w - - 0 1", 0, "no enemies"),
|
||||||
|
|
||||||
|
# eight adjacent enemies → 8 captures
|
||||||
|
("8/8/8/2ppp3/2pKp3/2ppp3/8/8 w - - 0 1", 8, "all adjacent caps"),
|
||||||
|
|
||||||
|
# edge a1 with only b2 enemy → 1 capture
|
||||||
|
("8/8/8/8/8/8/1p6/K7 w - - 0 1", 1, "a1: only Kxb2"),
|
||||||
|
|
||||||
|
# mixed: same as above mixed case → 2 captures
|
||||||
|
("8/8/8/2p5/2PKP3/4p3/8/8 w - - 0 1", 2, "captures c5 & e3"),
|
||||||
|
]
|
||||||
|
|
||||||
|
cases_king_castle_white = [
|
||||||
|
("8/8/8/8/8/8/8/R3K2R w KQ - 0 1", 7, "e1 with KQ rights, empty lanes: 5 king steps + O-O + O-O-O"),
|
||||||
|
("8/8/8/8/8/8/8/4K2R w K - 0 1", 6, "e1 with K only: 5 steps + O-O"),
|
||||||
|
("8/8/8/8/8/8/8/R3K3 w Q - 0 1", 6, "e1 with Q only: 5 steps + O-O-O"),
|
||||||
|
|
||||||
|
# blocked kingside (f1 occupied) → only O-O-O available
|
||||||
|
("8/8/8/8/8/8/8/R3KB1R w KQ - 0 1", 5, "f1 blocked by own piece; queen-side castle only"),
|
||||||
|
|
||||||
|
# blocked queenside (d1 occupied) → only O-O available
|
||||||
|
("8/8/8/8/8/8/8/R2BK2R w KQ - 0 1", 5, "d1 blocked by own piece; king-side castle only"),
|
||||||
|
|
||||||
|
# both sides blocked (d1 and f1 occupied) → no castles, just 5 normal moves
|
||||||
|
("8/8/8/8/8/8/8/R2BKB1R w KQ - 0 1", 3, "both lanes blocked; no castles"),
|
||||||
|
]
|
||||||
|
|
||||||
|
cases_king_castle_black = [
|
||||||
|
("r3k2r/8/8/8/8/8/8/8 b kq - 0 1", 7, "e8 with kq rights, empty lanes: 5 steps + O-O + O-O-O"),
|
||||||
|
("4k2r/8/8/8/8/8/8/8 b k - 0 1", 6, "e8 with k only: 5 steps + O-O"),
|
||||||
|
("r3k3/8/8/8/8/8/8/8 b q - 0 1", 6, "e8 with q only: 5 steps + O-O-O"),
|
||||||
|
|
||||||
|
# blocked kingside (f8 occupied) → only O-O-O available
|
||||||
|
("r3kb1r/8/8/8/8/8/8/8 b kq - 0 1", 5, "f8 blocked; only queen-side castle"),
|
||||||
|
|
||||||
|
# blocked queenside (d8 occupied) → only O-O available
|
||||||
|
("r2bk2r/8/8/8/8/8/8/8 b kq - 0 1", 5, "d8 blocked; only king-side castle"),
|
||||||
|
]
|
||||||
|
|
||||||
|
self.run_subtests(cases_king_all, gen_king_moves, captures_only=False)
|
||||||
|
self.run_subtests(cases_king_castle_white, gen_king_moves, captures_only=False)
|
||||||
|
self.run_subtests(cases_king_caps, gen_king_moves, captures_only=True)
|
||||||
|
self.run_subtests(cases_king_castle_black, gen_king_moves, captures_only=False)
|
||||||
|
|
||||||
|
|
||||||
def test_quiet_pawn_pushes_white(self):
|
def test_quiet_pawn_pushes_white(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user