-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolutions.js
More file actions
179 lines (159 loc) · 6.62 KB
/
solutions.js
File metadata and controls
179 lines (159 loc) · 6.62 KB
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
let navigatedSolutionIndex = 0;
let navigatedMoveIndex = 0;
const solutionIdPrefix = 'solution';
const copySolutionIdPrefix = 'copy';
function navigateToOriginalSolvePosition() {
if (navigatedMoveIndex >= 0) {
for (let i = 0; i < navigatedMoveIndex; i++) {
assert(undo(), 'Could not undo move in navigateToMove, navigatedMoveIndex = ' + navigatedMoveIndex);
}
} else {
for (let i = 0; i < -navigatedMoveIndex; i++) {
assert(redo(), 'Could not redo move in navigateToMove, navigatedMoveIndex = ' + navigatedMoveIndex);
}
}
navigatedMoveIndex = 0;
}
function navigateToSolutionPosition(solutionIndex, moveIndex) {
navigateToOriginalSolvePosition();
for (let i = 0; i < moveIndex; i++) {
const move = solutions[solutionIndex][i];
// we only check for pseudo-legality here because a new cage might change a legal move to an illegal move
assert(doRetraction(move.from, move.to, move.uncapturedUnit, move.unpromote, true, false) == error_ok,
"Non-pseudolegal move " + moveToString(move) + " found in solution");
}
navigatedSolutionIndex = solutionIndex;
navigatedMoveIndex = moveIndex;
}
function navigateToMove(event) {
const clickedButtonId = event.target.id;
const idSplit = clickedButtonId.split("_");
const solutionIndex = parseInt(idSplit[1]);
const moveIndex = parseInt(idSplit[2]);
navigateToSolutionPosition(solutionIndex, moveIndex);
updateVisualBoard();
finalizeRetraction();
}
function restoreOriginalPosition(originalNavigatedSolutionIndex, originalNavigatedMoveIndex) {
if (originalNavigatedMoveIndex >= 0) {
navigateToSolutionPosition(originalNavigatedSolutionIndex, originalNavigatedMoveIndex);
} else {
navigateToOriginalSolvePosition();
navigatedSolutionIndex = originalNavigatedSolutionIndex;
navigatedMoveIndex = originalNavigatedMoveIndex;
for (let i = 0; i < -navigatedMoveIndex; i++) {
assert(undo(), 'Could not undo move in exportSolutions, navigatedMoveIndex = ' + navigatedMoveIndex);
}
}
}
function copySolution(event) {
const clickedButtonId = event.target.id;
const idSplit = clickedButtonId.split("_");
const solutionIndex = parseInt(idSplit[1]);
const solution = solutions[solutionIndex];
let result = "";
solution.forEach(move => {
result += moveToString(move) + " ";
});
result = result.trim();
const originalNavigatedSolutionIndex = navigatedSolutionIndex;
const originalNavigatedMoveIndex = navigatedMoveIndex;
navigateToSolutionPosition(solutionIndex, solution.length);
const pgn = generatePGN(solution, board);
restoreOriginalPosition(originalNavigatedSolutionIndex, originalNavigatedMoveIndex);
populateAndShowCopyModal(result, pgn);
}
function disableSolutionButtons() {
const solutionButtons = document.getElementsByName("solution");
solutionButtons.forEach(solutionButton => {
solutionButton.disabled = true;
});
document.getElementById("exportSolutionsSpan").style.visibility = "hidden";
navigatedMoveIndex = null;
}
function createSolutionButton(solutionCount, moveCount, value) {
const startInput = document.createElement("input");
startInput.name = "solution";
startInput.id = solutionIdPrefix + "_" + solutionCount + "_" + moveCount;
startInput.type = "button";
startInput.value = value;
startInput.onclick = navigateToMove;
return startInput;
}
function showSolutions(maybeTruncated) {
navigatedMoveIndex = null;
const solutionDisplay = document.getElementById("solutionDisplay");
document.getElementById("exportSolutionsSpan").style.visibility = "visible";
solutionDisplay.innerHTML = "";
if (maybeTruncated) {
solutionDisplay.innerHTML = "<b>Maximum solution limit reached; not all solutions may have been found.</b><br>";
}
if (solutions != null && solutions.length > 0) {
const table = document.createElement("table");
let solutionCount = 0;
solutions.forEach(solution => {
const tr = document.createElement("tr");
let moveCount = 0;
const startTd = document.createElement("td");
const startInput = createSolutionButton(solutionCount, moveCount, "Start");
startTd.appendChild(startInput);
tr.appendChild(startTd);
moveCount++;
solution.forEach(move => {
const moveString = moveToString(move);
const moveTd = document.createElement("td");
const moveInput = createSolutionButton(solutionCount, moveCount, moveString);
moveTd.appendChild(moveInput);
tr.appendChild(moveTd);
moveCount++;
});
const copyTd = document.createElement("td");
const copyInput = document.createElement("input");
copyInput.name = "copy";
copyInput.id = copySolutionIdPrefix + "_" + solutionCount;
copyInput.type = "button";
copyInput.value = "Copy";
copyInput.onclick = copySolution;
copyTd.appendChild(copyInput);
tr.appendChild(copyTd);
table.appendChild(tr);
solutionCount++;
});
solutionDisplay.appendChild(table);
navigatedMoveIndex = 0;
}
}
function exportSolutions() {
if (solutions == null || solutions.length == 0) {
return null;
}
const originalNavigatedSolutionIndex = navigatedSolutionIndex;
const originalNavigatedMoveIndex = navigatedMoveIndex;
const exportedSolutions = [];
for (let i = 0; i < solutions.length; i++) {
const solution = solutions[i];
navigateToSolutionPosition(i, solution.length);
exportedSolutions.push(getForsythe(board));
}
restoreOriginalPosition(originalNavigatedSolutionIndex, originalNavigatedMoveIndex);
return exportedSolutions;
}
function exportSolutionsPGN() {
// Export all solutions in one PGN file
if (solutions == null || solutions.length == 0) {
return null;
}
const originalNavigatedSolutionIndex = navigatedSolutionIndex;
const originalNavigatedMoveIndex = navigatedMoveIndex;
const pgnArray = [];
for (let i = 0; i < solutions.length; i++) {
const solution = solutions[i];
navigateToSolutionPosition(i, solution.length); // now board has the ending position
const pgn = generatePGN(solution, board);
if (pgn != null) {
pgnArray.push(pgn);
}
}
restoreOriginalPosition(originalNavigatedSolutionIndex, originalNavigatedMoveIndex);
return pgnArray;
}