/
expertdaniil
/
Chess
Обзор
Документация
Войти
/
expertdaniil
/
Chess
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Chess_Attack.cpp
86 строк
3 KB
expertdaniil
upload files
22 янв 2026, 15:44
Верифицирован
22 янв 2026, 15:44
a081d0c
Код
Авторство
О чём код?
#include "Chess_Attack.h" int FindKingSquare(const ChessState& st, Side s) { for (int sq = 0; sq < 64; ++sq) { const Piece& p = st.pieceAt(sq); if (!p.isEmpty() && p.side == s && p.kind == PieceKind::King) return sq; } return -1; } static bool attacksByPawn(const ChessState& st, int sq, Side bySide) { int r = ChessState::row(sq); int c = ChessState::col(sq); int dir = (bySide == Side::White) ? +1 : -1; int pr = r - dir; for (int dc : {-1, +1}) { int pc = c + dc; if (!ChessState::inBounds(pr, pc)) continue; int from = ChessState::pack(pr, pc); const Piece& p = st.pieceAt(from); if (!p.isEmpty() && p.side == bySide && p.kind == PieceKind::Pawn) return true; } return false; } static bool attacksBySteps(const ChessState& st, const PieceDef& def, int sq, Side bySide) { int r = ChessState::row(sq); int c = ChessState::col(sq); for (const auto& d : def.steps) { int rr = r - d.dr; int cc = c - d.dc; if (!ChessState::inBounds(rr, cc)) continue; const Piece& p = st.pieceAt(ChessState::pack(rr, cc)); if (!p.isEmpty() && p.side == bySide && p.kind == def.kind) return true; } return false; } static bool attacksByRays(const ChessState& st, const PieceDef& def, int sq, Side bySide) { int r = ChessState::row(sq); int c = ChessState::col(sq); for (const auto& dir : def.rays) { int rr = r; int cc = c; for (int step = 1; step <= def.maxRaySteps; ++step) { rr -= dir.dr; cc -= dir.dc; if (!ChessState::inBounds(rr, cc)) break; int from = ChessState::pack(rr, cc); const Piece& p = st.pieceAt(from); if (p.isEmpty()) continue; if (p.side == bySide && p.kind == def.kind) return true; break; } } return false; } bool IsSquareAttacked(const ChessState& st, const PieceDefRegistry& reg, int sq, Side bySide) { if (!ChessState::inBoundsSq(sq)) return false; if (attacksByPawn(st, sq, bySide)) return true; for (const auto& def : reg.defs) { if (def.kind == PieceKind::None || def.kind == PieceKind::Pawn) continue; if (!def.steps.empty()) { if (attacksBySteps(st, def, sq, bySide)) return true; } if (!def.rays.empty()) { if (attacksByRays(st, def, sq, bySide)) return true; } } return false; } bool IsKingInCheck(const ChessState& st, const PieceDefRegistry& reg, Side s) { int ksq = FindKingSquare(st, s); if (ksq < 0) return false; return IsSquareAttacked(st, reg, ksq, Opposite(s)); }