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'}; 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]);
} }
removedPieces[startLetter + startNumber] = renderedPiece;
startSquare.removeChild(startSquare.childNodes[0]); startSquare.removeChild(startSquare.childNodes[0]);
placedPieces[endLetter + endNumber] = promotion ? renderedPromotion : renderedPiece;
endSquare.appendChild(document.createTextNode(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 +449,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