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