Commit 31cf6666 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(ludo): prevent pawns on safe squares from being sent home

Two bugs caused pawns to teleport back to the yard without capture:

1. Triple-six penalty blindly sent the last-moved piece to pos=-1
   regardless of whether it was resting on a safe square. Now the
   penalty only applies if the piece is NOT on a safe square.

2. captureAt() checked if the DESTINATION was safe but didn't guard
   against the victim's own position being safe. Added a per-piece
   safety check so a pawn sitting on any safe square (star squares,
   colored start squares) can never be captured under any circumstance.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 6d1a8ac5
...@@ -219,8 +219,14 @@ export function applyMove(game, playerIdx, move) { ...@@ -219,8 +219,14 @@ export function applyMove(game, playerIdx, move) {
game.extraTurn = true; game.extraTurn = true;
player.consecutiveSixes = (player.consecutiveSixes || 0) + 1; player.consecutiveSixes = (player.consecutiveSixes || 0) + 1;
if (player.consecutiveSixes >= 3) { if (player.consecutiveSixes >= 3) {
piece.pos = -1; // Triple-six penalty: send piece home ONLY if it's NOT resting on a safe square
piece.finished = false; const penaltyGlobal = piece.pos >= 0 && piece.pos <= HOME_ENTRY_POS
? (piece.pos + START_POSITIONS[playerIdx]) % SHARED_PATH_LENGTH
: -1;
if (penaltyGlobal === -1 || !SAFE_SQUARES.includes(penaltyGlobal)) {
piece.pos = -1;
piece.finished = false;
}
player.consecutiveSixes = 0; player.consecutiveSixes = 0;
game.extraTurn = false; game.extraTurn = false;
} }
...@@ -266,6 +272,8 @@ function captureAt(game, playerIdx, globalPos) { ...@@ -266,6 +272,8 @@ function captureAt(game, playerIdx, globalPos) {
if (piece.pos === -1 || piece.finished) continue; if (piece.pos === -1 || piece.finished) continue;
if (piece.pos > HOME_ENTRY_POS) continue; if (piece.pos > HOME_ENTRY_POS) continue;
const theirGlobal = (piece.pos + START_POSITIONS[i]) % SHARED_PATH_LENGTH; const theirGlobal = (piece.pos + START_POSITIONS[i]) % SHARED_PATH_LENGTH;
// Double-check: never capture a piece sitting on ANY safe square
if (SAFE_SQUARES.includes(theirGlobal)) continue;
if (theirGlobal === globalPos) { if (theirGlobal === globalPos) {
piece.pos = -1; piece.pos = -1;
if (!captured.includes(i)) captured.push(i); if (!captured.includes(i)) captured.push(i);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment