factor out the code to diff game history into a utility function
This commit is contained in:
parent
66b1bb05f6
commit
5be70b8891
116
js/pacosako.js
116
js/pacosako.js
|
|
@ -397,6 +397,69 @@ function addHistory(game) {
|
||||||
game._history += result;
|
game._history += result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function diffHistory(game1, game2) {
|
||||||
|
let prefixLength = 0;
|
||||||
|
|
||||||
|
while (prefixLength < game1._moves.length && prefixLength < game2._moves.length) {
|
||||||
|
const move1 = game1._moves[prefixLength];
|
||||||
|
const move2 = game2._moves[prefixLength];
|
||||||
|
|
||||||
|
if (move2.resign !== move1.resign ||
|
||||||
|
move2.from !== move1.from ||
|
||||||
|
move2.to !== move1.to) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
prefixLength += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const hist = new Game(game1._history === undefined ? game2 : game1);
|
||||||
|
game1 = undefined;
|
||||||
|
|
||||||
|
hist.clearRedo();
|
||||||
|
while (hist._moves.length > prefixLength) {
|
||||||
|
hist.undo();
|
||||||
|
hist.clearRedo();
|
||||||
|
}
|
||||||
|
|
||||||
|
const turn_before = hist._turn;
|
||||||
|
const history_before = hist.renderHistory();
|
||||||
|
hist.ignoreCheck = game2.ignoreCheck;
|
||||||
|
|
||||||
|
for (const move of game2._moves.slice(prefixLength)) {
|
||||||
|
if (hist._player !== move.side) {
|
||||||
|
if (hist._player === LIGHT) {
|
||||||
|
hist._turn += 1;
|
||||||
|
hist._history += `${hist._turn === 1 ? '' : ' '}${hist._turn}.${NBSP}…`;
|
||||||
|
} else {
|
||||||
|
hist._history += ` …`;
|
||||||
|
}
|
||||||
|
|
||||||
|
hist._player = move.side;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
hist._status = PLAYING;
|
||||||
|
hist.replayMove(move);
|
||||||
|
} catch(err) {
|
||||||
|
try {
|
||||||
|
hist.ignoreCheck = true;
|
||||||
|
hist.replayMove(move);
|
||||||
|
hist.ignoreCheck = game2.ignoreCheck;
|
||||||
|
} catch(err) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let suffix = hist.history.slice(history_before.length).trimStart();
|
||||||
|
if (!suffix.match(/^\d+\./)) {
|
||||||
|
suffix = `${turn_before}.${NBSP}… ${suffix}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return suffix;
|
||||||
|
}
|
||||||
|
|
||||||
class Game {
|
class Game {
|
||||||
constructor(original) {
|
constructor(original) {
|
||||||
if (original !== undefined) {
|
if (original !== undefined) {
|
||||||
|
|
@ -689,28 +752,12 @@ class Game {
|
||||||
if (sim.isLegalMove(other, from, king, true)) {
|
if (sim.isLegalMove(other, from, king, true)) {
|
||||||
/* this piece can directly capture the king */
|
/* this piece can directly capture the king */
|
||||||
let check = true;
|
let check = true;
|
||||||
try {
|
|
||||||
if (this._history !== undefined) {
|
if (this._history !== undefined) {
|
||||||
const hist = new Game(this);
|
try {
|
||||||
hist.ignoreCheck = true;
|
sim.move(from, king);
|
||||||
if (hist._player !== other) {
|
check = diffHistory(this, sim) || true;
|
||||||
if (hist._player === LIGHT) {
|
|
||||||
hist._turn += 1;
|
|
||||||
hist._history += ` ${hist._turn}.${NBSP}…`;
|
|
||||||
} else {
|
|
||||||
hist._history += ` …`;
|
|
||||||
}
|
|
||||||
hist._player = other;
|
|
||||||
}
|
|
||||||
const turn_before = hist._turn;
|
|
||||||
const history_before = hist._history;
|
|
||||||
hist.move(from, king);
|
|
||||||
check = hist.history.slice(history_before.length).trimStart();
|
|
||||||
if (!check.match(/^\d+\./)) {
|
|
||||||
check = `${turn_before}.${NBSP}… ${check}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(err) {}
|
} catch(err) {}
|
||||||
|
}
|
||||||
return recordCheck(this, check);
|
return recordCheck(this, check);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -761,31 +808,12 @@ class Game {
|
||||||
if (game2.isLegalMove(other, PHANTOM, king, true)) {
|
if (game2.isLegalMove(other, PHANTOM, king, true)) {
|
||||||
/* we have our answer */
|
/* we have our answer */
|
||||||
let check = true;
|
let check = true;
|
||||||
try {
|
|
||||||
if (this._history !== undefined) {
|
if (this._history !== undefined) {
|
||||||
const hist = new Game(this);
|
try {
|
||||||
hist.ignoreCheck = true;
|
game2.move(PHANTOM, king);
|
||||||
if (hist._player !== other) {
|
check = diffHistory(this, game2) || true;
|
||||||
if (hist._player === LIGHT) {
|
|
||||||
hist._turn += 1;
|
|
||||||
hist._history += ` ${hist._turn}.${NBSP}…`;
|
|
||||||
} else {
|
|
||||||
hist._history += ` …`;
|
|
||||||
}
|
|
||||||
hist._player = other;
|
|
||||||
}
|
|
||||||
const turn_before = hist._turn;
|
|
||||||
const history_before = hist._history;
|
|
||||||
for (const move of game2._moves.slice(hist._moves.length)) {
|
|
||||||
hist.replayMove(move);
|
|
||||||
}
|
|
||||||
hist.move(PHANTOM, king);
|
|
||||||
check = hist.history.slice(history_before.length).trimStart();
|
|
||||||
if (!check.match(/^\d+\./)) {
|
|
||||||
check = `${turn_before}.${NBSP}… ${check}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch(err) {}
|
} catch(err) {}
|
||||||
|
}
|
||||||
return recordCheck(this, check);
|
return recordCheck(this, check);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1163,7 +1191,7 @@ export default {
|
||||||
ROWS, COLUMNS, PHANTOM,
|
ROWS, COLUMNS, PHANTOM,
|
||||||
|
|
||||||
/* Miscellaneous */
|
/* Miscellaneous */
|
||||||
Util: { otherSide, offsetSquare },
|
Util: { otherSide, offsetSquare, diffHistory },
|
||||||
};
|
};
|
||||||
|
|
||||||
/* vim:set expandtab sw=3 ts=8: */
|
/* vim:set expandtab sw=3 ts=8: */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue