Compare commits
2 Commits
c081f45d50
...
5ef202d1b5
Author | SHA1 | Date |
---|---|---|
Juhani Krekelä | 5ef202d1b5 | |
Juhani Krekelä | 07421d4459 |
52
takhta.html
52
takhta.html
|
@ -390,11 +390,6 @@
|
||||||
renderedPromotion = whiteMove ? whitePieces[promotion] : blackPieces[promotion];
|
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?
|
// Are we moving the piece from where it is located?
|
||||||
let startSquare = document.getElementById(startLetter + startNumber);
|
let startSquare = document.getElementById(startLetter + startNumber);
|
||||||
if (startSquare.childNodes.length === 0) {
|
if (startSquare.childNodes.length === 0) {
|
||||||
|
@ -409,34 +404,38 @@
|
||||||
|
|
||||||
// Can the piece move to this location?
|
// Can the piece move to this location?
|
||||||
let endSquare = document.getElementById(endLetter + endNumber);
|
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?'};
|
return {error: 'Square ' + endLetter + endNumber + ' is occupied, did you mean to capture?'};
|
||||||
}
|
}
|
||||||
if (endSquare.childNodes.length === 0 && captures) {
|
if (endSquare.childNodes.length === 0 && captures) {
|
||||||
return {error: 'Square ' + endLetter + endNumber + ' empty even though capture was specified'};
|
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
|
// Apply move
|
||||||
|
let removedPieces = {}
|
||||||
|
let placedPieces = {}
|
||||||
if (captures) {
|
if (captures) {
|
||||||
|
removedPieces[endLetter + endNumber] = endSquare.childNodes[0].data;
|
||||||
endSquare.removeChild(endSquare.childNodes[0]);
|
endSquare.removeChild(endSquare.childNodes[0]);
|
||||||
}
|
}
|
||||||
startSquare.removeChild(startSquare.childNodes[0]);
|
if (startSquare !== endSquare || !captures) {
|
||||||
endSquare.appendChild(document.createTextNode(promotion ? renderedPromotion : renderedPiece))
|
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
|
// Flip whose turn it is
|
||||||
document.getElementById('tomove').childNodes[0].data = whiteMove ? 'Black' : 'White';
|
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 {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -447,13 +446,14 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
// Undo move on the board
|
// Undo move on the board
|
||||||
let {start, end, startPiece, capturedPiece, moveText} = moveHistory.pop();
|
let {removedPieces, placedPieces, moveText} = moveHistory.pop();
|
||||||
let startSquare = document.getElementById(start);
|
for (let coords in placedPieces) {
|
||||||
let endSquare = document.getElementById(end);
|
let square = document.getElementById(coords);
|
||||||
endSquare.removeChild(endSquare.childNodes[0]);
|
square.removeChild(square.childNodes[0]);
|
||||||
startSquare.appendChild(document.createTextNode(startPiece));
|
}
|
||||||
if (capturedPiece) {
|
for (let coords in removedPieces) {
|
||||||
endSquare.appendChild(document.createTextNode(capturedPiece));
|
let square = document.getElementById(coords);
|
||||||
|
square.appendChild(document.createTextNode(removedPieces[coords]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Flip whose turn it is
|
// Flip whose turn it is
|
||||||
|
|
Loading…
Reference in New Issue