Add fen loader and a test script for c
This commit is contained in:
@@ -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];
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user