style: format tic-tac-toe sample and boop tests
This commit is contained in:
parent
0c94e6923a
commit
b83ff28f60
|
|
@ -1,132 +1,189 @@
|
||||||
import {Part} from "boardgame-core";
|
import { Part } from "@/core/part";
|
||||||
import {createRegion} from "boardgame-core";
|
import { createRegion } from "@/core/region";
|
||||||
import {createGameCommandRegistry, createPromptDef, IGameContext} from "boardgame-core";
|
import {
|
||||||
|
createGameCommandRegistry,
|
||||||
|
createPromptDef,
|
||||||
|
IGameContext,
|
||||||
|
} from "@/core/game";
|
||||||
|
|
||||||
const BOARD_SIZE = 3;
|
const BOARD_SIZE = 3;
|
||||||
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
|
const MAX_TURNS = BOARD_SIZE * BOARD_SIZE;
|
||||||
const WINNING_LINES: number[][][] = [
|
const WINNING_LINES: number[][][] = [
|
||||||
[[0, 0], [0, 1], [0, 2]],
|
[
|
||||||
[[1, 0], [1, 1], [1, 2]],
|
[0, 0],
|
||||||
[[2, 0], [2, 1], [2, 2]],
|
[0, 1],
|
||||||
[[0, 0], [1, 0], [2, 0]],
|
[0, 2],
|
||||||
[[0, 1], [1, 1], [2, 1]],
|
],
|
||||||
[[0, 2], [1, 2], [2, 2]],
|
[
|
||||||
[[0, 0], [1, 1], [2, 2]],
|
[1, 0],
|
||||||
[[0, 2], [1, 1], [2, 0]],
|
[1, 1],
|
||||||
|
[1, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[2, 0],
|
||||||
|
[2, 1],
|
||||||
|
[2, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[0, 0],
|
||||||
|
[1, 0],
|
||||||
|
[2, 0],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[0, 1],
|
||||||
|
[1, 1],
|
||||||
|
[2, 1],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[0, 2],
|
||||||
|
[1, 2],
|
||||||
|
[2, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[0, 0],
|
||||||
|
[1, 1],
|
||||||
|
[2, 2],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
[0, 2],
|
||||||
|
[1, 1],
|
||||||
|
[2, 0],
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
export type PlayerType = 'X' | 'O';
|
export type PlayerType = "X" | "O";
|
||||||
export type WinnerType = PlayerType | 'draw' | null;
|
export type WinnerType = PlayerType | "draw" | null;
|
||||||
export type TicTacToePart = Part<{ player: PlayerType }>;
|
export type TicTacToePart = Part<{ player: PlayerType }>;
|
||||||
|
|
||||||
export function createInitialState() {
|
export function createInitialState() {
|
||||||
return {
|
return {
|
||||||
board: createRegion('board', [
|
board: createRegion("board", [
|
||||||
{ name: 'x', min: 0, max: BOARD_SIZE - 1 },
|
{ name: "x", min: 0, max: BOARD_SIZE - 1 },
|
||||||
{ name: 'y', min: 0, max: BOARD_SIZE - 1 },
|
{ name: "y", min: 0, max: BOARD_SIZE - 1 },
|
||||||
]),
|
]),
|
||||||
parts: {} as Record<string, TicTacToePart>,
|
parts: {} as Record<string, TicTacToePart>,
|
||||||
currentPlayer: 'X' as PlayerType,
|
currentPlayer: "X" as PlayerType,
|
||||||
winner: null as WinnerType,
|
winner: null as WinnerType,
|
||||||
turn: 0,
|
turn: 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
export type TicTacToeState = ReturnType<typeof createInitialState>;
|
export type TicTacToeState = ReturnType<typeof createInitialState>;
|
||||||
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
export type TicTacToeGame = IGameContext<TicTacToeState>;
|
||||||
export const registry = createGameCommandRegistry<TicTacToeState>();
|
export const registry = createGameCommandRegistry<TicTacToeState>();
|
||||||
export const prompts = {
|
export const prompts = {
|
||||||
play: createPromptDef<[PlayerType, number, number]>(
|
play: createPromptDef<[PlayerType, number, number]>(
|
||||||
'play <player> <row:number> <col:number>')
|
"play <player> <row:number> <col:number>",
|
||||||
}
|
),
|
||||||
|
};
|
||||||
|
|
||||||
export async function start(game: TicTacToeGame) {
|
export async function start(game: TicTacToeGame) {
|
||||||
while (true) {
|
while (true) {
|
||||||
const currentPlayer = game.value.currentPlayer;
|
const currentPlayer = game.value.currentPlayer;
|
||||||
const turnNumber = game.value.turn + 1;
|
const turnNumber = game.value.turn + 1;
|
||||||
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
const turnOutput = await turn(game, currentPlayer, turnNumber);
|
||||||
|
|
||||||
game.produce(state => {
|
game.produce((state) => {
|
||||||
state.winner = turnOutput.winner;
|
state.winner = turnOutput.winner;
|
||||||
if (!state.winner) {
|
if (!state.winner) {
|
||||||
state.currentPlayer = state.currentPlayer === 'X' ? 'O' : 'X';
|
state.currentPlayer = state.currentPlayer === "X" ? "O" : "X";
|
||||||
state.turn = turnNumber;
|
state.turn = turnNumber;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if (game.value.winner) break;
|
if (game.value.winner) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return game.value;
|
return game.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const turn = registry.register({
|
const turn = registry.register({
|
||||||
schema: 'turn <player> <turnNumber:number>',
|
schema: "turn <player> <turnNumber:number>",
|
||||||
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
async run(game: TicTacToeGame, turnPlayer: PlayerType, turnNumber: number) {
|
||||||
const {player, row, col} = await game.prompt(
|
const { player, row, col } = await game.prompt(
|
||||||
prompts.play,
|
prompts.play,
|
||||||
(player, row, col) => {
|
(player, row, col) => {
|
||||||
if (player !== turnPlayer) {
|
if (player !== turnPlayer) {
|
||||||
throw `Invalid player: ${player}. Expected ${turnPlayer}.`;
|
throw `Invalid player: ${player}. Expected ${turnPlayer}.`;
|
||||||
} else if (!isValidMove(row, col)) {
|
} else if (!isValidMove(row, col)) {
|
||||||
throw `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
throw `Invalid position: (${row}, ${col}). Must be between 0 and ${BOARD_SIZE - 1}.`;
|
||||||
} else if (isCellOccupied(game, row, col)) {
|
} else if (isCellOccupied(game, row, col)) {
|
||||||
throw `Cell (${row}, ${col}) is already occupied.`;
|
throw `Cell (${row}, ${col}) is already occupied.`;
|
||||||
} else {
|
} else {
|
||||||
return { player, row, col };
|
return { player, row, col };
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
game.value.currentPlayer
|
game.value.currentPlayer,
|
||||||
);
|
);
|
||||||
|
|
||||||
placePiece(game, row, col, turnPlayer);
|
placePiece(game, row, col, turnPlayer);
|
||||||
|
|
||||||
const winner = checkWinner(game);
|
const winner = checkWinner(game);
|
||||||
if (winner) return { winner };
|
if (winner) return { winner };
|
||||||
if (turnNumber >= MAX_TURNS) return { winner: 'draw' as WinnerType };
|
if (turnNumber >= MAX_TURNS) return { winner: "draw" as WinnerType };
|
||||||
|
|
||||||
return { winner: null };
|
return { winner: null };
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
function isValidMove(row: number, col: number): boolean {
|
function isValidMove(row: number, col: number): boolean {
|
||||||
return !isNaN(row) && !isNaN(col) && row >= 0 && row < BOARD_SIZE && col >= 0 && col < BOARD_SIZE;
|
return (
|
||||||
|
!isNaN(row) &&
|
||||||
|
!isNaN(col) &&
|
||||||
|
row >= 0 &&
|
||||||
|
row < BOARD_SIZE &&
|
||||||
|
col >= 0 &&
|
||||||
|
col < BOARD_SIZE
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isCellOccupied(host: TicTacToeGame, row: number, col: number): boolean {
|
export function isCellOccupied(
|
||||||
return !!host.value.board.partMap[`${row},${col}`];
|
host: TicTacToeGame,
|
||||||
|
row: number,
|
||||||
|
col: number,
|
||||||
|
): boolean {
|
||||||
|
return !!host.value.board.partMap[`${row},${col}`];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function hasWinningLine(positions: number[][]): boolean {
|
export function hasWinningLine(positions: number[][]): boolean {
|
||||||
return WINNING_LINES.some(line =>
|
return WINNING_LINES.some((line) =>
|
||||||
line.every(([r, c]) =>
|
line.every(([r, c]) => positions.some(([pr, pc]) => pr === r && pc === c)),
|
||||||
positions.some(([pr, pc]) => pr === r && pc === c)
|
);
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkWinner(host: TicTacToeGame): WinnerType {
|
export function checkWinner(host: TicTacToeGame): WinnerType {
|
||||||
const parts = host.value.parts;
|
const parts = host.value.parts;
|
||||||
const partsArray = Object.values(parts);
|
const partsArray = Object.values(parts);
|
||||||
|
|
||||||
const xPositions = partsArray.filter((p: TicTacToePart) => p.player === 'X').map((p: TicTacToePart) => p.position);
|
const xPositions = partsArray
|
||||||
const oPositions = partsArray.filter((p: TicTacToePart) => p.player === 'O').map((p: TicTacToePart) => p.position);
|
.filter((p: TicTacToePart) => p.player === "X")
|
||||||
|
.map((p: TicTacToePart) => p.position);
|
||||||
|
const oPositions = partsArray
|
||||||
|
.filter((p: TicTacToePart) => p.player === "O")
|
||||||
|
.map((p: TicTacToePart) => p.position);
|
||||||
|
|
||||||
if (hasWinningLine(xPositions)) return 'X';
|
if (hasWinningLine(xPositions)) return "X";
|
||||||
if (hasWinningLine(oPositions)) return 'O';
|
if (hasWinningLine(oPositions)) return "O";
|
||||||
if (partsArray.length >= MAX_TURNS) return 'draw';
|
if (partsArray.length >= MAX_TURNS) return "draw";
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function placePiece(host: TicTacToeGame, row: number, col: number, player: PlayerType) {
|
export function placePiece(
|
||||||
const board = host.value.board;
|
host: TicTacToeGame,
|
||||||
const moveNumber = Object.keys(host.value.parts).length + 1;
|
row: number,
|
||||||
const piece: TicTacToePart = {
|
col: number,
|
||||||
regionId: 'board', position: [row, col], player,
|
player: PlayerType,
|
||||||
id: `piece-${player}-${moveNumber}`
|
) {
|
||||||
}
|
const board = host.value.board;
|
||||||
host.produce(state => {
|
const moveNumber = Object.keys(host.value.parts).length + 1;
|
||||||
state.parts[piece.id] = piece;
|
const piece: TicTacToePart = {
|
||||||
board.childIds.push(piece.id);
|
regionId: "board",
|
||||||
board.partMap[`${row},${col}`] = piece.id;
|
position: [row, col],
|
||||||
});
|
player,
|
||||||
|
id: `piece-${player}-${moveNumber}`,
|
||||||
|
};
|
||||||
|
host.produce((state) => {
|
||||||
|
state.parts[piece.id] = piece;
|
||||||
|
board.childIds.push(piece.id);
|
||||||
|
board.partMap[`${row},${col}`] = piece.id;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue