Changes
This commit is contained in:
parent
bbdbdbda06
commit
8aad6ed2b2
1 changed files with 49 additions and 7 deletions
|
|
@ -146,11 +146,52 @@ const BagchalGame: React.FC<BagchalGameProps> = ({ 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<BagchalGameProps> = ({ 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<BagchalGameProps> = ({ 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' };
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue