fix implementation of en passant to match official rules

This commit is contained in:
Jesse D. McDonald 2020-03-14 17:21:43 -05:00
parent 23c8b1384e
commit bd8dcc6077
1 changed files with 7 additions and 3 deletions

View File

@ -253,7 +253,8 @@ function movePiece(priorBoard, side, from, to){
var other = otherSide(side);
var type = boardGet(priorBoard, from, side);
var took = boardGet(priorBoard, to, other);
var replaced = boardGet(priorBoard, to, side);
var replacedFrom = to;
var replaced = boardGet(priorBoard, replacedFrom, side);
var alongside = boardGet(priorBoard, from, other);
var actuallyFrom = (from === 'phantom') ? priorBoard.phantom.from : from;
@ -306,7 +307,7 @@ function movePiece(priorBoard, side, from, to){
board.move.promotion = 'q'; /* TODO: allow other choices */
alongside = 'q';
}
if (type === 'p' && alongside === ' ' && to[0] !== actuallyFrom[0]) {
if (type === 'p' && alongside === ' ' && replaced === ' ' && to[0] !== actuallyFrom[0]) {
var otherBoard = priorBoard;
while (otherBoard && otherBoard.move && otherBoard.move.side[0] === side[0]) {
otherBoard = otherBoard.prior;
@ -319,6 +320,9 @@ function movePiece(priorBoard, side, from, to){
board.move.en_passant = true;
alongside = 'p';
boardPut(board, move.to, other, ' ');
replacedFrom = move.to;
replaced = boardGet(board, replacedFrom, side);
boardPut(board, replacedFrom, side, ' ');
}
}
}
@ -340,7 +344,7 @@ function movePiece(priorBoard, side, from, to){
if (replaced === ' ') {
board.player = otherSide(board.player);
} else {
board.phantom = { from: to, type: replaced + side[0] };
board.phantom = { from: replacedFrom, type: replaced + side[0] };
}
return board;