/
docNemo
/
tactic-hack-core
Обзор
Документация
Войти
/
docNemo
/
tactic-hack-core
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
src/geometry.ts
53 строки
2 KB
neo
Implement rules engine: geometry, placement, attack reducer, replay, projection, deduction, protocol types
19 июл 2026, 00:03
19 июл 2026, 00:03
d1403fb
Код
Авторство
О чём код?
import { AXES, Axis, BOARD_SIZE, Cell, inBounds, sameCell, } from "./types.js"; const AXIS_STEPS: Record<Axis, { dr: number; dc: number }> = { horizontal: { dr: 0, dc: 1 }, vertical: { dr: 1, dc: 0 }, diagonalUp: { dr: -1, dc: 1 }, diagonalDown: { dr: 1, dc: 1 }, }; /** Клетки одной оси через `origin` до границ поля, не включая сам `origin`. */ export function axisCells(origin: Cell, axis: Axis): Cell[] { const { dr, dc } = AXIS_STEPS[axis]; const cells: Cell[] = []; for (const sign of [1, -1]) { let row = origin.row + dr * sign; let col = origin.col + dc * sign; while (inBounds({ row, col })) { cells.push({ row, col }); row += dr * sign; col += dc * sign; } } return cells; } /** Все клетки сплошных линий корабля в `origin` по 4 осям (без клетки корабля). */ export function shipLineCells(origin: Cell): Cell[] { return AXES.flatMap((axis) => axisCells(origin, axis)); } /** Ось, на которой лежат обе клетки, или null (для совпадающих клеток — null). */ export function sharedAxis(a: Cell, b: Cell): Axis | null { if (sameCell(a, b)) return null; if (a.row === b.row) return "horizontal"; if (a.col === b.col) return "vertical"; if (a.row + a.col === b.row + b.col) return "diagonalUp"; if (a.row - a.col === b.row - b.col) return "diagonalDown"; return null; } /** Число сплошных линий, пересекающих клетку (0–4 при валидной расстановке). */ export function countSolidLines(ships: readonly Cell[], cell: Cell): number { return ships.filter((ship) => sharedAxis(ship, cell) !== null).length; } export { BOARD_SIZE };