-
Notifications
You must be signed in to change notification settings - Fork 0
/
stateChanges.js
302 lines (273 loc) · 10.8 KB
/
stateChanges.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Functions to update the internal state based on clicks on various HTML elements
function setUncapturedPiece(event) {
currentUncapturedUnit = event.target.value;
}
function resetUncapturedPiece() {
document.getElementById("pieceSelection_none").checked = true;
currentUncapturedUnit = "";
}
function resetUnpromote() {
document.getElementById("unpromoteSelection").checked = false;
document.getElementById("pieceSelection_ep").style.visibility = "visible";
}
function getUnpromote() {
return document.getElementById("unpromoteSelection").checked;
}
function toggleUnpromote() {
if (getUnpromote())
document.getElementById("pieceSelection_ep").style.visibility = "hidden";
else document.getElementById("pieceSelection_ep").style.visibility = "visible";
}
function undoGui(permanent) {
// permanent means don't allow redo on this move. It is used in the case when we pop up a warning
// for an illegal position, and the user decides to undo it. We don't want to allow redo back
// to the illegal position.
if (permanent == null) {
permanent = false;
}
if (undo(permanent)) {
resetLegalityCheckerWorker();
updateVisualBoard();
finalizeRetraction();
if (navigatedMoveIndex != null) {
navigatedMoveIndex--;
}
return true;
} else {
showError("No more moves to go back.");
return false;
}
}
function undoAllGui() {
while (undo()) {
if (navigatedMoveIndex != null) {
navigatedMoveIndex--;
}
}
resetLegalityCheckerWorker();
updateVisualBoard();
finalizeRetraction();
}
function redoGui() {
if (redo()) {
updateVisualBoard();
finalizeRetraction();
if (navigatedMoveIndex != null) {
navigatedMoveIndex++;
}
} else {
showError("No more moves to go forward.");
}
}
function redoAllGui() {
while (redo()) {
if (navigatedMoveIndex != null) {
navigatedMoveIndex++;
}
}
updateVisualBoard();
finalizeRetraction();
}
function copyGameGui() {
const moveRecord = undoStack.getMoveRecordToHere();
const textMoveRecord = moveRecord.map(moveToString).join(" ");
document.getElementById("copyGameText").innerHTML = textMoveRecord;
showModal("copyGameModal");
}
function replayGame(moveText) {
const moveTextSplit = moveText.trim().split(/\s+/);
let numMoves = 0;
let illegalMove = null;
for (let i = 0; i < moveTextSplit.length; i++) {
const moveSingleText = moveTextSplit[i];
const move = stringToMove(moveSingleText, currentRetract);
if (move != null) {
const isPseudoLegal =
board[move.from.mFile][move.from.mRank].color == currentRetract
&& board[move.from.mFile][move.from.mRank].unit == move.fromUnit
&& doRetraction(move.from, move.to, move.uncapturedUnit, move.unpromote, true, false) == error_ok;
if (isPseudoLegal) {
numMoves++;
} else {
illegalMove = moveSingleText;
break;
}
}
}
for (let i = 0; i < numMoves; i++) {
assert(undo(), "Could not undo moves just replayed");
}
return [numMoves, illegalMove];
}
function processMoveText(moveText) {
const [numMoves, illegalMove] = replayGame(moveText);
let message;
message = numMoves.toString() + " move" + ((numMoves != 1) ? "s" : "") + " replayed.";
if (numMoves > 0) {
message += " Use 'forward' button to view.";
disableSolutionButtons();
}
if (illegalMove != null) {
message += " Stopped at illegal move " + illegalMove;
}
showError(message);
}
function resetSolver() {
if (solverWorker && solverActive) {
solverWorker.terminate();
solverWorker = null;
}
solverActive = false;
clearModal("solveModal");
document.getElementById("startSolve").value = "Solve";
document.getElementById("solveErrorMessage").innerHTML = "";
}
let solverWorker = null;
let solverActive = false;
let solutions;
function solveGui() {
if (!solverWorker) {
solverWorker = new Worker("solverWorker.js");
solverWorker.onmessage = function (e) {
solverActive = false;
solutions = e.data[0];
getPawnCaptureCache().set(e.data[1]);
const maybeTruncated = e.data[2];
const timeInSeconds = e.data[3];
resetSolver();
if (solutions.length == 0) {
showError("No solutions found; position is impossible. Time: " + timeInSeconds + " seconds.");
} else {
showError("Solve finished in " + timeInSeconds + " seconds.");
}
showSolutions(maybeTruncated);
updateVisualBoard();
}
}
if (!solverActive) {
const solveDepth = parseInt(document.getElementById("solveDepth").value, 10);
const extraDepth = parseInt(document.getElementById("extraDepth").value, 10);
const maxSolutions = parseInt(document.getElementById("maxSolutions").value, 10);
const noWhiteUncaptures = document.getElementById("noWhiteUncaptures").checked;
const noBlackUncaptures = document.getElementById("noBlackUncaptures").checked;
if (isNaN(solveDepth) || isNaN(extraDepth) || isNaN(maxSolutions)) {
document.getElementById("solveErrorMessage").innerHTML = "Invalid parameters";
return;
}
document.getElementById("startSolve").value = "Cancel Solve";
document.getElementById("solveErrorMessage").innerHTML = "Solving, please wait...";
resetLegalityCheckerWorker();
solverActive = true;
solverWorker.postMessage([new SolveParameters(solveDepth, extraDepth, maxSolutions, noWhiteUncaptures, noBlackUncaptures), board,
currentRetract, positionData.ep, getPawnCaptureCache(), knownCages, getPawnCaptureConfig()]);
} else { // cancel
resetSolver();
}
}
function setRetractGui(event) {
if (currentMode == MODE_EDIT) {
setRetract(event.target.value);
}
}
function verifyCageGui() {
verifyCageSelectionMode = true;
document.getElementById("verifyCageSelectAll").style.visibility = "visible";
document.getElementById("verifyCageUnselectAll").style.visibility = "visible";
document.getElementById("verifyCageMessage").innerHTML = "Click the squares forming the cage to be verified.";
document.getElementById("verifyCageVerify").value = "Verify";
document.getElementById("verifyCageTryAnother").style.visibility = "hidden";
document.getElementById("verifyCageImport").style.visibility = "visible";
document.getElementById("verifyCageExport").style.visibility = "visible";
if (serializedRequestedCages.length > 0) {
document.getElementById("verifyCageExport").removeAttribute("disabled");
} else {
document.getElementById("verifyCageExport").setAttribute("disabled", "");
}
// We set the board's zIndex higher than the modal's so that we can click on the board squares.
document.getElementById("board").style.zIndex = 3;
showModal('verifyCageModal');
}
function showError(error) {
document.getElementById("errorMessage").innerHTML = error;
}
function setMode(event) {
const newMode = event.target.value == "edit" ? MODE_EDIT : MODE_PLAY;
if (newMode == MODE_EDIT) {
switchToEditMode(true);
} else { // play mode
switchToPlayMode();
}
return true;
}
function switchToPlayMode() {
currentMode = MODE_PLAY;
startPlay(false);
document.getElementById("modePlay").checked = true;
document.getElementById("modeEdit").checked = false;
document.getElementById("ep_and_unpromote").style.visibility = "visible";
document.getElementById("black_pieces").style.visibility = "hidden";
document.getElementById("retractRadioButtons").style.display = "none";
document.getElementById("retractColor").innerHTML = (currentRetract == "w" ? "White" : "Black");
document.getElementById("clearBoardGroup").style.display = "none";
document.getElementById("controlGroup").style.display = "block";
document.getElementById("setPositionButton").style.visibility = "hidden";
document.getElementById("pawnCapturesTable").hidden = false;
document.getElementById("promoteeTable").hidden = false;
document.getElementById("config").hidden = true;
document.getElementById("forsytheText").setAttribute("readonly", "");
document.getElementById("moves").style.display = "block";
solutions = null;
navigatedMoveIndex = null;
finalizeRetraction();
updateVisualBoard();
asyncLegalityCheck();
}
function switchToEditMode(clearFlags) {
currentMode = MODE_EDIT;
stopPlay(clearFlags);
resetSolver();
resetLegalityCheckerWorker();
solutions = null;
document.getElementById("modePlay").checked = false;
document.getElementById("modeEdit").checked = true;
document.getElementById("ep_and_unpromote").style.visibility = "hidden";
document.getElementById("pieceSelection_ep").style.visibility = "hidden";
document.getElementById("black_pieces").style.visibility = "visible";
document.getElementById("retractRadioButtons").style.display = "block";
document.getElementById("retractColor").innerHTML = "";
document.getElementById("retractWhite").checked = (currentRetract == "w");
document.getElementById("retractBlack").checked = (currentRetract == "b");
document.getElementById("clearBoardGroup").style.display = "block";
document.getElementById("controlGroup").style.display = "none";
document.getElementById("setPositionButton").style.visibility = "visible";
document.getElementById("forsytheText").removeAttribute("readonly");
document.getElementById("exportSolutionsSpan").style.visibility = "hidden";
document.getElementById("solutionDisplay").innerHTML = "";
document.getElementById("moves").style.display = "none";
document.getElementById("pawnCapturesTable").hidden = true;
document.getElementById("promoteeTable").hidden = true;
document.getElementById("config").hidden = false;
resetUncapturedPiece();
showError("");
updateEditModeData();
}
function setPosition() {
if (currentMode == MODE_EDIT) {
const forsythe = document.getElementById("forsytheText").value.trim();
const err = validateForsythe(forsythe);
if (err == "ok") {
setForsythe(forsythe);
updateVisualBoard();
showError("");
} else showError("Invalid Forsythe notation: " + err);
}
}
function download(data, filename) {
const url = window.URL.createObjectURL(data);
fakeDownload.href = url;
fakeDownload.download = filename;
document.body.appendChild(fakeDownload);
fakeDownload.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(fakeDownload);
}