import { TokenFlow } from "../flow"; export class GridFlow extends TokenFlow { rows: number; columns: number; constructor(rows: number, columns: number) { super(); this.rows = rows; this.columns = columns; } get requiredLength(): number | null { return this.rows * this.columns; } isWithin( anchorIndex: number, headIndex: number, otherIndex: number ): boolean { const anchorRow = Math.floor(anchorIndex / this.columns); const anchorCol = anchorIndex % this.columns; const headRow = Math.floor(headIndex / this.columns); const headCol = headIndex % this.columns; const otherRow = Math.floor(otherIndex / this.columns); const otherCol = otherIndex % this.columns; const minRow = Math.min(anchorRow, headRow); const maxRow = Math.max(anchorRow, headRow); const minCol = Math.min(anchorCol, headCol); const maxCol = Math.max(anchorCol, headCol); return ( otherRow >= minRow && otherRow <= maxRow && otherCol >= minCol && otherCol <= maxCol ); } verticalCandidates( direction: "up" | "down", fromIndex: number, length: number ): number[] { const row = Math.floor(fromIndex / this.columns); const col = fromIndex % this.columns; const candidates: number[] = []; if (direction === "up" && row > 0) { candidates.push((row - 1) * this.columns + col); } else if (direction === "down" && row < this.rows - 1) { candidates.push((row + 1) * this.columns + col); } return candidates.filter((index) => index < length); } }