import {createGameCommandRegistry, Part, Entity, entity, RegionEntity} from '@/index'; const BOARD_SIZE = 6; const MAX_PIECES_PER_PLAYER = 8; const WIN_LENGTH = 3; export type PlayerType = 'white' | 'black'; export type PieceType = 'kitten' | 'cat'; export type WinnerType = PlayerType | 'draw' | null; type BoopPart = Part & { player: PlayerType; pieceType: PieceType }; type PieceSupply = { supply: number; placed: number }; type Player = { id: PlayerType; kitten: PieceSupply; cat: PieceSupply; }; type PlayerEntity = Entity; function createPlayer(id: PlayerType): PlayerEntity { return entity(id, { id, kitten: { supply: MAX_PIECES_PER_PLAYER, placed: 0 }, cat: { supply: 0, placed: 0 }, }); } export function createInitialState() { return { board: new RegionEntity('board', { id: 'board', axes: [ { name: 'x', min: 0, max: BOARD_SIZE - 1 }, { name: 'y', min: 0, max: BOARD_SIZE - 1 }, ], children: [], }), currentPlayer: 'white' as PlayerType, winner: null as WinnerType, players: { white: createPlayer('white'), black: createPlayer('black'), }, }; } export type BoopState = ReturnType; const registration = createGameCommandRegistry(); export const registry = registration.registry; // Player Entity helper functions export function getPlayer(host: Entity, player: PlayerType): PlayerEntity { return host.value.players[player]; } export function decrementSupply(player: PlayerEntity, pieceType: PieceType) { player.produce(p => { p[pieceType].supply--; p[pieceType].placed++; }); } export function incrementSupply(player: PlayerEntity, pieceType: PieceType, count?: number) { player.produce(p => { p[pieceType].supply += count ?? 1; }); } registration.add('setup', async function() { const {context} = this; while (true) { const currentPlayer = context.value.currentPlayer; const turnOutput = await this.run<{winner: WinnerType}>(`turn ${currentPlayer}`); if (!turnOutput.success) throw new Error(turnOutput.error); context.produce(state => { state.winner = turnOutput.result.winner; if (!state.winner) { state.currentPlayer = state.currentPlayer === 'white' ? 'black' : 'white'; } }); if (context.value.winner) break; } return context.value; }); registration.add('turn ', async function(cmd) { const [turnPlayer] = cmd.params as [PlayerType]; const playCmd = await this.prompt( 'play [type:string]', (command) => { const [player, row, col, type] = command.params as [PlayerType, number, number, PieceType?]; const pieceType = type === 'cat' ? 'cat' : 'kitten'; if (player !== turnPlayer) { return `Invalid player: ${player}. Expected ${turnPlayer}.`; } if (!isValidMove(row, col)) { return `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`; } if (isCellOccupied(this.context, row, col)) { return `Cell (${row}, ${col}) is already occupied.`; } const playerEntity = getPlayer(this.context, player); const supply = playerEntity.value[pieceType].supply; if (supply <= 0) { return `No ${pieceType}s left in ${player}'s supply.`; } return null; } ); const [player, row, col, type] = playCmd.params as [PlayerType, number, number, PieceType?]; const pieceType = type === 'cat' ? 'cat' : 'kitten'; placePiece(this.context, row, col, turnPlayer, pieceType); applyBoops(this.context, row, col, pieceType); const graduatedLines = checkGraduation(this.context, turnPlayer); if (graduatedLines.length > 0) { processGraduation(this.context, turnPlayer, graduatedLines); } const winner = checkWinner(this.context); if (winner) return { winner }; return { winner: null }; }); function isValidMove(row: number, col: number): boolean { return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE; } export function getBoardRegion(host: Entity) { return host.value.board; } export function isCellOccupied(host: Entity, row: number, col: number): boolean { const board = getBoardRegion(host); return board.partsMap.value[`${row},${col}`] !== undefined; } export function getPartAt(host: Entity, row: number, col: number): Entity | null { const board = getBoardRegion(host); return (board.partsMap.value[`${row},${col}`] as Entity | undefined) || null; } export function placePiece(host: Entity, row: number, col: number, player: PlayerType, pieceType: PieceType) { const board = getBoardRegion(host); const playerEntity = getPlayer(host, player); const count = playerEntity.value[pieceType].placed + 1; const piece: BoopPart = { id: `${player}-${pieceType}-${count}`, region: board, position: [row, col], player, pieceType, }; host.produce(s => { const e = entity(piece.id, piece); board.produce(draft => { draft.children.push(e); }); }); decrementSupply(playerEntity, pieceType); } export function applyBoops(host: Entity, placedRow: number, placedCol: number, placedType: PieceType) { const board = getBoardRegion(host); const partsMap = board.partsMap.value; const piecesToBoop: { part: Entity; dr: number; dc: number }[] = []; for (const key in partsMap) { const part = partsMap[key] as Entity; const [r, c] = part.value.position; if (r === placedRow && c === placedCol) continue; const dr = Math.sign(r - placedRow); const dc = Math.sign(c - placedCol); if (Math.abs(r - placedRow) <= 1 && Math.abs(c - placedCol) <= 1) { const booperIsKitten = placedType === 'kitten'; const targetIsCat = part.value.pieceType === 'cat'; if (booperIsKitten && targetIsCat) continue; piecesToBoop.push({ part, dr, dc }); } } for (const { part, dr, dc } of piecesToBoop) { const [r, c] = part.value.position; const newRow = r + dr; const newCol = c + dc; if (newRow < 0 || newRow >= BOARD_SIZE || newCol < 0 || newCol >= BOARD_SIZE) { const pt = part.value.pieceType; const pl = part.value.player; const playerEntity = getPlayer(host, pl); removePieceFromBoard(host, part); incrementSupply(playerEntity, pt); continue; } if (isCellOccupied(host, newRow, newCol)) continue; part.produce(p => { p.position = [newRow, newCol]; }); } } export function removePieceFromBoard(host: Entity, part: Entity) { const board = getBoardRegion(host); board.produce(draft => { draft.children = draft.children.filter(p => p.id !== part.id); }); } const DIRECTIONS: [number, number][] = [ [0, 1], [1, 0], [1, 1], [1, -1], ]; export function* linesThrough(r: number, c: number): Generator { for (const [dr, dc] of DIRECTIONS) { const minStart = -(WIN_LENGTH - 1); for (let offset = minStart; offset <= 0; offset++) { const startR = r + offset * dr; const startC = c + offset * dc; const endR = startR + (WIN_LENGTH - 1) * dr; const endC = startC + (WIN_LENGTH - 1) * dc; if (startR < 0 || startR >= BOARD_SIZE || startC < 0 || startC >= BOARD_SIZE) continue; if (endR < 0 || endR >= BOARD_SIZE || endC < 0 || endC >= BOARD_SIZE) continue; const line: number[][] = []; for (let i = 0; i < WIN_LENGTH; i++) { line.push([startR + i * dr, startC + i * dc]); } yield line; } } } export function* allLines(): Generator { const seen = new Set(); for (let r = 0; r < BOARD_SIZE; r++) { for (let c = 0; c < BOARD_SIZE; c++) { for (const line of linesThrough(r, c)) { const key = line.map(p => p.join(',')).join(';'); if (!seen.has(key)) { seen.add(key); yield line; } } } } } export function hasWinningLine(positions: number[][]): boolean { const posSet = new Set(positions.map(p => `${p[0]},${p[1]}`)); for (const line of allLines()) { if (line.every(([lr, lc]) => posSet.has(`${lr},${lc}`))) return true; } return false; } export function checkGraduation(host: Entity, player: PlayerType): number[][][] { const board = getBoardRegion(host); const partsMap = board.partsMap.value; const posSet = new Set(); for (const key in partsMap) { const part = partsMap[key] as Entity; if (part.value.player === player && part.value.pieceType === 'kitten') { posSet.add(`${part.value.position[0]},${part.value.position[1]}`); } } const winningLines: number[][][] = []; for (const line of allLines()) { if (line.every(([lr, lc]) => posSet.has(`${lr},${lc}`))) { winningLines.push(line); } } return winningLines; } export function processGraduation(host: Entity, player: PlayerType, lines: number[][][]) { const allPositions = new Set(); for (const line of lines) { for (const [r, c] of line) { allPositions.add(`${r},${c}`); } } const board = getBoardRegion(host); const partsMap = board.partsMap.value; const partsToRemove: Entity[] = []; for (const key in partsMap) { const part = partsMap[key] as Entity; if (part.value.player === player && part.value.pieceType === 'kitten' && allPositions.has(`${part.value.position[0]},${part.value.position[1]}`)) { partsToRemove.push(part); } } for (const part of partsToRemove) { removePieceFromBoard(host, part); } const count = partsToRemove.length; const playerEntity = getPlayer(host, player); incrementSupply(playerEntity, 'cat', count); } export function checkWinner(host: Entity): WinnerType { const board = getBoardRegion(host); const partsMap = board.partsMap.value; for (const player of ['white', 'black'] as PlayerType[]) { const positions: number[][] = []; for (const key in partsMap) { const part = partsMap[key] as Entity; if (part.value.player === player && part.value.pieceType === 'cat') { positions.push(part.value.position); } } if (hasWinningLine(positions)) return player; } const whitePlayer = getPlayer(host, 'white'); const blackPlayer = getPlayer(host, 'black'); const whiteTotal = MAX_PIECES_PER_PLAYER - whitePlayer.value.kitten.supply + whitePlayer.value.cat.supply; const blackTotal = MAX_PIECES_PER_PLAYER - blackPlayer.value.kitten.supply + blackPlayer.value.cat.supply; if (whiteTotal >= MAX_PIECES_PER_PLAYER && blackTotal >= MAX_PIECES_PER_PLAYER) { return 'draw'; } return null; }