-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaze.html
More file actions
410 lines (362 loc) · 10.9 KB
/
maze.html
File metadata and controls
410 lines (362 loc) · 10.9 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<!DOCTYPE html>
<html>
<head>
<title>Maze</title>
<meta charset="UTF-8">
<style>
body { margin: 0px; background-color: #000; }
#container { position: relative; width: 0; height: 0; }
.cell { position: absolute; background-color: #000; font-size: 8px; text-align: center; color: #00f; }
.wall { position: absolute; background-color: #000; }
.empty { background-color: #000; }
.start { background-color: #33f!important; color: #fff; }
.end { background-color: #f33!important; }
.forward { background-color: #ccf!important; }
.backward { background-color: #fcc!important; }
.white { background-color: #fff; z-index: -1; }
.yellow { background-color: #ee3; }
</style>
</head>
<body>
<div id="container"></div>
<script>
var _speed = 10; // Animation speed.
var _show_solution = false; // Show the solution at the beginning.
var _show_solution_steps = true; // Solution animation details.
var _cell_size = 20;
var _wall_size = 3;
var _board_col = 100, _board_row = 50;
var _sx = 0, _sy = 0;
var _ex = 0, _ey = 0;
var _count = 0;
var _board;
var _container;
var _solution;
var _solution_steps;
var _maze_steps;
var queue = null;
window.onload = initGame();
function initGame()
{
initBoard();
if (_count++ % 2 == 0)
findSolution(_solution_steps, _solution, _board, _sx, _sy, 0);
else
findSolution2(_solution_steps, _solution, _board, _sx, _sy, 0);
showSteps(_maze_steps, 0);
}
function initBoard()
{
_board_col = Math.min(Math.floor(window.innerWidth / (_cell_size + _wall_size)), 100);
_board_row = Math.min(Math.floor(window.innerHeight / (_cell_size + _wall_size)), 100);
_sx = Math.floor(Math.random() * _board_col);
_sy = Math.floor(Math.random() * _board_row);
_ex = Math.floor(Math.random() * _board_col);
_ey = Math.floor(Math.random() * _board_row);
_container = document.getElementById('container');
_container.innerHTML = '';
_container.style['left'] = (window.innerWidth - _board_col * (_cell_size + _wall_size)) / 2 + 'px';
_container.style['top'] = (window.innerHeight - _board_row * (_cell_size + _wall_size)) / 2 + 'px';
_maze_steps = new Array();
_board = new Array();
_solution_steps = new Array();
_solution = new Array();
for (var i = 0; i < _board_row; i++)
{
_board.push(new Array());
for (var j = 0; j < _board_col; j++)
{
_board[i].push([0, 0, 0, 0]);
}
}
createBoard(_container, _board, _cell_size, _wall_size);
setEmptyCell(_maze_steps, _board, 0, 0, 0);
}
function setWall(wall, type, i, j, cell_size, wall_size)
{
wall.setAttribute('id', 'wall_' + type + '_' + j + '_' + i);
wall.setAttribute('x', j);
wall.setAttribute('y', i);
wall.setAttribute('class', 'wall');
if (type == 'x')
{
wall.style['width'] = wall_size + 'px';
wall.style['height'] = cell_size + wall_size * 2 + 'px';
}
else
{
wall.style['width'] = cell_size + wall_size * 2 + 'px';
wall.style['height'] = wall_size + 'px';
}
wall.style['left'] = cell_size * j + wall_size * (j - 1) + 'px';
wall.style['top'] = cell_size * i + wall_size * (i - 1) + 'px';
}
function createBoard(container, board, cell_size, wall_size)
{
for (var i = 0; i < board.length; i++)
{
for (var j = 0; j < board[0].length; j++)
{
var cell = document.createElement('div');
cell.setAttribute('id', 'cell_' + j + '_' + i);
cell.setAttribute('x', j);
cell.setAttribute('y', i);
cell.setAttribute('class', 'cell');
cell.style['width'] = cell_size + 'px';
cell.style['height'] = cell_size + 'px';
cell.style['left'] = cell_size * j + wall_size * j + 'px';
cell.style['top'] = cell_size * i + wall_size * i + 'px';
cell.style['line-height'] = cell_size + 'px';
container.appendChild(cell);
if (i == _sy && j == _sx)
cell.classList.add('start');
if (i == _ey && j == _ex)
cell.classList.add('end');
}
}
for (var i = 0; i < board.length; i++)
{
for (var j = 0; j < board[0].length + 1; j++)
{
var wall = document.createElement('div');
setWall(wall, 'x', i, j, cell_size, wall_size)
container.appendChild(wall);
}
}
for (var i = 0; i < board.length + 1; i++)
{
for (var j = 0; j < board[0].length; j++)
{
var wall = document.createElement('div');
setWall(wall, 'y', i, j, cell_size, wall_size);
container.appendChild(wall);
}
}
}
function setEmptyCell(maze_steps, board, x, y, depth)
{
var cell = document.getElementById('cell_' + x + '_' + y);
if (cell == null || cell.classList.contains('empty')) return false;
maze_steps.push(cell);
cell.classList.add('empty');
var offsets = [[0, -1], [0, 1], [-1, 0], [1, 0]];
for (var i = 0; i < offsets.length; i++)
{
var rand = Math.floor(Math.random() * offsets.length);
var temp = offsets[i];
offsets[i] = offsets[rand];
offsets[rand] = temp;
}
for (var i = 0; i < offsets.length; i++)
{
var new_x = x + offsets[i][0];
var new_y = y + offsets[i][1];
if (new_x < 0 || new_y < 0 || new_x >= board[0].length || new_y >= board.length) continue;
if (document.getElementById('cell_' + new_x + '_' + new_y).classList.contains('empty')) continue;
var wall;
if (offsets[i][0] == 0)
{
var temp = (offsets[i][1] + 1) / 2;
wall = document.getElementById('wall_y_' + x + '_' + (y + offsets[i][1] + 1 - temp));
board[y][x][temp] = 1;
board[new_y][new_x][Math.abs(temp - 1)] = 1;
}
else if (offsets[i][1] == 0)
{
var temp = (offsets[i][0] + 1) / 2;
wall = document.getElementById('wall_x_' + (x + offsets[i][0] + 1 - temp) + '_' + y);
board[y][x][temp + 2] = 1;
board[new_y][new_x][Math.abs(temp - 1) + 2] = 1;
}
maze_steps.push(wall);
setEmptyCell(maze_steps, board, new_x, new_y, depth + 1);
}
return true;
}
function findSolution(solution_steps, solution, board, x, y, depth)
{
var cell = document.getElementById('cell_' + x + '_' + y);
if (cell == null || !cell.classList.contains('empty')) return false;
if (cell.classList.contains('found')) return false;
if (cell.classList.contains('end')) return true;
cell.classList.add('found');
var offsets = new Array();
if (board[y][x][3] == 1) offsets.push([1, 0]);
if (board[y][x][1] == 1) offsets.push([0, 1]);
if (board[y][x][0] == 1) offsets.push([0, -1]);
if (board[y][x][2] == 1) offsets.push([-1, 0]);
for (var i = 0; i < offsets.length; i++)
{
var rand = Math.floor(Math.random() * offsets.length);
var temp = offsets[i];
offsets[i] = offsets[rand];
offsets[rand] = temp;
}
for (var i = 0; i < offsets.length; i++)
{
if (_show_solution) cell.innerHTML = depth % 1000;
solution_steps.push([x, y, 1, offsets[i]]);
solution.push([x, y, 1, offsets[i]]);
var new_x = x + offsets[i][0];
var new_y = y + offsets[i][1];
var flag = findSolution(solution_steps, solution, board, new_x, new_y, depth + 1);
if (flag) return flag;
solution.pop();
}
cell.innerHTML = '';
solution_steps.push([x, y, 0, [0, 0], 0]);
return false;
}
function findSolution2(solution_steps, solution, board, x, y, depth)
{
if (depth == 0)
{
queue = new Array();
var cell = document.getElementById('cell_' + x + '_' + y);
queue.push(cell);
solution_steps.push([x, y, 1, [0, 0], -1, -1, 0]);
solution.push([x, y, 1, [0, 0], -1, -1, 0]);
}
while (queue.length > 0)
{
var cell = queue.shift();
x = parseInt(cell.getAttribute('x'));
y = parseInt(cell.getAttribute('y'));
var offsets = new Array();
if (board[y][x][3] == 1) offsets.push([1, 0]);
if (board[y][x][1] == 1) offsets.push([0, 1]);
if (board[y][x][0] == 1) offsets.push([0, -1]);
if (board[y][x][2] == 1) offsets.push([-1, 0]);
for (var i = 0; i < offsets.length; i++)
{
var rand = Math.floor(Math.random() * offsets.length);
var temp = offsets[i];
offsets[i] = offsets[rand];
offsets[rand] = temp;
}
for (var i = 0; i < offsets.length; i++)
{
var new_x = x + offsets[i][0];
var new_y = y + offsets[i][1];
var cell = document.getElementById('cell_' + new_x + '_' + new_y);
if (cell.classList.contains('found')) continue;
if (cell.classList.contains('start')) continue;
if (cell.classList.contains('end'))
{
while (queue.length > 0) queue.pop();
var temp_x = x;
var temp_y = y;
while (temp_x >= 0 && temp_y >= 0)
{
for (var j = 0; j < solution.length; j++)
{
if (solution[j][0] == temp_x && solution[j][1] == temp_y)
{
temp_i = j;
break;
}
}
temp_x = solution[temp_i][4];
temp_y = solution[temp_i][5];
solution[temp_i][6] = 1;
solution_steps[temp_i][6] = 1;
}
break;
}
cell.classList.add('found');
queue.push(cell);
solution_steps.push([new_x, new_y, 1, offsets[i], x, y, 0]);
solution.push([new_x, new_y, 1, offsets[i], x, y, 0]);
}
}
}
function showSolution(solution, index)
{
for (var i = 0; i < _speed; i++)
{
var x = solution[index][0];
var y = solution[index][1];
var cell = document.getElementById('cell_' + x + '_' + y);
if (cell == null) break;
if (solution[index][2] == 1)
{
if (index > 0 && (x != _sx || y != _sy))
cell.classList.add('forward');
if (solution[index][3][0] == 0)
{
if (solution[index][3][1] == 1)
cell.innerHTML = '🡣';
else if (solution[index][3][1] == -1)
cell.innerHTML = '🡡';
}
else if (solution[index][3][1] == 0)
{
if (solution[index][3][0] == 1)
cell.innerHTML = '🡢';
else if (solution[index][3][0] == -1)
cell.innerHTML = '🡠';
}
}
else
{
if (index > 0 && (x != _sx || y != _sy))
cell.classList.add('backward');
cell.innerHTML = '';
}
index++;
if (index >= solution.length)
{
for (var j = 0; j < solution.length; j++)
{
x = solution[j][0];
y = solution[j][1];
cell = document.getElementById('cell_' + x + '_' + y);
if (cell == null) break;
if (solution[0].length >= 6)
{
if (solution[j][6] == 1)
{
cell.classList.remove('forward');
cell.classList.add('yellow');
}
}
else
{
cell.classList.remove('forward');
cell.classList.add('yellow');
}
}
setTimeout('initGame()', 8000);
return;
}
}
setTimeout(function () { showSolution(solution, index); }, 50);
}
function showSteps(maze_steps, index)
{
for (var i = 0; i < _speed * 10; i++)
{
var obj = maze_steps[index];
if (obj == null) break;
if (obj.classList.contains('empty'))
obj.classList.add('white');
else
obj.classList.add('white');
index++;
if (index >= maze_steps.length)
{
//*
if (_show_solution_steps)
showSolution(_solution_steps, 0);
else
showSolution(_solution, 0);
//*/
console.log(_solution_steps);
return;
}
}
setTimeout(function () { showSteps(maze_steps, index); }, 4);
}
</script>
</body>
</html>