#include #include #include "board.h" int pop_count(Bitboard bb){ if (bb == 0) return 0; return pop_count(bb >> 1) + (bb & 1); } void print_bitboard(Bitboard bb) { for (U64 index = 0; index < 64; index++) { printf("%lu", (bb >> index) & 1); if ((index + 1) % 8 == 0) printf("\n"); } printf("\n\n"); } void precompute_knight_attack_table(Bitboard attacks[64]) { for (int index = 0; index < 64; index++) { U64 position = (U64)1 << index; attacks[index] = ((position & notAFile & notBFile) << 6) | ((position & notGFile & notHFile) << 10) | ((position & notAFile) << 15) | ((position & notHFile) << 17) | ((position & notGFile & notHFile) >> 6) | ((position & notAFile & notBFile) >> 10) | ((position & notHFile) >> 15) | ((position & notAFile) >> 17); } } Board parse_FEN(char* FEN) { Board board; for (int i = 0; i < 12; i++) board.pieces[i] = 0; int rank = 7; int file = 0; for (int k = 0; k < strlen(FEN); k++) { int c = FEN[k]; int index = rank * 8 + file; Bitboard position = (Bitboard)1 << index; if (c > '0' && c <= '8') file += c - '0'; else { switch (c) { case 'R': board.pieces[ROOK] |= position; break; case 'N': board.pieces[KNIGHT] |= position; break; case 'B': board.pieces[BISHOP] |= position; break; case 'Q': board.pieces[QUEEN] |= position; break; case 'K': board.pieces[KING] |= position; break; case 'P': board.pieces[PAWN] |= position; break; case 'r': board.pieces[ROOK | BLACK] |= position; break; case 'n': board.pieces[KNIGHT | BLACK] |= position; break; case 'b': board.pieces[BISHOP | BLACK] |= position; break; case 'q': board.pieces[QUEEN | BLACK] |= position; break; case 'k': board.pieces[KING | BLACK] |= position; break; case 'p': board.pieces[PAWN | BLACK] |= position; break; case '/': rank--; file = -1; // So that it becomes 0 break; case ' ': // TODO: parse everything after return board; } file++; } } return board; } void print_board(Board board) { printf("\n"); for (int rank = 7; rank >= 0; rank--) { printf("%i|", rank + 1); for (int file = 0; file < 8; file++) { int index = rank * 8 + file; Bitboard position = (Bitboard)1 << index; int found = 0; for (int piece = 0; piece < 12; piece++) { if (board.pieces[piece] & position) { found = 1; printf("%s ", pieces[piece]); break; } } if (!found) printf(". "); } printf("\n"); } printf(" a b c d e f g h\n"); }