From 244a8bb35a0bf9436fd24a83e9d9e6d928b8d4fd Mon Sep 17 00:00:00 2001 From: hypercross Date: Wed, 8 Apr 2026 12:25:50 +0800 Subject: [PATCH] fix: onitama win con --- src/samples/onitama/commands.ts | 16 +++++++------ tests/samples/onitama.test.ts | 40 +++++++++++++++++++++++++++++---- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/samples/onitama/commands.ts b/src/samples/onitama/commands.ts index 3f442e7..917fb44 100644 --- a/src/samples/onitama/commands.ts +++ b/src/samples/onitama/commands.ts @@ -176,22 +176,24 @@ const swapCard = registry.register({ /** * 检查占领胜利条件:玩家的师父棋子到达对手的初始位置 + * 红色师父需要到达 (2, 4) - 黑色师父的初始位置 + * 黑色师父需要到达 (2, 0) - 红色师父的初始位置 */ async function handleCheckConquestWin(game: OnitamaGame): Promise { const state = game.value; - - // 红色师父到达 y=4(黑色初始位置) + + // 红色师父到达 (2, 4)(黑色师父的初始位置) const redMaster = state.pawns['red-master']; - if (redMaster && redMaster.regionId === 'board' && redMaster.position[1] === 4) { + if (redMaster && redMaster.regionId === 'board' && redMaster.position[0] === 2 && redMaster.position[1] === 4) { return 'red'; } - - // 黑色师父到达 y=0(红色初始位置) + + // 黑色师父到达 (2, 0)(红色师父的初始位置) const blackMaster = state.pawns['black-master']; - if (blackMaster && blackMaster.regionId === 'board' && blackMaster.position[1] === 0) { + if (blackMaster && blackMaster.regionId === 'board' && blackMaster.position[0] === 2 && blackMaster.position[1] === 0) { return 'black'; } - + return null; } diff --git a/tests/samples/onitama.test.ts b/tests/samples/onitama.test.ts index a66f4d5..c7a1d64 100644 --- a/tests/samples/onitama.test.ts +++ b/tests/samples/onitama.test.ts @@ -266,10 +266,10 @@ describe('Onitama Game', () => { }); describe('Win Conditions', () => { - it('should detect conquest win for red (master reaches y=4)', async () => { + it('should detect conquest win for red (master reaches 2,4)', async () => { const { ctx } = createDeterministicContext(); - // Move red master to y=4 + // Move red master to (2, 4) - black master's initial position ctx.produce(state => { const redMaster = state.pawns['red-master']; redMaster.position = [2, 4]; @@ -282,10 +282,10 @@ describe('Onitama Game', () => { } }); - it('should detect conquest win for black (master reaches y=0)', async () => { + it('should detect conquest win for black (master reaches 2,0)', async () => { const { ctx } = createDeterministicContext(); - // Move black master to y=0 + // Move black master to (2, 0) - red master's initial position ctx.produce(state => { const blackMaster = state.pawns['black-master']; blackMaster.position = [2, 0]; @@ -298,6 +298,38 @@ describe('Onitama Game', () => { } }); + it('should NOT detect conquest win for red if x is wrong', async () => { + const { ctx } = createDeterministicContext(); + + // Move red master to (1, 4) - wrong x position + ctx.produce(state => { + const redMaster = state.pawns['red-master']; + redMaster.position = [1, 4]; + }); + + const result = await ctx.run('check-win'); + expect(result.success).toBe(true); + if (result.success) { + expect(result.result).toBeNull(); + } + }); + + it('should NOT detect conquest win for black if x is wrong', async () => { + const { ctx } = createDeterministicContext(); + + // Move black master to (3, 0) - wrong x position + ctx.produce(state => { + const blackMaster = state.pawns['black-master']; + blackMaster.position = [3, 0]; + }); + + const result = await ctx.run('check-win'); + expect(result.success).toBe(true); + if (result.success) { + expect(result.result).toBeNull(); + } + }); + it('should detect capture win when red master is captured', async () => { const { ctx } = createDeterministicContext();