-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.js
169 lines (153 loc) · 4.37 KB
/
maze.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
var Maze = window.Maze = function(width, height, el) {
if (!(this instanceof Maze)) {
return new Maze(options);
}
this.width = width;
this.height = height;
this.container = $(el);
this.initGrid();
};
Maze.N = 1;
Maze.S = 2;
Maze.E = 4;
Maze.W = 8;
Maze.Breadcrumb = 16;
Maze.Current = 32;
Maze.List = [1, 2, 4, 8];
Maze.dx = {
1: 0,
2: 0,
4: 1,
8: -1
};
Maze.dy = {
1: -1,
2: 1,
4: 0,
8: 0
};
Maze.opposite = {
1: 2,
2: 1,
4: 8,
8: 4
};
Maze.prototype.isOpenSide = function(cell, direction) {
return (cell & direction) === direction;
};
Maze.prototype.hasFlag = function(cell, flag) {
return (cell & flag) === flag;
};
Maze.prototype.initGrid = function() {
// Genererate maze data structure
var width = this.width,
height = this.height,
grid = this.grid = new Array(height);
for (var y = 0; y < height; y++) {
grid[y] = new Array(width);
for (var x = 0; x < width; x++) {
grid[y][x] = 0;
}
}
};
Maze.prototype.generate = function() {
console.log('Maze:generate:start');
this.carvePassageFrom(0, 0);
this.grid[0][0] |= Maze.W;
this.grid[this.height - 1][this.width - 1] |= Maze.E;
console.log('Maze:generate:end');
this.render();
};
Maze.prototype.carvePassageFrom = function(x, y) {
var directions = _.shuffle(Maze.List);
_.each(directions, function(direction) {
var nx = x + Maze.dx[direction],
ny = y + Maze.dy[direction];
if (ny >= 0 && ny < this.grid.length &&
nx >= 0 && nx < this.grid[ny].length &&
this.grid[ny][nx] === 0) {
this.grid[y][x] |= direction;
this.grid[ny][nx] |= Maze.opposite[direction];
this.carvePassageFrom(nx, ny);
}
}, this);
};
Maze.prototype.createCell = function(cell) {
var cellEl = $('<div>').addClass('cell');
if (this.isOpenSide(cell, Maze.N)) {
cellEl.addClass('n');
}
if (this.isOpenSide(cell, Maze.S)) {
cellEl.addClass('s');
}
if (this.isOpenSide(cell, Maze.E)) {
cellEl.addClass('e');
}
if (this.isOpenSide(cell, Maze.W)) {
cellEl.addClass('w');
}
if (this.hasFlag(cell, Maze.Breadcrumb)) {
cellEl.addClass('bc');
}
if (this.hasFlag(cell, Maze.Current)) {
cellEl.addClass('current');
}
return cellEl.get(0);
};
Maze.prototype.createRow = function(row) {
var rowEl = $('<div>').addClass('row');
var cells = _.map(row, this.createCell, this);
return rowEl.append(cells).get(0);
};
Maze.prototype.render = function() {
console.log('Maze:render');
var gridEl = $('<div>').attr('id', 'grid');
var elements = _.map(this.grid, this.createRow, this);
gridEl.append(elements).get(0);
this.container.html(gridEl);
};
Maze.prototype.solve = function() {
var x = 0,
y = 0,
solved = 0,
originDirection = Maze.W,
that = this;
this.grid[y][x] |= Maze.Breadcrumb;
var loop = function() {
var directions = that.directionToTry(originDirection);
for (var i = 0, l = directions.length; i < l; i++) {
var direction = directions[i];
var cell = that.grid[y][x];
if (that.isOpenSide(cell, direction)) {
// Remove current flag on ald cell
that.grid[y][x] &= ~Maze.Current;
x += Maze.dx[direction];
y += Maze.dy[direction];
originDirection = Maze.opposite[direction];
that.grid[y][x] |= Maze.Breadcrumb; // Mark as path
that.grid[y][x] |= Maze.Current; // Put current flag
that.render();
break; // Break direction iteration
}
}
if (!that.isFinish(x, y)) {
requestAnimFrame(loop);
}
};
loop();
};
Maze.prototype.directionToTry = function(lastOriginDirection) {
switch (lastOriginDirection) {
case Maze.N:
return [Maze.W, Maze.S, Maze.E, Maze.N];
case Maze.W:
return [Maze.S, Maze.E, Maze.N, Maze.W];
case Maze.S:
return [Maze.E, Maze.N, Maze.W, Maze.S];
case Maze.E:
return [Maze.N, Maze.W, Maze.S, Maze.E];
}
};
Maze.prototype.isFinish = function(x, y) {
return (y === (this.grid.length-1) && x === (this.grid[y].length-1));
};