Compare commits

...

2 Commits

Author SHA1 Message Date
Juhani Krekelä 5ef202d1b5 Allow piece to move onto its own square and capture itself 2021-01-10 19:19:29 +02:00
Juhani Krekelä 07421d4459 Change the format move history is stored in 2021-01-10 19:15:11 +02:00
1 changed files with 26 additions and 26 deletions

View File

@ -390,11 +390,6 @@
renderedPromotion = whiteMove ? whitePieces[promotion] : blackPieces[promotion];
}
// Are we moving the piece onto itself?
if (startLetter === endLetter && startNumber === endNumber) {
return {error: 'Start and end squares of the move cannot be identical'};
}
// Are we moving the piece from where it is located?
let startSquare = document.getElementById(startLetter + startNumber);
if (startSquare.childNodes.length === 0) {
@ -409,34 +404,38 @@
// Can the piece move to this location?
let endSquare = document.getElementById(endLetter + endNumber);
if (endSquare.childNodes.length !== 0 && !captures) {
if (endSquare.childNodes.length !== 0 && !captures && startSquare !== endSquare) {
return {error: 'Square ' + endLetter + endNumber + ' is occupied, did you mean to capture?'};
}
if (endSquare.childNodes.length === 0 && captures) {
return {error: 'Square ' + endLetter + endNumber + ' empty even though capture was specified'};
}
// Record our alterations to the table
let moveRecord = {
start: startLetter + startNumber,
end: endLetter + endNumber,
startPiece: renderedPiece,
endPiece: promotion ? renderedPromotion : renderedPiece,
capturedPiece: captures ? endSquare.childNodes[0].data : null,
moveText: move
};
moveHistory.push(moveRecord);
// Apply move
let removedPieces = {}
let placedPieces = {}
if (captures) {
removedPieces[endLetter + endNumber] = endSquare.childNodes[0].data;
endSquare.removeChild(endSquare.childNodes[0]);
}
startSquare.removeChild(startSquare.childNodes[0]);
endSquare.appendChild(document.createTextNode(promotion ? renderedPromotion : renderedPiece))
if (startSquare !== endSquare || !captures) {
removedPieces[startLetter + startNumber] = renderedPiece;
startSquare.removeChild(startSquare.childNodes[0]);
placedPieces[endLetter + endNumber] = promotion ? renderedPromotion : renderedPiece;
endSquare.appendChild(document.createTextNode(promotion ? renderedPromotion : renderedPiece))
}
// Flip whose turn it is
document.getElementById('tomove').childNodes[0].data = whiteMove ? 'Black' : 'White';
// Record our alterations to the table
let moveRecord = {
removedPieces: removedPieces,
placedPieces: placedPieces,
moveText: move
};
moveHistory.push(moveRecord);
return {}
}
@ -447,13 +446,14 @@
}
// Undo move on the board
let {start, end, startPiece, capturedPiece, moveText} = moveHistory.pop();
let startSquare = document.getElementById(start);
let endSquare = document.getElementById(end);
endSquare.removeChild(endSquare.childNodes[0]);
startSquare.appendChild(document.createTextNode(startPiece));
if (capturedPiece) {
endSquare.appendChild(document.createTextNode(capturedPiece));
let {removedPieces, placedPieces, moveText} = moveHistory.pop();
for (let coords in placedPieces) {
let square = document.getElementById(coords);
square.removeChild(square.childNodes[0]);
}
for (let coords in removedPieces) {
let square = document.getElementById(coords);
square.appendChild(document.createTextNode(removedPieces[coords]));
}
// Flip whose turn it is