/
expertdaniil
/
Chess
Обзор
Документация
Войти
/
expertdaniil
/
Chess
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Chess_Search.cpp
87 строк
2 KB
expertdaniil
upload files
22 янв 2026, 15:44
Верифицирован
22 янв 2026, 15:44
a081d0c
Код
Авторство
О чём код?
#include "Chess_Search.h" #include "Chess_MoveGen.h" #include "Chess_Apply.h" #include "Chess_Eval.h" #include "Chess_Attack.h" #include <algorithm> static const int MATE_SCORE = 100000; static int movePriority(const Move& m) { int p = 0; if (m.isPromotion) p += 1000; if (m.isCapture) p += 100; if (m.isCastle) p += 10; return p; } static int negamax(ChessState& st, const PieceDefRegistry& reg, const RulesConfig& rules, int depth, int alpha, int beta, Side rootSide) { auto legal = ChessMoveGen::GenerateLegalMoves(st, reg, rules); if (legal.empty()) { if (IsKingInCheck(st, reg, st.sideToMove())) { return (st.sideToMove() == rootSide) ? -MATE_SCORE : +MATE_SCORE; } return 0; } if (depth == 0) { return EvaluateMaterial(st, reg, rootSide); } std::sort(legal.begin(), legal.end(), [](const Move& a, const Move& b){ return movePriority(a) > movePriority(b); }); int best = -1000000000; for (const auto& mv : legal) { ChessUndo u = ChessApplier::ApplyMove(st, mv, rules); int score = -negamax(st, reg, rules, depth-1, -beta, -alpha, rootSide); ChessApplier::UndoMove(st, u); if (score > best) best = score; if (score > alpha) alpha = score; if (alpha >= beta) break; } return best; } Move ChessSearch::FindBestMove(ChessState& st, const PieceDefRegistry& reg, const RulesConfig& rules, int depth) { Move bestMove; int bestScore = -1000000000; Side root = st.sideToMove(); auto legal = ChessMoveGen::GenerateLegalMoves(st, reg, rules); if (legal.empty()) return bestMove; std::sort(legal.begin(), legal.end(), [](const Move& a, const Move& b){ return movePriority(a) > movePriority(b); }); for (auto mv : legal) { if (mv.isPromotion && mv.promoKind == PieceKind::None) mv.promoKind = PieceKind::Queen; ChessUndo u = ChessApplier::ApplyMove(st, mv, rules); int score = -negamax(st, reg, rules, depth-1, -1000000000, +1000000000, root); ChessApplier::UndoMove(st, u); if (score > bestScore) { bestScore = score; bestMove = mv; } } return bestMove; }