clean up the logic around en passant and pawn promotion

This commit is contained in:
Jesse D. McDonald 2020-03-15 12:58:14 -05:00
parent fad973b1bc
commit baf4c19f1d
1 changed files with 20 additions and 11 deletions

View File

@ -280,35 +280,32 @@ function movePiece(priorBoard, side, from, to){
from: from, from: from,
to: to to: to
}; };
if (took !== ' ') { if (took !== ' ') {
board.move.took = took; board.move.took = took;
} }
if (replaced !== ' ') { if (replaced !== ' ') {
board.move.replaced = replaced; board.move.replaced = replaced;
} }
if (alongside !== ' ') { if (alongside !== ' ') {
board.move.alongside = alongside; board.move.alongside = alongside;
} }
if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'g') { if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'g') {
board.move.castle = true; board.move.castle = true;
boardPut(board, 'h' + actuallyFrom[1], side, ' '); boardPut(board, 'h' + actuallyFrom[1], side, ' ');
boardPut(board, 'f' + actuallyFrom[1], side, 'r'); boardPut(board, 'f' + actuallyFrom[1], side, 'r');
} }
if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'c') { else if (type === 'k' && actuallyFrom[0] === 'e' && to[0] === 'c') {
board.move.queen_castle = true; board.move.queen_castle = true;
boardPut(board, 'a' + actuallyFrom[1], side, ' '); boardPut(board, 'a' + actuallyFrom[1], side, ' ');
boardPut(board, 'd' + actuallyFrom[1], side, 'r'); boardPut(board, 'd' + actuallyFrom[1], side, 'r');
} }
if (type === 'p' && (isDark(side) ? (to[1] === '1') : (to[1] === '8'))) { else if (type === 'p' && alongside === ' ' && replaced === ' ' && to[0] !== actuallyFrom[0]) {
board.move.promotion = 'q'; /* TODO: allow other choices */
type = 'q';
}
if (alongside === 'p' && (isDark(other) ? (to[1] === '1') : (to[1] === '8'))) {
board.move.promotion = 'q'; /* TODO: allow other choices */
alongside = 'q';
}
if (type === 'p' && alongside === ' ' && replaced === ' ' && to[0] !== actuallyFrom[0]) {
let otherBoard = priorBoard; let otherBoard = priorBoard;
/* scan for the opponent's last move, since this could be part of a chain */
while (otherBoard && otherBoard.move && otherBoard.move.side[0] === side[0]) { while (otherBoard && otherBoard.move && otherBoard.move.side[0] === side[0]) {
otherBoard = otherBoard.prior; otherBoard = otherBoard.prior;
} }
@ -318,9 +315,11 @@ function movePiece(priorBoard, side, from, to){
const moveFrom = (move.from === 'phantom') ? otherBoard.prior.phantom.from : move.from; const moveFrom = (move.from === 'phantom') ? otherBoard.prior.phantom.from : move.from;
if (move.side[0] === other[0] && moveFrom[1] != to[1]) { if (move.side[0] === other[0] && moveFrom[1] != to[1]) {
board.move.en_passant = true; board.move.en_passant = true;
alongside = 'p';
board.move.took = 'p'; board.move.took = 'p';
/* move the opponent's pawn back one space */
boardPut(board, move.to, other, ' '); boardPut(board, move.to, other, ' ');
boardPut(board, to, other, 'p');
/* see if we replaced a piece from the other square */
replacedFrom = move.to; replacedFrom = move.to;
replaced = boardGet(board, replacedFrom, side); replaced = boardGet(board, replacedFrom, side);
boardPut(board, replacedFrom, side, ' '); boardPut(board, replacedFrom, side, ' ');
@ -329,6 +328,16 @@ function movePiece(priorBoard, side, from, to){
} }
} }
if (type === 'p' && to[1] === (isDark(side) ? '1' : '8')) {
board.move.promotion = 'q'; /* TODO: allow other choices */
type = 'q';
}
if (alongside === 'p' && to[1] === (isDark(other) ? '1' : '8')) {
board.move.promotion = 'q'; /* TODO: allow other choices */
alongside = 'q';
}
if (from === 'phantom') { if (from === 'phantom') {
delete board.phantom; delete board.phantom;
} else { } else {