From 8aad6ed2b2dad8147920626ee1aea20cc9a12945 Mon Sep 17 00:00:00 2001 From: "gpt-engineer-app[bot]" <159125892+gpt-engineer-app[bot]@users.noreply.github.com> Date: Mon, 12 Jan 2026 09:47:46 +0000 Subject: [PATCH] Changes --- src/components/BagchalGame.tsx | 56 +++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/src/components/BagchalGame.tsx b/src/components/BagchalGame.tsx index 5ab3b93..ad3635e 100644 --- a/src/components/BagchalGame.tsx +++ b/src/components/BagchalGame.tsx @@ -146,11 +146,52 @@ const BagchalGame: React.FC = ({ showSocialShare = false }) => return captures; }; - // Check if tiger is trapped (no valid moves) + // Check if tiger is trapped (no valid moves) with given goat positions + const isTigerTrappedWithGoats = (tigerPos: Position, goats: Position[]): boolean => { + // Check for valid moves (adjacent empty positions) + const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]]; + + for (const [dr, dc] of directions) { + const newRow = tigerPos.row + dr; + const newCol = tigerPos.col + dc; + + if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) { + const isTiger = tigerPos.row === newRow && tigerPos.col === newCol; + const isGoat = goats.some(g => g.row === newRow && g.col === newCol); + + if (!isTiger && !isGoat) { + // There's an empty adjacent cell - tiger can move + return false; + } + } + } + + // Check for captures (jump over goat to empty space) + for (const [dr, dc] of directions) { + const goatRow = tigerPos.row + dr; + const goatCol = tigerPos.col + dc; + const landRow = tigerPos.row + 2 * dr; + const landCol = tigerPos.col + 2 * dc; + + if (goatRow >= 0 && goatRow < rows && goatCol >= 0 && goatCol < cols && + landRow >= 0 && landRow < rows && landCol >= 0 && landCol < cols) { + const hasGoat = goats.some(g => g.row === goatRow && g.col === goatCol); + const landingIsTiger = tigerPos.row === landRow && tigerPos.col === landCol; + const landingIsGoat = goats.some(g => g.row === landRow && g.col === landCol); + + if (hasGoat && !landingIsTiger && !landingIsGoat) { + // Tiger can capture - not trapped + return false; + } + } + } + + return true; + }; + + // Legacy function for backward compatibility const isTigerTrapped = (tigerPos: Position): boolean => { - const moves = getValidTigerMoves(tigerPos); - const captures = getTigerCaptures(tigerPos); - return moves.length === 0 && captures.length === 0; + return isTigerTrappedWithGoats(tigerPos, gameState.goatPositions); }; // Computer strategy for goats on 2xn board with 4 goats @@ -418,8 +459,8 @@ const BagchalGame: React.FC = ({ showSocialShare = false }) => turn: 'tiger' as Turn, }; - // Check if tiger is trapped - if (prev.tigerPosition && isTigerTrapped(prev.tigerPosition)) { + // Check if tiger is trapped with the NEW goat positions + if (prev.tigerPosition && isTigerTrappedWithGoats(prev.tigerPosition, newGoatPositions)) { return { ...newState, winner: 'goats' }; } @@ -491,7 +532,8 @@ const BagchalGame: React.FC = ({ showSocialShare = false }) => turn: 'tiger' as Turn, }; - if (prev.tigerPosition && isTigerTrapped(prev.tigerPosition)) { + // Check if tiger is trapped with the NEW goat positions + if (prev.tigerPosition && isTigerTrappedWithGoats(prev.tigerPosition, newGoatPositions)) { return { ...newState, winner: 'goats' }; }