-
Notifications
You must be signed in to change notification settings - Fork 3
/
ui.js
382 lines (321 loc) · 10.9 KB
/
ui.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
'use strict';
//////////////////////////////////////////////////////////////////////
// Text/board conversion. (not really UI-related, though)
// Receives an array of strings, where each string is one line.
// Returns a board.
function parse_text_input(lines) {
var board = new Board();
var terminator_count = {};
for (var i = 0; i < lines.length; i++) {
board.nodes[i] = [];
for (var j = 0; j < lines[i].length; j++) {
var c = lines[i][j];
if (c == ' ' || c == '0') {
board.nodes[i][j] = null;
} else if (c >= '1' && c <= '4') {
var num = parseInt(c, 10);
board.nodes[i][j] = new Node(null, 2 * num);
} else if ('tsdph'.indexOf(c) > -1) {
board.nodes[i][j] = new Node(c, 2);
} else if ('TSDPH'.indexOf(c) > -1) {
board.nodes[i][j] = new Node(c.toLowerCase(), 1);
board.terminators.push({'x': j, 'y': i});
terminator_count[c] = (terminator_count[c] || 0) + 1;
} else {
board.errors.push('Invalid character: ' + c);
}
}
if (j > board.width){
board.width = j;
}
}
board.height = i;
if (board.width < 1 || board.height < 1) {
board.errors.push('Empty board.');
}
if (terminator_count.length === 0) {
board.errors.push('No terminator node was found.');
}
for (var i in terminator_count) {
if (terminator_count[i] != 2) {
board.errors.push('There are ' + terminator_count[i] + ' terminators of type ' + i + ', but only 2 are expected.');
}
}
return board;
}
// Receives an already solved board.
// Returns a multi-line string with the solution in ASCII drawing.
function solution_to_text(board) {
var sol = [];
// Nodes.
for (var i = 0; i < board.height; i++) {
var line1 = [];
var line2 = [];
for (var j = 0; j < board.width; j++) {
var name = ' ';
var node = board.nodes[i][j];
if (node) {
if (node.max_edges === 1) {
name = node.color.toUpperCase();
} else {
name = node.color || '*';
}
}
line1.push(name, ' ');
line2.push(' ', ' ');
}
sol.push(line1, line2);
}
// Edges.
for (var i = 0; i < board.height; i++) {
for (var j = 0; j < board.width; j++) {
var node = board.nodes[i][j];
if (node) {
for (var k = 0; k < node.edges.length; k++) {
var edge = node.edges[k];
var x = j * 2 + edge.direction.dx;
var y = i * 2 + edge.direction.dy;
var c = edge.direction.char;
sol[y][x] = c;
// Edge color is ignored here.
}
}
}
}
var ret = '';
for (var i = 0; i < sol.length; i++) {
ret += sol[i].join('') + '\n';
}
return ret;
}
//////////////////////////////////////////////////////////////////////
// DOM-related and UI-related manipulation.
function parse_board_from_input() {
var puzzleinput = document.getElementById('puzzleinput');
var lines = puzzleinput.value.split(/[\r\n]+/);
var board = parse_text_input(lines);
var messages = document.getElementById('messages');
messages.textContent = board.errors.join('\n');
messages.classList.remove('busy');
return board;
}
function build_svg_from_board(board) {
if (board.width < 1 || board.height < 1) {
return;
}
var svgsolutioncontainer = document.getElementById('svgsolutioncontainer');
var svg_code = '';
var width = (board.width * 1.5 - 0.5);
var height = (board.height * 1.5 - 0.5);
svg_code += '<svg preserveAspectRatio="xMidYMin" viewBox="0 0 ' + width + ' ' + height + '">';
var edge_colors = {
's': '#fc1f20',
'd': '#0086ff',
't': '#ffa515',
'p': '#009e00',
'h': '#ca197d'
};
var revealrange = document.getElementById('revealrange');
if (board.edge_count === 0) {
revealrange.disabled = true;
} else {
revealrange.disabled = false;
// Updating the slider to the new edge count.
var is_reveal_slider_at_max = (revealrange.value === revealrange.max);
revealrange.max = board.edge_count;
if (is_reveal_slider_at_max) {
// If value was already at maximum before, the user likely wants
// the value at maximum again.
revealrange.value = revealrange.max;
}
// Edge indexes being shuffled in a random order.
var random_numbers = [];
for (var i = 0; i < board.edge_count; i++) {
random_numbers[i] = i;
}
shuffle(random_numbers);
// Building the edges.
for (var i = 0; i < board.height; i++) {
for (var j = 0; j < board.width; j++) {
var node = board.nodes[i][j];
if (node) {
for (var k = 0; k < node.edges.length; k++) {
var edge = node.edges[k];
svg_code += '<polyline ' +
'class="edge" '+
'id="edge' + random_numbers.pop() + '" ' +
'stroke="' + edge_colors[edge.color] + '" ' +
'marker-mid="url(#mid_arrow)"' +
'points="' +
'' + ((j ) * 1.5 + 0.5) + ',' + ((i ) * 1.5 + 0.5) + ' ' +
'' + ((j + edge.direction.dx / 2) * 1.5 + 0.5) + ',' + ((i + edge.direction.dy / 2) * 1.5 + 0.5) + ' ' +
'' + ((j + edge.direction.dx ) * 1.5 + 0.5) + ',' + ((i + edge.direction.dy ) * 1.5 + 0.5) + '" />';
}
}
}
}
}
// Building the nodes.
for (var i = 0; i < board.height; i++) {
for (var j = 0; j < board.width; j++) {
var node = board.nodes[i][j];
if (node) {
var name = '';
if (node.max_edges === 1) {
name = node.color.toUpperCase();
} else if (node.color) {
name = node.color;
} else {
name = Math.floor(node.max_edges / 2) + '';
}
svg_code += '<use xlink:href="#node_' + name + '" x="' + (j * 1.5) + '" y="' + (i * 1.5) + '" width="1" height="1" />';
}
}
}
svg_code += '</svg>';
svgsolutioncontainer.innerHTML = svg_code;
reveal_edges(revealrange.value);
}
function reveal_edges(how_many) {
var all_edges = document.querySelectorAll('#svgsolutioncontainer > svg .edge');
for (var i = 0; i < all_edges.length; i++) {
all_edges[i].style.display = 'none';
}
for (var i = 0; i < how_many; i++) {
var edge = document.getElementById('edge' + i);
if (edge) {
edge.style.display = 'block';
}
}
}
//////////////////////////////////////////////////////////////////////
// Web Worker handling.
function interrupt_worker() {
if (g_worker) {
g_worker.terminate();
g_worker = null;
var solvebutton = document.getElementById('solvebutton');
solvebutton.value = 'Solve it!';
var messages = document.getElementById('messages');
messages.classList.remove('busy');
}
}
function solution_found_handler(e) {
var solve_finish_date = new Date();
var solve_time = solve_finish_date.getTime() - g_solve_start_date.getTime();
solve_time = solve_time / 1000; // Converting to seconds.
interrupt_worker();
var messages = document.getElementById('messages');
var board = e.data;
if (board.solution_found === true) {
messages.textContent = 'Solution found after ' + solve_time.toFixed(1) + 's!';
} else if (board.solution_found === false) {
messages.textContent = 'No solution found after ' + solve_time.toFixed(1)+ 's. :(';
} else {
console.error('board.solution_found: ' + board.solution_found);
messages.textContent = 'This should not have happened.';
}
messages.classList.remove('busy');
//console.log(solution_to_text(board));
build_svg_from_board(board);
}
function start_background_solver(board) {
g_solve_start_date = new Date();
// Run the solving algorithm in a background thread.
g_worker = new Worker('algorithm.js');
g_worker.addEventListener('message', solution_found_handler);
g_worker.postMessage(board);
var solvebutton = document.getElementById('solvebutton');
solvebutton.value = 'Abort!';
messages.textContent = 'Searching for a solution…';
messages.classList.add('busy');
}
//////////////////////////////////////////////////////////////////////
// UI event handling.
function puzzleinput_input_handler() {
interrupt_worker();
var board = parse_board_from_input();
build_svg_from_board(board);
if (board.errors.length === 0) {
var autosolvecheckbox = document.getElementById('autosolvecheckbox');
if (autosolvecheckbox.checked) {
start_background_solver(board);
}
}
}
function solvebutton_click_handler() {
var messages = document.getElementById('messages');
if (g_worker) {
interrupt_worker();
messages.textContent = 'Interrupted by user.';
} else {
var board = parse_board_from_input();
if (board.errors.length === 0) {
start_background_solver(board);
}
}
}
function revealrange_input_handler() {
var revealrange = document.getElementById('revealrange');
reveal_edges(revealrange.value);
}
function showarrowscheckbox_click_handler() {
var showarrowscheckbox = document.getElementById('showarrowscheckbox');
var svgsolutioncontainer = document.getElementById('svgsolutioncontainer');
// Damn IE… Does not support .toggle() second argument.
//svgsolutioncontainer.classList.toggle('hidearrows', !showarrowscheckbox.checked);
if (showarrowscheckbox.checked) {
svgsolutioncontainer.classList.remove('hidearrows');
} else {
svgsolutioncontainer.classList.add('hidearrows');
}
}
function autosolvecheckbox_click_handler(e) {
var autosolvecheckbox = document.getElementById('autosolvecheckbox');
if (autosolvecheckbox.checked) {
if (!g_worker) {
solvebutton_click_handler();
}
}
}
function init() {
var puzzleinput = document.getElementById('puzzleinput');
puzzleinput.addEventListener('input', puzzleinput_input_handler);
var solvebutton = document.getElementById('solvebutton');
solvebutton.addEventListener('click', solvebutton_click_handler);
var revealrange = document.getElementById('revealrange');
revealrange.addEventListener('input', revealrange_input_handler);
// Workaround for IE 11.
// https://msdn.microsoft.com/en-us/library/gg592978(v=vs.85).aspx
// https://connect.microsoft.com/IE/Feedback/Details/856998
// I'm not even writing workarounds for earlier IE versions, as they will
// probably be utterly broken beyond all hope.
if (navigator.userAgent.search(/Trident\/7/) > -1) {
// The "onchange" event should only happen after the input element
// loses focus. However, in IE it happens immediately, behaving like
// "oninput".
revealrange.addEventListener('change', revealrange_input_handler);
}
var showarrowscheckbox = document.getElementById('showarrowscheckbox');
showarrowscheckbox.addEventListener('click', showarrowscheckbox_click_handler);
// IE11 can't render the arrows.
if (navigator.userAgent.search(/Trident\/7/) > -1) {
showarrowscheckbox.checked = false;
showarrowscheckbox.disabled = true;
showarrowscheckbox.parentNode.title = 'Not working in Microsoft Internet Explorer. Please consider using a modern browser.';
showarrowscheckbox.parentNode.style.textDecoration = 'line-through';
}
var autosolvecheckbox = document.getElementById('autosolvecheckbox');
autosolvecheckbox.addEventListener('click', autosolvecheckbox_click_handler);
puzzleinput_input_handler();
showarrowscheckbox_click_handler();
// Preload this file.
var tmp_worker = new Worker('algorithm.js');
tmp_worker.terminate();
}
// Global var pointing to the background thread.
var g_worker = null;
// Date object to measure the algorithm time.
var g_solve_start_date = null;
// This script should be included with "defer" attribute.
init();