#include "bitboard.h" #include #include position_t position_starting() { return (position_t) { .bitboards = { 16ULL, 8ULL, 129ULL, 36ULL, 66ULL, 65280ULL, 1152921504606846976ULL, 576460752303423488ULL, 9295429630892703744ULL, 2594073385365405696ULL, 4755801206503243776ULL, 71776119061217280ULL, }, .castling = WHITE_SHORT_CASTLE | WHITE_LONG_CASTLE | BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE, .turn = WHITE_TURN, .passantable_file = 0, .halfmove_clock = 0, .fullmove_clock = 1 }; } void assert_valid_position(position_t position) { assert(check_valid_position(position) == true); } bool check_valid_position(position_t position) { if (position.castling > ( WHITE_SHORT_CASTLE | WHITE_LONG_CASTLE | BLACK_SHORT_CASTLE | BLACK_LONG_CASTLE ) ) return false; if (position.turn != WHITE_TURN && position.turn != BLACK_TURN) return false; if (position.passantable_file > 8) return false; // 0 means no passant if (position.bitboards[WHITE_KING] == 0) return false; if (position.bitboards[BLACK_KING] == 0) return false; return true; } char get_piece_char(size_t piece_type) { assert(piece_type < PIECE_TYPE_COUNT); if (WHITE_KING == piece_type) return 'K'; if (WHITE_QUEEN == piece_type) return 'Q'; if (WHITE_ROOK == piece_type) return 'R'; if (WHITE_BISHOP == piece_type) return 'B'; if (WHITE_KNIGHT == piece_type) return 'N'; if (WHITE_PAWN == piece_type) return 'P'; if (BLACK_KING == piece_type) return 'k'; if (BLACK_QUEEN == piece_type) return 'q'; if (BLACK_ROOK == piece_type) return 'r'; if (BLACK_BISHOP == piece_type) return 'b'; if (BLACK_KNIGHT == piece_type) return 'n'; if (BLACK_PAWN == piece_type) return 'p'; } char get_piece_char_from_sqr(position_t position, size_t square) { assert(square < 64); for (int i = 0; i < PIECE_TYPE_COUNT; i++) { if ((position.bitboards[i] >> square) & (bitboard_t)1) { return get_piece_char(i); } } return ' '; } int find_piece_on_square(position_t* p, int square) { if ((p->bitboards[WHITE_KING] >> square) & 1) return WHITE_KING; if ((p->bitboards[WHITE_QUEEN] >> square) & 1) return WHITE_QUEEN; if ((p->bitboards[WHITE_ROOK] >> square) & 1) return WHITE_ROOK; if ((p->bitboards[WHITE_BISHOP] >> square) & 1) return WHITE_BISHOP; if ((p->bitboards[WHITE_KNIGHT] >> square) & 1) return WHITE_KNIGHT; if ((p->bitboards[WHITE_PAWN] >> square) & 1) return WHITE_PAWN; if ((p->bitboards[BLACK_KING] >> square) & 1) return BLACK_KING; if ((p->bitboards[BLACK_QUEEN] >> square) & 1) return BLACK_QUEEN; if ((p->bitboards[BLACK_ROOK] >> square) & 1) return BLACK_ROOK; if ((p->bitboards[BLACK_BISHOP] >> square) & 1) return BLACK_BISHOP; if ((p->bitboards[BLACK_KNIGHT] >> square) & 1) return BLACK_KNIGHT; if ((p->bitboards[BLACK_PAWN] >> square) & 1) return BLACK_PAWN; assert(0 && "Failed to find the piece on the square"); } void print_position(position_t position) { for (int i = 8; i > 0; i--) { printf("+---+---+---+---+---+---+---+---+\n"); printf( "| %c | %c | %c | %c | %c | %c | %c | %c | %d\n", get_piece_char_from_sqr(position, 8 * i - 8), get_piece_char_from_sqr(position, 8 * i - 7), get_piece_char_from_sqr(position, 8 * i - 6), get_piece_char_from_sqr(position, 8 * i - 5), get_piece_char_from_sqr(position, 8 * i - 4), get_piece_char_from_sqr(position, 8 * i - 3), get_piece_char_from_sqr(position, 8 * i - 2), get_piece_char_from_sqr(position, 8 * i - 1), i ); } printf("+---+---+---+---+---+---+---+---+\n"); printf(" a b c d e f g h\n"); } void print_bitboard(bitboard_t bitboard) { printf("Bitboard(%" PRIu64 ")\n", bitboard); for (int i = 7; i >= 0; i--) { for (int j = 0; j < 8; j++) { printf("%" PRIu64 "", (bitboard >> (8 * i + j)) & 1); } printf("\n"); } }