factor out the code to diff game history into a utility function
This commit is contained in:
parent
66b1bb05f6
commit
5be70b8891
124
js/pacosako.js
124
js/pacosako.js
|
|
@ -397,6 +397,69 @@ function addHistory(game) {
|
|||
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 {
|
||||
constructor(original) {
|
||||
if (original !== undefined) {
|
||||
|
|
@ -689,28 +752,12 @@ class Game {
|
|||
if (sim.isLegalMove(other, from, king, true)) {
|
||||
/* this piece can directly capture the king */
|
||||
let check = true;
|
||||
try {
|
||||
if (this._history !== undefined) {
|
||||
const hist = new Game(this);
|
||||
hist.ignoreCheck = true;
|
||||
if (hist._player !== other) {
|
||||
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) {}
|
||||
if (this._history !== undefined) {
|
||||
try {
|
||||
sim.move(from, king);
|
||||
check = diffHistory(this, sim) || true;
|
||||
} catch(err) {}
|
||||
}
|
||||
return recordCheck(this, check);
|
||||
}
|
||||
|
||||
|
|
@ -761,31 +808,12 @@ class Game {
|
|||
if (game2.isLegalMove(other, PHANTOM, king, true)) {
|
||||
/* we have our answer */
|
||||
let check = true;
|
||||
try {
|
||||
if (this._history !== undefined) {
|
||||
const hist = new Game(this);
|
||||
hist.ignoreCheck = true;
|
||||
if (hist._player !== other) {
|
||||
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) {}
|
||||
if (this._history !== undefined) {
|
||||
try {
|
||||
game2.move(PHANTOM, king);
|
||||
check = diffHistory(this, game2) || true;
|
||||
} catch(err) {}
|
||||
}
|
||||
return recordCheck(this, check);
|
||||
}
|
||||
|
||||
|
|
@ -1163,7 +1191,7 @@ export default {
|
|||
ROWS, COLUMNS, PHANTOM,
|
||||
|
||||
/* Miscellaneous */
|
||||
Util: { otherSide, offsetSquare },
|
||||
Util: { otherSide, offsetSquare, diffHistory },
|
||||
};
|
||||
|
||||
/* vim:set expandtab sw=3 ts=8: */
|
||||
|
|
|
|||
Loading…
Reference in New Issue