omit source location in history when unambiguous
This commit is contained in:
parent
cbb8c28542
commit
2248ff2794
91
js/chess.js
91
js/chess.js
|
|
@ -350,12 +350,26 @@ function movePiece(priorBoard, side, from, to){
|
|||
return board;
|
||||
}
|
||||
|
||||
function renderHistory(board) {
|
||||
function findPieces(board, side, type){
|
||||
let pieces = [];
|
||||
|
||||
for (const row of "12345678") {
|
||||
for (const column of "abcdefgh") {
|
||||
const here = column + row;
|
||||
if (boardGet(board, here, side) === type) {
|
||||
pieces.push(here);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pieces;
|
||||
}
|
||||
|
||||
function renderHistory(currentBoard) {
|
||||
let list = [];
|
||||
|
||||
while (board && board.move) {
|
||||
list.push(board.move);
|
||||
board = board.prior;
|
||||
for (let board = currentBoard; board && board.move; board = board.prior) {
|
||||
list.push(board);
|
||||
}
|
||||
|
||||
const NBSP = '\u00a0'; /* non-breaking space */
|
||||
|
|
@ -366,11 +380,11 @@ function renderHistory(board) {
|
|||
let n = 0;
|
||||
|
||||
while (list.length > 0) {
|
||||
const move = list.pop();
|
||||
const board = list.pop();
|
||||
const move = board.move;
|
||||
|
||||
if (move.from === 'phantom') {
|
||||
const piece = move.type === 'p' ? '' : move.type.toUpperCase();
|
||||
const took = move.took ? 'x' : '';
|
||||
result += SHY + '*' + piece + took + move.to;
|
||||
result += SHY + '*';
|
||||
} else {
|
||||
if (n > 0 || isDark(move.side)) {
|
||||
result += ' ';
|
||||
|
|
@ -380,24 +394,61 @@ function renderHistory(board) {
|
|||
++n;
|
||||
result += String(n) + '.' + NBSP;
|
||||
}
|
||||
}
|
||||
|
||||
if (move.pass) {
|
||||
result += '...';
|
||||
} else if (move.alongside) {
|
||||
if (move.pass) {
|
||||
result += '...';
|
||||
} else if (move.castle) {
|
||||
result += 'O-O';
|
||||
} else if (move.queen_castle) {
|
||||
result += 'O-O-O';
|
||||
} else {
|
||||
let piece = '';
|
||||
|
||||
if (move.alongside) {
|
||||
if (isLight(move.side)) {
|
||||
result += move.type.toUpperCase() + move.alongside.toUpperCase() + move.from + move.to;
|
||||
piece = move.type.toUpperCase() + move.alongside.toUpperCase();
|
||||
} else {
|
||||
result += move.alongside.toUpperCase() + move.type.toUpperCase() + move.from + move.to;
|
||||
piece = move.alongside.toUpperCase() + move.type.toUpperCase();
|
||||
}
|
||||
} else if (move.castle) {
|
||||
result += 'O-O';
|
||||
} else if (move.queen_castle) {
|
||||
result += 'O-O-O';
|
||||
} else {
|
||||
const piece = move.type === 'p' ? '' : move.type.toUpperCase();
|
||||
const took = move.took ? 'x' : '';
|
||||
result += piece + move.from + took + move.to;
|
||||
piece = move.type === 'p' ? '' : move.type.toUpperCase();
|
||||
}
|
||||
|
||||
if (move.from !== 'phantom') {
|
||||
const sameKind = findPieces(board.prior, move.side, move.type);
|
||||
const legalFrom = [];
|
||||
let sameFile = 0; /* column / letter */
|
||||
let sameRank = 0; /* row / number */
|
||||
|
||||
for (const where of sameKind) {
|
||||
if (legalMoves(board.prior, move.side, move.type, where, true).includes(move.to)) {
|
||||
legalFrom.push(where);
|
||||
|
||||
if (where[0] === move.from[0]) {
|
||||
sameFile += 1;
|
||||
}
|
||||
|
||||
if (where[1] === move.from[1]) {
|
||||
sameRank += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (legalFrom.length !== 1) {
|
||||
/* append file, rank, or both to disambiguate */
|
||||
if (sameFile === 1) {
|
||||
piece += move.from[0];
|
||||
} else if (sameRank === 1) {
|
||||
piece += move.from[1];
|
||||
} else {
|
||||
piece += move.from;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const took = move.took ? 'x' : '';
|
||||
result += piece + took + move.to;
|
||||
}
|
||||
|
||||
if (move.en_passant) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue