unsigned long long calculate_zobrist(struct Board * board) { unsigned long long zobristHash = 0; int i; for(i = 0; i < 12; ++i) { bitboard piece = board->pieces[i]; while (piece) { int index = bit_scan_forward(piece); zobristHash ^= pieceKeys[i][index]; piece = clear_lsb(piece); } } if (board->sideToMove == BLACK) { zobristHash ^= blackToMoveKey; } assert(board->castling < 16); zobristHash ^= castlingKeys[board->castling]; if (board->enPassant) { int index = bit_scan_forward(board->enPassant); zobristHash ^= enPassantKeys[index]; } return zobristHash; }
force_inline static move *generate_knight_moves(uint64_t knights, const uint64_t targets, move *moves) { int from; int to; uint64_t posmoves; while (knights) { from = lsb(knights); posmoves = knight_attacks(from) & targets; while (posmoves) { to = lsb(posmoves); *moves++ = MOVE(from, to); clear_lsb(posmoves); } clear_lsb(knights); } return moves; }
force_inline static move *generate_rook_moves(uint64_t rooks, const uint64_t occupied, const uint64_t targets, move *moves) { int from; int to; uint64_t posmoves; while (rooks) { from = lsb(rooks); posmoves = rook_attacks(from, occupied) & targets; while (posmoves) { to = lsb(posmoves); *moves++ = MOVE(from, to); clear_lsb(posmoves); } clear_lsb(rooks); } return moves; }
force_inline static move *generate_king_moves(const int ksq, const uint64_t targets, move *moves) { int to; uint64_t posmoves = king_attacks(ksq) & targets; while (posmoves) { to = lsb(posmoves); *moves++ = MOVE(ksq, to); clear_lsb(posmoves); } return moves; }
uint64_t reverse_board50(uint64_t image) { int bit; uint64_t rev; rev = 0; while (image) { bit = LSB64(image); image = clear_lsb(image); rev |= ((uint64_t)1 << mirrored(bit)); } return(rev); }
int evaluate_piece(bitboard pieces, int* squareValues, int pieceValue) { int score = 0; while(pieces) { int pieceSquare = bit_scan_forward(pieces); score += squareValues[pieceSquare] + pieceValue; pieces = clear_lsb(pieces); } return score; }
bool Board::isBishopAttack(const Move & move) const { const Field & fto = getField(move.to_); if ( fto.type() != Figure::TypeBishop ) return false; Figure::Color color = color_; Figure::Color ocolor = Figure::otherColor(color); const BitMask & bi_caps = g_movesTable->caps(Figure::TypeBishop, move.to_); BitMask op_mask = fmgr().rook_mask(color) | fmgr().queen_mask(color); if ( !(op_mask & bi_caps) ) return false; BitMask all_mask = fmgr().mask(Figure::ColorWhite) | fmgr().mask(Figure::ColorBlack); all_mask ^= op_mask; BitMask inv_mask_all = ~all_mask; op_mask &= bi_caps; const BitMask & oki_mask = fmgr().king_mask(color_); int oki_pos = _lsb64(oki_mask); int attackedN = 0; for ( ; op_mask;) { int p = clear_lsb(op_mask); if ( is_something_between(move.to_, p, inv_mask_all) ) continue; // 2 attacked figures found if ( ++attackedN > 1 ) return true; // may be it's pinned BitMask mask_from = g_betweenMasks->from(move.to_, p); mask_from &= oki_mask; if ( !mask_from ) continue; if ( is_nothing_between(move.to_, oki_pos, inv_mask_all) ) return true; } return false; }