-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
482 lines (399 loc) · 16.4 KB
/
bundle.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
//main.js
//
//Copyright Lucas Manning
//
'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _mazeJs = __webpack_require__(1);
var _mazeJs2 = _interopRequireDefault(_mazeJs);
var _displayJs = __webpack_require__(3);
var _displayJs2 = _interopRequireDefault(_displayJs);
var canvas = document.getElementById('main');
var solve_button = document.getElementById('solve');
var gen_button = document.getElementById('generate');
var tile_size = 10;
var delay = 20;
var myDisplay = new _displayJs2['default'](tile_size, canvas);
var myMaze = new _mazeJs2['default'](tile_size, canvas.width, canvas.height, myDisplay);
myMaze.gen_random();
myDisplay.render(myMaze);
solve_button.addEventListener('click', function () {
console.log('solving...');
myMaze.a_star_search(delay);
});
gen_button.addEventListener('click', function () {
console.log('generating...');
myMaze.gen_random();
myDisplay.render(myMaze);
});
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
//maze.js
//
//Copyright Lucas Manning
//
//
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var _queueJs = __webpack_require__(2);
var _queueJs2 = _interopRequireDefault(_queueJs);
var Maze = (function () {
function Maze(tile_size, width, height, display) {
_classCallCheck(this, Maze);
this.tile_size = tile_size;
this.height = height;
this.width = width;
this.tiles = [];
this.start_tile = null;
this.end_tile = null;
this.searched = [];
this.display = display;
}
_createClass(Maze, [{
key: "reset",
value: function reset() {
this.tiles = [];
this.end_tile = null;
this.start_tile = null;
this.searched = [];
}
}, {
key: "gen_random",
// generate random maze
value: function gen_random() {
this.reset();
for (var y = 0; y < this.height / this.tile_size; y++) {
for (var x = 0; x < this.width / this.tile_size; x++) {
var wall = Math.random() <= 0.25;
var new_tile = {
x: x,
y: y,
type: 0,
parent: null,
dist_cost: null
};
if (wall) {
new_tile.type = 1;
}
this.tiles.push(new_tile);
}
}
this.start_tile = this.random_tile();
this.end_tile = this.random_tile();
while (this.manhattan_distance(this.start_tile) === 0) {
this.end_tile = this.random_tile();
}
this.start_tile.type = 2;
this.start_tile.dist_cost = 0;
this.end_tile.type = 3;
}
}, {
key: "random_tile",
value: function random_tile() {
return this.tiles[Math.floor(Math.random() * this.width * this.height / Math.pow(this.tile_size, 2))];
}
}, {
key: "generateRecursiveBacktrack",
//generate recursive backtrack maze
value: function generateRecursiveBacktrack() {}
}, {
key: "tile_in_bounds",
//this is a formal apology for the following method. I'm sorry.
value: function tile_in_bounds(og_tile, tile) {
return tile != undefined && (Math.abs(og_tile.x - tile.x) == 1 && Math.abs(og_tile.y - tile.y) == 0 || Math.abs(og_tile.x - tile.x) == 0 && Math.abs(og_tile.y - tile.y) == 1);
}
}, {
key: "tile_is_empty",
value: function tile_is_empty(tile) {
return tile.type !== 1;
}
}, {
key: "neighbors",
value: function neighbors(tile) {
var index = this.tile_index(tile);
var og_tile = this.tiles[index];
var indicies = [index + 1, index - 1, index + 50, index - 50];
var tile_neighbors = [];
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = indicies[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _index = _step.value;
var _tile = this.tiles[_index];
if (this.tile_in_bounds(og_tile, _tile) && this.tile_is_empty(_tile)) {
tile_neighbors.push(_tile);
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator["return"]) {
_iterator["return"]();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return tile_neighbors;
}
}, {
key: "manhattan_distance",
value: function manhattan_distance(tile) {
var dx = Math.abs(tile.x - this.end_tile.x);
var dy = Math.abs(tile.y - this.end_tile.y);
return 1 * (dx + dy);
}
}, {
key: "tile_index",
value: function tile_index(tile) {
return tile.x + tile.y * 50;
}
}, {
key: "a_star_search",
value: function a_star_search() {
var _this = this;
var delay = arguments[0] === undefined ? 20 : arguments[0];
var frontier = new _queueJs2["default"]();
frontier.add(0, this.start_tile);
this.searched.push(this.start_tile);
var search_loop = setInterval(function () {
if (frontier.empty()) {
clearInterval(search_loop);
}
var current = frontier.get();
//manahttan dist of zero means the current tile is the end_tile
if (_this.manhattan_distance(current) === 0) {
console.log("goal reached");
_this.trace_path();
//break
clearInterval(search_loop);
}
var _iteratorNormalCompletion2 = true;
var _didIteratorError2 = false;
var _iteratorError2 = undefined;
try {
for (var _iterator2 = _this.neighbors(current)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
var tile = _step2.value;
var cost = current.dist_cost + 1;
var searched_index = _this.searched.indexOf(tile);
if (searched_index === -1 || cost < _this.searched[searched_index].dist_cost) {
tile.dist_cost = cost;
var priority = cost + _this.manhattan_distance(tile);
tile.parent = _this.tile_index(current);
if (tile !== _this.end_tile) {
tile.type = 4;
}
_this.searched.push(tile);
frontier.add(priority, tile);
}
}
} catch (err) {
_didIteratorError2 = true;
_iteratorError2 = err;
} finally {
try {
if (!_iteratorNormalCompletion2 && _iterator2["return"]) {
_iterator2["return"]();
}
} finally {
if (_didIteratorError2) {
throw _iteratorError2;
}
}
}
_this.display.render(_this);
}, delay);
}
}, {
key: "trace_path",
value: function trace_path() {
var tile = this.tiles[this.end_tile.parent];
while (tile.parent != null) {
if (tile !== this.start_tile) {
tile.type = 5;
}
tile = this.tiles[tile.parent];
}
}
}]);
return Maze;
})();
exports["default"] = Maze;
module.exports = exports["default"];
/***/ },
/* 2 */
/***/ function(module, exports) {
//queue.js
//
//Copyright Lucas Manning
//
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Queue = (function () {
function Queue() {
_classCallCheck(this, Queue);
this.items = [];
}
_createClass(Queue, [{
key: "add",
value: function add(priority, element) {
if (!this.empty()) {
var index = 0;
while (index < this.items.length && priority > this.items[index][0]) {
index++;
}
var item = [priority, element];
this.items.splice(index, 0, item);
} else {
this.items.push([priority, element]);
}
}
}, {
key: "empty",
value: function empty() {
return this.items.length === 0;
}
}, {
key: "get",
value: function get() {
return this.items.shift()[1];
}
}]);
return Queue;
})();
exports["default"] = Queue;
module.exports = exports["default"];
/***/ },
/* 3 */
/***/ function(module, exports) {
//display.js
//
//Copyright Lucas Manning
//
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
var Display = (function () {
function Display(tile_size, canvas) {
_classCallCheck(this, Display);
this.tile_size = tile_size;
this.canvas = canvas;
this.ctx = this.canvas.getContext('2d');
this.ctx.fillStyle = '#000000';
}
_createClass(Display, [{
key: 'render',
value: function render(maze) {
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = maze.tiles[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var tile = _step.value;
switch (tile.type) {
case 0:
this.ctx.fillStyle = '#d3d3d3';
this.draw_tile(tile);
break;
case 1:
this.ctx.fillStyle = '#000000';
this.draw_tile(tile);
break;
case 2:
this.ctx.fillStyle = '#00FF00';
this.draw_tile(tile);
break;
case 3:
this.ctx.fillStyle = '#FF0000';
this.draw_tile(tile);
break;
case 4:
this.ctx.fillStyle = '#0000FF';
this.draw_tile(tile);
break;
case 5:
this.ctx.fillStyle = '#FFA500';
this.draw_tile(tile);
break;
}
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator['return']) {
_iterator['return']();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
}, {
key: 'draw_tile',
value: function draw_tile(tile) {
this.ctx.fillRect(tile.x * 10, tile.y * 10, 10, 10);
}
}]);
return Display;
})();
exports['default'] = Display;
module.exports = exports['default'];
/***/ }
/******/ ]);