add-fen-loader (#5)
All checks were successful
Python tests (make) / test (push) Successful in 10s
All checks were successful
Python tests (make) / test (push) Successful in 10s
Reviewed-on: #5 Co-authored-by: Josh <josh@joshuaschuett.com> Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
@@ -20,8 +20,16 @@
|
||||
#define RANK_7 0x00FF000000000000ULL
|
||||
#define RANK_8 0xFF00000000000000ULL
|
||||
|
||||
enum Color { WHITE = 0, BLACK = 1 };
|
||||
enum Piece { PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING, PIECE_N = 6 };
|
||||
#define SET_BIT(bb, sq) ((bb) |= (1ULL << (sq)))
|
||||
#define CLEAR_BIT(bb, sq) ((bb) &= ~(1ULL << (sq)))
|
||||
#define GET_BIT(bb, sq) ((bb) & (1ULL << (sq)))
|
||||
#define TOGGLE_BIT(bb, sq) ((bb) ^= (1ULL << (sq)))
|
||||
|
||||
enum Color { WHITE = 0, BLACK = 1, BOTH = 2 };
|
||||
enum Piece {
|
||||
P, N, B, R, Q, K, // 0..5 white
|
||||
p, n, b, r, q, k // 6..11 black
|
||||
};
|
||||
|
||||
enum Castling {
|
||||
CASTLE_WK = 1 << 0,
|
||||
@@ -31,20 +39,20 @@ enum Castling {
|
||||
};
|
||||
|
||||
struct Board {
|
||||
uint64_t bb[2][PIECE_N]; // Each set of pieces get a bitboard for each player.
|
||||
uint64_t occ[2]; // Color occupancy bitboards.
|
||||
uint64_t occ_both; // occ[WHITE] | occ[BLACK]
|
||||
uint64_t pieces[12]; // Each set of pieces get a bitboard for each player.
|
||||
uint64_t occ[3]; // Color occupancy bitboards.
|
||||
|
||||
uint64_t king_square[2];
|
||||
|
||||
uint8_t castling;
|
||||
uint64_t ep_square;
|
||||
uint8_t castling_rights;
|
||||
int ep_square;
|
||||
enum Color side_to_move;
|
||||
|
||||
uint16_t halfmove_clock;
|
||||
uint16_t fullmove_number;
|
||||
int halfmove_clock;
|
||||
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');
|
||||
}
|
||||
}
|
||||
120
engine/src/fen.c
Normal file
120
engine/src/fen.c
Normal file
@@ -0,0 +1,120 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "bitboard.h"
|
||||
|
||||
|
||||
int square_index(int file, int rank) {
|
||||
return rank*8 + file;
|
||||
}
|
||||
|
||||
int char_to_piece_index(char c) {
|
||||
switch (c) {
|
||||
case 'P': return P; case 'N': return N; case 'B': return B;
|
||||
case 'R': return R; case 'Q': return Q; case 'K': return K;
|
||||
case 'p': return p; case 'n': return n; case 'b': return b;
|
||||
case 'r': return r; case 'q': return q; case 'k': return k;
|
||||
default: return -1;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
// Place pieces into the board.
|
||||
while (*ptr && *ptr != ' ') {
|
||||
if (isdigit(*ptr)) {
|
||||
file += *ptr - '0';
|
||||
ptr++;
|
||||
} else if (isalpha(*ptr)) {
|
||||
if (file >= 8) return -1;
|
||||
|
||||
int piece = char_to_piece_index(*ptr);
|
||||
|
||||
if (piece == -1) return -1;
|
||||
|
||||
SET_BIT(board->pieces[piece], rank * 8 + file);
|
||||
file++;
|
||||
ptr++;
|
||||
} else if (*ptr == '/') {
|
||||
// Each rank must have exactly 8 files
|
||||
if (file != 8) return -1;
|
||||
rank--;
|
||||
file = 0;
|
||||
ptr++;
|
||||
} else {
|
||||
// Unexpected character
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (rank != 0 || file != 8) return -1;
|
||||
ptr++;
|
||||
|
||||
// Side to move.
|
||||
if (*ptr != 'w' && *ptr != 'b') return -1;
|
||||
|
||||
board->side_to_move = (*ptr == 'w') ? WHITE : BLACK;
|
||||
ptr += 2;
|
||||
|
||||
// Castling rights.
|
||||
board->castling_rights = 0;
|
||||
while (*ptr && *ptr != ' ') {
|
||||
switch (*ptr) {
|
||||
case 'K': board->castling_rights |= CASTLE_WK; break;
|
||||
case 'Q': board->castling_rights |= CASTLE_WQ; break;
|
||||
case 'k': board->castling_rights |= CASTLE_BK; break;
|
||||
case 'q': board->castling_rights |= CASTLE_BQ; break;
|
||||
case '-': break;
|
||||
default: return -1;
|
||||
}
|
||||
ptr++;
|
||||
}
|
||||
|
||||
if (*ptr != ' ') return -1;
|
||||
ptr++;
|
||||
|
||||
// En Passant square.
|
||||
if (*ptr == '-') {
|
||||
board->ep_square = -1;
|
||||
ptr += 2;
|
||||
} else {
|
||||
if (ptr[0] < 'a' || ptr[0] > 'h' || ptr[1] < '1' || ptr[1] > '8') return -1;
|
||||
|
||||
// Strict FEN: Rank must be 3 or 6.
|
||||
char rank_char = ptr[1];
|
||||
if (rank_char != '3' && rank_char != '6') return -1;
|
||||
|
||||
int file = ptr[0] - 'a';
|
||||
int rank = ptr[1] - '1';
|
||||
|
||||
|
||||
|
||||
board->ep_square = square_index(file, rank);
|
||||
ptr += 3;
|
||||
}
|
||||
|
||||
// Parse halfmove clock for the game.
|
||||
board->halfmove_clock = atoi(ptr);
|
||||
while (*ptr && *ptr != ' ') ptr++;
|
||||
if (*ptr == ' ') ptr++;
|
||||
|
||||
// Parse the fullmove number.
|
||||
board->fullmove_number = atoi(ptr);
|
||||
|
||||
int num_pieces = 6;
|
||||
for (int i = 0; i < num_pieces; i++) {
|
||||
board->occ[WHITE] |= board->pieces[i]; // white
|
||||
board->occ[BLACK] |= board->pieces[i + 6]; // black
|
||||
}
|
||||
board->occ[BOTH] = board->occ[WHITE] | board->occ[BLACK];
|
||||
|
||||
return 0;
|
||||
}
|
||||
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