Change the format move history is stored in

This commit is contained in:
Juhani Krekelä 2021-01-10 19:15:11 +02:00
parent c081f45d50
commit 07421d4459
1 changed files with 21 additions and 18 deletions

View File

@ -416,27 +416,29 @@
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]);
}
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 +449,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