Repository navigation
Expand file tree
/
Copy pathmazeGenerator.js
More file actions
130 lines (104 loc) · 3.85 KB
/
Copy pathmazeGenerator.js
File metadata and controls
130 lines (104 loc) · 3.85 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
(function(global) {
"use strict";
var jsmaze = global.jsmaze;
if (!jsmaze) {
jsmaze = {};
global.jsmaze = jsmaze;
}
jsmaze.generateMaze = function(maze) {
initializeMaze(maze);
fillMaze(maze, Math.floor(maze.width/2), Math.floor(maze.height/2));
markEntranceAndExit(maze);
return maze;
}
function initializeMaze(maze) {
for (var column = 0; column < maze.width; column++) {
maze.cells[column] = [];
for (var row = 0; row < maze.height; row++) {
var state = ((column == 0 || column == maze.width - 1 || row == 0 || row == maze.height - 1) ? maze.BOARDER : maze.WALL);
maze.cells[column][row] = state;
}
}
maze.cells[1][0] = maze.WALL;
maze.cells[maze.width - 2][maze.height - 1] = maze.WALL;
}
function fillMaze(maze, x, y) {
var pos = createPos(x, y);
var pathStack = [];
pathStack.push(pos);
maze.cells[pos.x][pos.y] = maze.PATH;
while (pathStack.length != 0) {
if (replaceBlock(shuffle(directionOperations), maze, pos, maze.WALL, maze.PATH)) {
pathStack.push(createPos(pos.x, pos.y));
} else {
pos = pathStack.pop();
}
}
}
function markEntranceAndExit(maze) {
maze.cells[1][0] = maze.ENTRANCE;
maze.cells[maze.width - 2][maze.height - 1] = maze.EXIT;
}
function createPos(x, y) {
return {
'x': x,
'y': y
};
}
function replaceBlock(operations, maze, pos, match, replace) {
for (var operationPos = 0; operationPos < operations.length; operationPos++) {
if (operations[operationPos](maze, pos, match, replace)) {
return true;
}
}
return false;
}
var directionOperations = [
function(maze, pos, match, replace) {
if (pos.x + 2 < maze.width && maze.cells[pos.x + 1][pos.y] == match && maze.cells[pos.x + 2][pos.y] == match) {
maze.cells[pos.x + 1][pos.y] = replace;
maze.cells[pos.x + 2][pos.y] = replace;
pos.x += 2;
return true;
}
},
function(maze, pos, match, replace) {
if (pos.x - 2 >= 0 && maze.cells[pos.x - 1][pos.y] == match && maze.cells[pos.x - 2][pos.y] == match) {
maze.cells[pos.x - 1][pos.y] = replace;
maze.cells[pos.x - 2][pos.y] = replace;
pos.x -= 2;
return true;
}
},
function(maze, pos, match, replace) {
if (pos.y + 2 < maze.height && maze.cells[pos.x][pos.y + 1] == match && maze.cells[pos.x][pos.y + 2] == match) {
maze.cells[pos.x][pos.y + 1] = replace;
maze.cells[pos.x][pos.y + 2] = replace;
pos.y += 2;
return true;
}
},
function(maze, pos, match, replace) {
if (pos.y - 2 >= 0 && maze.cells[pos.x][pos.y - 1] == match && maze.cells[pos.x][pos.y - 2] == match) {
maze.cells[pos.x][pos.y - 1] = replace;
maze.cells[pos.x][pos.y - 2] = replace;
pos.y -= 2;
return true;
}
}
];
function shuffle(array) {
if (Math.floor(Math.random() * 100) > 70) {
var currentIndex = array.length,
temporaryValue, randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
}
return array;
}
})(this);