add more precise disambiguation of pieces in history
This commit is contained in:
parent
5011cfed8c
commit
0769d7579b
57
js/chess.js
57
js/chess.js
|
|
@ -79,6 +79,24 @@ function boardPut(board, where, side, piece){
|
||||||
board[side][row] = prefix + piece + suffix;
|
board[side][row] = prefix + piece + suffix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function movedFrom(board){
|
||||||
|
if (!board || !board.move || !board.move.from) {
|
||||||
|
return null;
|
||||||
|
} else if (board.move.from === 'phantom') {
|
||||||
|
return board.prior.phantom.from;
|
||||||
|
} else {
|
||||||
|
return board.move.from;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function movedTo(board){
|
||||||
|
if (!board || !board.move || !board.move.to) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return board.move.to;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function countMoves(board) {
|
function countMoves(board) {
|
||||||
let n = 0;
|
let n = 0;
|
||||||
while (board && board.prior) {
|
while (board && board.prior) {
|
||||||
|
|
@ -145,6 +163,7 @@ function hasMoved(board, side, where){
|
||||||
|
|
||||||
function legalMoves(board, side, type, from, canCapture){
|
function legalMoves(board, side, type, from, canCapture){
|
||||||
if (board.move && board.move.took === 'k') {
|
if (board.move && board.move.took === 'k') {
|
||||||
|
/* checkmate, the game is over */
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -236,11 +255,8 @@ function legalMoves(board, side, type, from, canCapture){
|
||||||
if (otherBoard && otherBoard.move) {
|
if (otherBoard && otherBoard.move) {
|
||||||
const move = otherBoard.move;
|
const move = otherBoard.move;
|
||||||
if (move.side[0] !== side[0] && move.type === 'p') {
|
if (move.side[0] !== side[0] && move.type === 'p') {
|
||||||
const from =
|
const from = movedFrom(otherBoard);
|
||||||
(move.from === 'phantom') ?
|
if (from && from[0] === there[0] && from[1] === forward2[1]) {
|
||||||
otherBoard.prior.phantom.from :
|
|
||||||
move.from;
|
|
||||||
if (from[0] === there[0] && from[1] === forward2[1]) {
|
|
||||||
/* en passant */
|
/* en passant */
|
||||||
legals.push(there);
|
legals.push(there);
|
||||||
}
|
}
|
||||||
|
|
@ -324,7 +340,7 @@ function movePiece(priorBoard, side, from, to){
|
||||||
if (otherBoard && otherBoard.move) {
|
if (otherBoard && otherBoard.move) {
|
||||||
const move = otherBoard.move;
|
const move = otherBoard.move;
|
||||||
if (move.type === 'p' && move.to[0] === to[0] && move.to[1] === actuallyFrom[1]) {
|
if (move.type === 'p' && move.to[0] === to[0] && move.to[1] === actuallyFrom[1]) {
|
||||||
const moveFrom = (move.from === 'phantom') ? otherBoard.prior.phantom.from : move.from;
|
const moveFrom = movedFrom(otherBoard);
|
||||||
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;
|
||||||
board.move.took = 'p';
|
board.move.took = 'p';
|
||||||
|
|
@ -372,14 +388,21 @@ function movePiece(priorBoard, side, from, to){
|
||||||
return board;
|
return board;
|
||||||
}
|
}
|
||||||
|
|
||||||
function findPieces(board, side, type){
|
function findPieces(board, side, type, alongside){
|
||||||
let pieces = [];
|
let pieces = [];
|
||||||
|
const other = otherSide(side);
|
||||||
|
|
||||||
|
if (board.phantom && board.phantom.type === type + side[0]) {
|
||||||
|
pieces.push(board.phantom.from);
|
||||||
|
}
|
||||||
|
|
||||||
for (const row of "12345678") {
|
for (const row of "12345678") {
|
||||||
for (const column of "abcdefgh") {
|
for (const column of "abcdefgh") {
|
||||||
const here = column + row;
|
const here = column + row;
|
||||||
if (boardGet(board, here, side) === type) {
|
if (boardGet(board, here, side) === type) {
|
||||||
pieces.push(here);
|
if (!alongside || boardGet(board, here, other) === alongside) {
|
||||||
|
pieces.push(here);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -437,8 +460,10 @@ function renderHistory(currentBoard) {
|
||||||
piece = move.type === 'p' ? '' : move.type.toUpperCase();
|
piece = move.type === 'p' ? '' : move.type.toUpperCase();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (move.from !== 'phantom') {
|
const from = movedFrom(board);
|
||||||
const sameKind = findPieces(board.prior, move.side, move.type);
|
|
||||||
|
if (move.from !== 'phantom' || from !== movedTo(board.prior)) {
|
||||||
|
const sameKind = findPieces(board.prior, move.side, move.type, move.alongside || ' ');
|
||||||
const legalFrom = [];
|
const legalFrom = [];
|
||||||
let sameFile = 0; /* column / letter */
|
let sameFile = 0; /* column / letter */
|
||||||
let sameRank = 0; /* row / number */
|
let sameRank = 0; /* row / number */
|
||||||
|
|
@ -447,24 +472,24 @@ function renderHistory(currentBoard) {
|
||||||
if (legalMoves(board.prior, move.side, move.type, where, true).includes(move.to)) {
|
if (legalMoves(board.prior, move.side, move.type, where, true).includes(move.to)) {
|
||||||
legalFrom.push(where);
|
legalFrom.push(where);
|
||||||
|
|
||||||
if (where[0] === move.from[0]) {
|
if (where[0] === from[0]) {
|
||||||
sameFile += 1;
|
sameFile += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (where[1] === move.from[1]) {
|
if (where[1] === from[1]) {
|
||||||
sameRank += 1;
|
sameRank += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (legalFrom.length !== 1) {
|
if (legalFrom.length !== 1 || move.en_passant || (move.type === 'p' && move.took)) {
|
||||||
/* append file, rank, or both to disambiguate */
|
/* append file, rank, or both to disambiguate */
|
||||||
if (sameFile === 1) {
|
if (sameFile === 1) {
|
||||||
piece += move.from[0];
|
piece += from[0];
|
||||||
} else if (sameRank === 1) {
|
} else if (sameRank === 1) {
|
||||||
piece += move.from[1];
|
piece += from[1];
|
||||||
} else {
|
} else {
|
||||||
piece += move.from;
|
piece += from;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue