Add king move gen and a test.c file for quick debug
All checks were successful
Python tests (make) / test (push) Successful in 10s
Python tests (make) / test (pull_request) Successful in 10s

This commit is contained in:
2025-08-17 20:27:03 -04:00
parent fd9af926e0
commit b811e33b81
3 changed files with 207 additions and 26 deletions

View File

@@ -1,33 +1,116 @@
/**
A quick test script to debug and visually inspect functions
and operations.
*/
// engine/src/test_pawns.c
#include <stdio.h>
#include <string.h>
#include "bitboard.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; }
static const char NAME[12] = {'P','N','B','R','Q','K','p','n','b','r','q','k'};
void sq_to_coord(int sq, char out[3]) {
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) {
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;
memset(&b, 0, sizeof(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);
if (load_fen(&b, fen) != 0) {
fprintf(stderr, "load_fen failed\n");
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;
}
}