Allow piece to move onto its own square and capture itself
This commit is contained in:
parent
07421d4459
commit
5ef202d1b5
17
takhta.html
17
takhta.html
|
@ -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,7 +404,7 @@
|
|||
|
||||
// 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) {
|
||||
|
@ -423,10 +418,12 @@
|
|||
removedPieces[endLetter + endNumber] = endSquare.childNodes[0].data;
|
||||
endSquare.removeChild(endSquare.childNodes[0]);
|
||||
}
|
||||
removedPieces[startLetter + startNumber] = renderedPiece;
|
||||
startSquare.removeChild(startSquare.childNodes[0]);
|
||||
placedPieces[endLetter + endNumber] = promotion ? renderedPromotion : renderedPiece;
|
||||
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';
|
||||
|
|
Loading…
Reference in New Issue