Fix tiger trap logic in Bagchal

Adjust bagchal game to correctly detect tiger traps using updated goat positions
- Reintroduced isTigerTrappedWithGoats to evaluate traps with current goat placements
- Replaced premature trap checks to use updated goat positions after moves
- Fixed three call sites to ensure trap evaluation uses latest state
- Preserved existing win conditions and goat-tiger interaction flows

X-Lovable-Edit-ID: edt-74e1bc3c-b0e9-49f9-83c7-efbe7bea49bc
This commit is contained in:
gpt-engineer-app[bot] 2026-01-12 09:47:46 +00:00
commit 45386ffd9b

View file

@ -146,11 +146,52 @@ const BagchalGame: React.FC<BagchalGameProps> = ({ showSocialShare = false }) =>
return captures; 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 isTigerTrapped = (tigerPos: Position): boolean => {
const moves = getValidTigerMoves(tigerPos); return isTigerTrappedWithGoats(tigerPos, gameState.goatPositions);
const captures = getTigerCaptures(tigerPos);
return moves.length === 0 && captures.length === 0;
}; };
// Computer strategy for goats on 2xn board with 4 goats // Computer strategy for goats on 2xn board with 4 goats
@ -418,8 +459,8 @@ const BagchalGame: React.FC<BagchalGameProps> = ({ showSocialShare = false }) =>
turn: 'tiger' as Turn, turn: 'tiger' as Turn,
}; };
// Check if tiger is trapped // Check if tiger is trapped with the NEW goat positions
if (prev.tigerPosition && isTigerTrapped(prev.tigerPosition)) { if (prev.tigerPosition && isTigerTrappedWithGoats(prev.tigerPosition, newGoatPositions)) {
return { ...newState, winner: 'goats' }; return { ...newState, winner: 'goats' };
} }
@ -491,7 +532,8 @@ const BagchalGame: React.FC<BagchalGameProps> = ({ showSocialShare = false }) =>
turn: 'tiger' as Turn, 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' }; return { ...newState, winner: 'goats' };
} }