feat: add tic-tac-toe with rule invoking rule

This commit is contained in:
2026-04-01 23:58:07 +08:00
parent dbd2a25185
commit b33c901c11
5 changed files with 464 additions and 32 deletions
+28 -13
View File
@@ -1,6 +1,11 @@
import { describe, it, expect } from 'vitest';
import { createRule, type RuleContext } from '../../src/core/rule';
import { createRule, type RuleContext, type GameContextLike } from '../../src/core/rule';
import { createGameContext } from '../../src/core/context';
import type { Command } from '../../src/utils/command';
function isCommand(value: Command | RuleContext<unknown>): value is Command {
return 'name' in value;
}
describe('Rule System', () => {
function createTestGame() {
@@ -24,11 +29,12 @@ describe('Rule System', () => {
});
it('should create a generator when called', () => {
const game = createTestGame();
const rule = createRule('<target>', function*(cmd) {
return cmd.params[0];
});
const gen = rule.create({ name: 'test', params: ['card1'], flags: {}, options: {} });
const gen = rule.create.call(game as unknown as GameContextLike, { name: 'test', params: ['card1'], flags: {}, options: {} });
const result = gen.next();
expect(result.done).toBe(true);
expect(result.value).toBe('card1');
@@ -57,7 +63,8 @@ describe('Rule System', () => {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const confirm = yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0], confirmed: confirm.name === 'confirm' };
const confirmCmd = isCommand(confirm) ? confirm : undefined;
return { moved: cmd.params[0], confirmed: confirmCmd?.name === 'confirm' };
}));
game.dispatchCommand('move card1 hand');
@@ -132,7 +139,8 @@ describe('Rule System', () => {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const response = yield { name: '', params: [], options: [], flags: [] };
return { moved: cmd.params[0], response: response.name };
const rcmd = isCommand(response) ? response : undefined;
return { moved: cmd.params[0], response: rcmd?.name };
}));
game.dispatchCommand('move card1 hand');
@@ -147,7 +155,8 @@ describe('Rule System', () => {
game.registerRule('move', createRule('<from> <to>', function*(cmd) {
const response = yield '<item>';
return { response: response.params[0] };
const rcmd = isCommand(response) ? response : undefined;
return { response: rcmd?.params[0] };
}));
game.dispatchCommand('move card1 hand');
@@ -162,7 +171,8 @@ describe('Rule System', () => {
game.registerRule('trade', createRule('<from> <to>', function*(cmd) {
const response = yield '<item> [amount: number]';
return { traded: response.params[0] };
const rcmd = isCommand(response) ? response : undefined;
return { traded: rcmd?.params[0] };
}));
game.dispatchCommand('trade player1 player2');
@@ -333,7 +343,8 @@ describe('Rule System', () => {
game.registerRule('test', createRule('<arg>', function*() {
const cmd = yield customSchema;
return { received: cmd.params[0] };
const rcmd = isCommand(cmd) ? cmd : undefined;
return { received: rcmd?.params[0] };
}));
game.dispatchCommand('test val1');
@@ -349,7 +360,9 @@ describe('Rule System', () => {
game.registerRule('multi', createRule('<start>', function*() {
const a = yield '<value>';
const b = yield '<value>';
return { a: a.params[0], b: b.params[0] };
const acmd = isCommand(a) ? a : undefined;
const bcmd = isCommand(b) ? b : undefined;
return { a: acmd?.params[0], b: bcmd?.params[0] };
}));
game.dispatchCommand('multi init');
@@ -369,13 +382,15 @@ describe('Rule System', () => {
const player = cmd.params[0];
const action = yield { name: '', params: [], options: [], flags: [] };
if (action.name === 'move') {
yield '<target>';
} else if (action.name === 'attack') {
yield '<target> [--power: number]';
if (isCommand(action)) {
if (action.name === 'move') {
yield '<target>';
} else if (action.name === 'attack') {
yield '<target> [--power: number]';
}
}
return { player, action: action.name };
return { player, action: isCommand(action) ? action.name : '' };
}));
const ctx1 = game.dispatchCommand('start alice');
+127
View File
@@ -0,0 +1,127 @@
import { describe, it, expect } from 'vitest';
import { createGameContext } from '../../src/core/context';
import { registerTicTacToeRules, startTicTacToe, type TicTacToeState } from '../../src/samples/tic-tac-toe';
describe('Tic-Tac-Toe', () => {
function createGame() {
const game = createGameContext();
registerTicTacToeRules(game);
return game;
}
function getBoardState(game: ReturnType<typeof createGame>) {
return game.latestContext<TicTacToeState>('tic-tac-toe')!.value;
}
it('should initialize the board and start the game', () => {
const game = createGame();
startTicTacToe(game);
const state = getBoardState(game);
expect(state.currentPlayer).toBe('X');
expect(state.winner).toBeNull();
expect(state.moveCount).toBe(0);
const board = game.regions.get('board');
expect(board.value.axes).toHaveLength(2);
expect(board.value.axes[0].name).toBe('x');
expect(board.value.axes[1].name).toBe('y');
});
it('should play moves and determine a winner', () => {
const game = createGame();
startTicTacToe(game);
// X wins with column 0
game.dispatchCommand('play X 0 0');
game.dispatchCommand('play O 0 1');
game.dispatchCommand('play X 1 0');
game.dispatchCommand('play O 1 1');
game.dispatchCommand('play X 2 0');
const state = getBoardState(game);
expect(state.winner).toBe('X');
expect(state.moveCount).toBe(5);
});
it('should reject out-of-bounds moves', () => {
const game = createGame();
startTicTacToe(game);
const beforeCount = getBoardState(game).moveCount;
game.dispatchCommand('play X 5 5');
game.dispatchCommand('play X -1 0');
game.dispatchCommand('play X 3 3');
expect(getBoardState(game).moveCount).toBe(beforeCount);
});
it('should reject moves on occupied cells', () => {
const game = createGame();
startTicTacToe(game);
game.dispatchCommand('play X 1 1');
expect(getBoardState(game).moveCount).toBe(1);
// Try to play on the same cell
game.dispatchCommand('play O 1 1');
expect(getBoardState(game).moveCount).toBe(1);
});
it('should ignore moves after game is over', () => {
const game = createGame();
startTicTacToe(game);
// X wins
game.dispatchCommand('play X 0 0');
game.dispatchCommand('play O 0 1');
game.dispatchCommand('play X 1 0');
game.dispatchCommand('play O 1 1');
game.dispatchCommand('play X 2 0');
expect(getBoardState(game).winner).toBe('X');
const moveCountAfterWin = getBoardState(game).moveCount;
// Try to play more
game.dispatchCommand('play X 2 1');
game.dispatchCommand('play O 2 2');
expect(getBoardState(game).moveCount).toBe(moveCountAfterWin);
});
it('should detect a draw', () => {
const game = createGame();
startTicTacToe(game);
// Fill board with no winner (cat's game)
// X: (0,0), (0,2), (1,1), (2,0)
// O: (0,1), (1,0), (1,2), (2,1), (2,2)
game.dispatchCommand('play X 0 0'); // X
game.dispatchCommand('play O 0 1'); // O
game.dispatchCommand('play X 0 2'); // X
game.dispatchCommand('play O 1 0'); // O
game.dispatchCommand('play X 1 1'); // X (center)
game.dispatchCommand('play O 1 2'); // O
game.dispatchCommand('play X 2 0'); // X
game.dispatchCommand('play O 2 1'); // O
game.dispatchCommand('play X 2 2'); // X (last move, draw)
const state = getBoardState(game);
expect(state.winner).toBe('draw');
expect(state.moveCount).toBe(9);
});
it('should place parts on the board region at correct positions', () => {
const game = createGame();
startTicTacToe(game);
game.dispatchCommand('play X 1 2');
const board = game.regions.get('board');
expect(board.value.children).toHaveLength(1);
const piece = board.value.children[0].value;
expect(piece.position).toEqual([1, 2]);
});
});