Here is the modification to update the check and checkmate text appending logic.
Find the movePieceWithAnimation function. Locate the Checkmate / Stalemate Detection block we added previously inside the onComplete callback (around line 1250), and replace it entirely with this updated block:
// --- Added Logic: Checkmate / Stalemate / Check Detection ---
const nextTurnIsRed = (currentTurn === 'w');
const legalMoves = getAllLegalMoves(nextTurnIsRed, tileMap);
let isCheck = false;
let myKingPos = null;
// Find the King of the next player to determine if they are in check
for (const [pos, id] of tileMap.entries()) {
if ((nextTurnIsRed && id[0] === 'K') || (!nextTurnIsRed && id[0] === 'k')) {
myKingPos = pos.split(',').map(Number);
break;
}
}
// Determine if the King is currently under attack
if (myKingPos && isSquareAttacked(myKingPos[0], myKingPos[1], !nextTurnIsRed, tileMap)) {
isCheck = true;
}
// Get the output element to append the status to the existing text
const outputElement = document.querySelector('.ejceesoutput');
if (legalMoves.length === 0) {
if (isCheck) {
if (outputElement) outputElement.textContent += " checkmate";
setTimeout(() => alert("Game Over: Checkmate!"), 50);
} else {
if (outputElement) outputElement.textContent += " stalemate";
setTimeout(() => alert("Game Over: Stalemate!"), 50);
}
} else if (isCheck) {
// Append check status if the game is not over
if (outputElement) outputElement.textContent += " check!";
}
// ------------------------------------------------------------
Here is the modification to add the check, checkmate, and stalemate detection to the jumpToStep function.
Note: I have deliberately omitted the alert popups here, as it would be very annoying to get a popup every time you scroll through the game history and land on the final move. The text will simply update in the output bar.
Find the jumpToStep function (around line 935) and modify its inner if (stepData) block:
const totalDepth = getPathDepth(historyFEN, currentBranch);
if (index >= 0 && index < totalDepth) {
currentStepIndex = index;
stepSlider.value = index;
const stepData = getNodeAtStep(index);
if (stepData) {
loadFEN(stepData.fen);
applyLastMoveVisuals(stepData.lastMove);
highlightActiveStep(index);
restoreOutputText();
renderNoteUI();
// --- Added Logic: Checkmate / Stalemate / Check Detection on Jump ---
// We do not trigger alerts here to avoid spamming the user when navigating history
const nextTurnIsRed = (currentTurn === 'w');
const legalMoves = getAllLegalMoves(nextTurnIsRed, tileMap);
let isCheck = false;
let myKingPos = null;
// Find the King of the next player to determine if they are in check
for (const [pos, id] of tileMap.entries()) {
if ((nextTurnIsRed && id[0] === 'K') || (!nextTurnIsRed && id[0] === 'k')) {
myKingPos = pos.split(',').map(Number);
break;
}
}
// Determine if the King is currently under attack
if (myKingPos && isSquareAttacked(myKingPos[0], myKingPos[1], !nextTurnIsRed, tileMap)) {
isCheck = true;
}
// Get the output element to append the status to the existing text
const outputElement = document.querySelector('.ejceesoutput');
if (legalMoves.length === 0) {
if (isCheck) {
if (outputElement) outputElement.textContent += " checkmate";
} else {
if (outputElement) outputElement.textContent += " stalemate";
}
} else if (isCheck) {
if (outputElement) outputElement.textContent += " check!";
}
// --------------------------------------------------------------------
}
}
Here is the modification to add the check, checkmate, and stalemate detection to the auto-play functionality. It also ensures that the auto-play automatically stops if the game reaches a checkmate or stalemate state.
Find the playNextAnimated() function (around line 2580). Locate the movePieceWithAnimation callback inside it, and replace that block with the following:
// Trigger animation; callback runs only after the piece has finished moving
movePieceWithAnimation(pieceId, moveData.endX, moveData.endY, eatenId, () => {
// Animation complete: sync turn and visuals, advance step
currentTurn = currentTurn === 'w' ? 'b' : 'w';
applyLastMoveVisuals(moveData);
currentStepIndex = nextStepIndex;
stepSlider.value = currentStepIndex;
highlightActiveStep(currentStepIndex);
renderNoteUI();
restoreOutputText();
// --- Added Logic: Checkmate / Stalemate / Check Detection during Auto-Play ---
const nextTurnIsRed = (currentTurn === 'w');
const legalMoves = getAllLegalMoves(nextTurnIsRed, tileMap);
let isCheck = false;
let myKingPos = null;
// Find the King of the next player to determine if they are in check
for (const [pos, id] of tileMap.entries()) {
if ((nextTurnIsRed && id[0] === 'K') || (!nextTurnIsRed && id[0] === 'k')) {
myKingPos = pos.split(',').map(Number);
break;
}
}
// Determine if the King is currently under attack
if (myKingPos && isSquareAttacked(myKingPos[0], myKingPos[1], !nextTurnIsRed, tileMap)) {
isCheck = true;
}
const outputElement = document.querySelector('.ejceesoutput');
if (legalMoves.length === 0) {
if (isCheck) {
if (outputElement) outputElement.textContent += " checkmate";
} else {
if (outputElement) outputElement.textContent += " stalemate";
}
stopAutoPlay(); // Stop playing if the game is over
return; // Do not schedule the next move
} else if (isCheck) {
if (outputElement) outputElement.textContent += " check!";
}
// -----------------------------------------------------------------------------
// Chain the next animated move after the full moveInterval delay
setTimeout(playNextAnimated, moveInterval * 1000);
});
gemini pro:
Here is the modification to add support for parsing the ASCII art board format into a standard FEN string.
Find the importExportedText function (around line 2150). Insert the new ASCII detection and conversion logic right after the initial normalization steps, before it checks for URL formats.