refactor: improve tic tac toe with region entity

This commit is contained in:
2026-04-02 16:52:58 +08:00
parent b1a6619ae3
commit 06c801e6ae
3 changed files with 13 additions and 15 deletions
+5 -11
View File
@@ -1,4 +1,4 @@
import {createGameCommandRegistry, Part, Entity, entity, Region} from '@/index';
import {createGameCommandRegistry, Part, Entity, entity, RegionEntity} from '@/index';
const BOARD_SIZE = 3;
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
@@ -20,7 +20,7 @@ type TicTacToePart = Part & { player: PlayerType };
export function createInitialState() {
return {
board: entity<Region>('board', {
board: new RegionEntity('board', {
id: 'board',
axes: [
{ name: 'x', min: 0, max: BOARD_SIZE - 1 },
@@ -89,15 +89,9 @@ 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<TicTacToeState>) {
return host.value.board;
}
export function isCellOccupied(host: Entity<TicTacToeState>, row: number, col: number): boolean {
const board = getBoardRegion(host);
return board.value.children.some(
part => part.value.position[0] === row && part.value.position[1] === col
);
const board = host.value.board;
return board.partsMap.value[`${row},${col}`] !== undefined;
}
export function hasWinningLine(positions: number[][]): boolean {
@@ -122,7 +116,7 @@ export function checkWinner(host: Entity<TicTacToeState>): WinnerType {
}
export function placePiece(host: Entity<TicTacToeState>, row: number, col: number, player: PlayerType) {
const board = getBoardRegion(host);
const board = host.value.board;
const moveNumber = host.value.parts.length + 1;
const piece: TicTacToePart = {
id: `piece-${player}-${moveNumber}`,