#include #include #include "board.h" 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 = BIT << 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; } 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 = BIT << 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"); } void precompute_knight_attack_table(Bitboard attacks[64]) { for (int index = 0; index < 64; index++) { U64 position = BIT << 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); } } void precompute_king_attack_table(Bitboard attacks[64]) { for (int index = 0; index < 64; index++) { U64 position = BIT << index; attacks[index] = (position & notAFile) << 7 | (position << 8) | (position & notHFile) << 9 | (position & notAFile) >> 1 | (position & notHFile) << 1 | (position & notAFile) >> 9 | (position >> 8) | (position & notHFile) >> 7; } } void precompute_pawn_attack_table(Bitboard attacks[64], BYTE color) { for (int index = 0; index < 64; index++) { U64 position = BIT << index; if (color == WHITE) attacks[index] = (position & notAFile) << 7 | (position & notHFile) << 9; else attacks[index] = (position & notAFile) >> 9 | (position & notHFile) >> 7; } }