Spaces:
Runtime error
Runtime error
| import type { Cell } from './types.ts' | |
| import { BitBoard, bitBoardWinResult } from './bot/BitBoard.ts' | |
| export function checkWin( | |
| board: Map<string, Cell> | BitBoard, | |
| x: number, | |
| y: number, | |
| playerId: string, | |
| noOverlines?: boolean, | |
| ): { won: boolean; cells: { x: number; y: number }[] } { | |
| if (board instanceof BitBoard) { | |
| const player = Number(playerId) | |
| if (player !== 0 && player !== 1) return { won: false, cells: [] } | |
| return bitBoardWinResult(board, x, y, player, noOverlines) | |
| } | |
| const directions = [[1, 0], [0, 1], [1, 1], [1, -1]] | |
| for (const [dx, dy] of directions) { | |
| const cells: { x: number; y: number }[] = [{ x, y }] | |
| let count = 1 | |
| let cx = x + dx | |
| let cy = y + dy | |
| while (board.get(`${cx}:${cy}`)?.playerId === playerId) { | |
| cells.push({ x: cx, y: cy }) | |
| count++ | |
| cx += dx | |
| cy += dy | |
| } | |
| const posEnd = board.get(`${cx}:${cy}`) | |
| let nx = x - dx | |
| let ny = y - dy | |
| while (board.get(`${nx}:${ny}`)?.playerId === playerId) { | |
| cells.push({ x: nx, y: ny }) | |
| count++ | |
| nx -= dx | |
| ny -= dy | |
| } | |
| const negEnd = board.get(`${nx}:${ny}`) | |
| if (count >= 5) { | |
| if (noOverlines && count > 5) continue | |
| const bothBlocked = posEnd !== undefined && negEnd !== undefined | |
| if (!bothBlocked) { | |
| return { won: true, cells } | |
| } | |
| } | |
| } | |
| return { won: false, cells: [] } | |
| } | |