diff --git a/takhta.html b/takhta.html index f70c639..3943642 100644 --- a/takhta.html +++ b/takhta.html @@ -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