Add piece promotion
This commit is contained in:
parent
1d65249948
commit
1e06e07a10
|
@ -194,6 +194,7 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// Piece
|
||||
let piece = move.slice(0, 1);
|
||||
let index = 0;
|
||||
switch(piece) {
|
||||
|
@ -220,6 +221,7 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// Start square
|
||||
let startLetter = move.slice(index, index + 1);
|
||||
let startNumber = move.slice(index + 1, index + 2);
|
||||
index += 2;
|
||||
|
@ -231,12 +233,14 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// Capturing
|
||||
let captures = false;
|
||||
if (move.slice(index, index + 1) === 'x') {
|
||||
captures = true;
|
||||
index++;
|
||||
}
|
||||
|
||||
// End square
|
||||
let endLetter = move.slice(index, index + 1);
|
||||
let endNumber = move.slice(index + 1, index + 2);
|
||||
index += 2;
|
||||
|
@ -248,6 +252,33 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// Promotion
|
||||
let promotion = null;
|
||||
if (move.slice(index, index + 1) === '=') {
|
||||
index++;
|
||||
switch (move.slice(index, index + 1)) {
|
||||
case 'Q':
|
||||
case 'B':
|
||||
case 'N':
|
||||
case 'R':
|
||||
promotion = move.slice(index, index + 1);
|
||||
break;
|
||||
case 'K':
|
||||
alert('Cannot promote a piece to king');
|
||||
return;
|
||||
break;
|
||||
case 'P':
|
||||
alert('Cannot promote a piece to pawn');
|
||||
return;
|
||||
break;
|
||||
default:
|
||||
alert('Unrecognized piece: ' + move.slice(index, index + 1));
|
||||
return;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
// Check(mate)
|
||||
switch(move.slice(index)) {
|
||||
case '':
|
||||
// nothing
|
||||
|
@ -263,16 +294,16 @@
|
|||
return;
|
||||
}
|
||||
|
||||
// End of parsing, start of move construction
|
||||
let whitePieces = {'K': '♔', 'Q': '♕', 'R': '♖', 'B': '♗', 'N': '♘', 'P': '♙'};
|
||||
let blackPieces = {'K': '♚', 'Q': '♛', 'R': '♜', 'B': '♝', 'N': '♞', 'P': '♟'};
|
||||
|
||||
let whiteMove = document.getElementById('tomove').childNodes[0].data == 'White';
|
||||
|
||||
let renderedPiece = null;
|
||||
if (whiteMove) {
|
||||
renderedPiece = whitePieces[piece];
|
||||
} else {
|
||||
renderedPiece = blackPieces[piece];
|
||||
let renderedPiece = whiteMove ? whitePieces[piece] : blackPieces[piece];
|
||||
let renderedPromotion = null;
|
||||
if (promotion) {
|
||||
renderedPromotion = whiteMove ? whitePieces[promotion] : blackPieces[promotion];
|
||||
}
|
||||
|
||||
// Are we moving the piece from where it is located?
|
||||
|
@ -282,7 +313,7 @@
|
|||
return;
|
||||
}
|
||||
if (startSquare.childNodes[0].data !== renderedPiece) {
|
||||
let pieceNames = {'K': 'King', 'Q': 'Vizier', 'R': 'Rook', 'B': 'Elephant', 'N': 'Horse', 'P': 'Pawn'};
|
||||
let pieceNames = {'K': 'king', 'Q': 'vizier', 'R': 'rook', 'B': 'elephant', 'N': 'horse', 'P': 'pawn'};
|
||||
alert((whiteMove ? 'White' : 'Black') + ' ' + pieceNames[piece] + ' not on square ' + startLetter + startNumber);
|
||||
return;
|
||||
}
|
||||
|
@ -301,17 +332,21 @@
|
|||
}
|
||||
|
||||
// Record our alterations to the table
|
||||
let movedPiece = {start: startLetter + startNumber, end: endLetter + endNumber, piece: renderedPiece};
|
||||
let capturedPiece = captures ? endSquare.childNodes[0].data : null;
|
||||
moveHistory.push({move: movedPiece, capture: capturedPiece});
|
||||
console.log(moveHistory);
|
||||
let moveRecord = {
|
||||
start: startLetter + startNumber,
|
||||
end: endLetter + endNumber,
|
||||
startPiece: renderedPiece,
|
||||
endPiece: promotion ? renderedPromotion : renderedPiece,
|
||||
capturedPiece: captures ? endSquare.childNodes[0].data : null
|
||||
};
|
||||
moveHistory.push(moveRecord);
|
||||
|
||||
// Apply move
|
||||
if (captures) {
|
||||
endSquare.removeChild(endSquare.childNodes[0]);
|
||||
}
|
||||
startSquare.removeChild(startSquare.childNodes[0]);
|
||||
endSquare.appendChild(document.createTextNode(renderedPiece))
|
||||
endSquare.appendChild(document.createTextNode(promotion ? renderedPromotion : renderedPiece))
|
||||
|
||||
// Flip whose turn it is
|
||||
document.getElementById('tomove').childNodes[0].data = whiteMove ? 'Black' : 'White';
|
||||
|
@ -332,13 +367,13 @@
|
|||
}
|
||||
|
||||
// Undo move on the board
|
||||
let {move, capture} = moveHistory.pop();
|
||||
let startSquare = document.getElementById(move.start);
|
||||
let endSquare = document.getElementById(move.end);
|
||||
let {start, end, startPiece, endPiece, capturedPiece} = moveHistory.pop();
|
||||
let startSquare = document.getElementById(start);
|
||||
let endSquare = document.getElementById(end);
|
||||
endSquare.removeChild(endSquare.childNodes[0]);
|
||||
startSquare.appendChild(document.createTextNode(move.piece));
|
||||
if (capture) {
|
||||
endSquare.appendChild(document.createTextNode(capture));
|
||||
startSquare.appendChild(document.createTextNode(startPiece));
|
||||
if (capturedPiece) {
|
||||
endSquare.appendChild(document.createTextNode(capturedPiece));
|
||||
}
|
||||
|
||||
// Flip whose turn it is
|
||||
|
|
Loading…
Reference in New Issue