Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
169 lines (154 loc) · 4.7 KB
/
Copy pathindex.html
File metadata and controls
169 lines (154 loc) · 4.7 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
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
canvas {
border: solid 10px red;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="850" height="850"></canvas>
<script src="rng.js"></script>
<script>
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');
var seed = 'sdfujhiksdrfuyjhiksdfguyjhiksdtfguyhikwerfuyhikserfuyjhiksedrtf';
// Seedable Random Number Generator
var rng = new RNG(seed);
// Width and Height of Maze
var width = 17;
var height = 17;
// Holder for Rows
var rows = [];
// Keeps track of movement of positions
var stack = [];
// Generates empty maze
for (var i = 0; i < height; i++) {
var row = [];
for (var j = 0; j < width; j++) {
row.push(0);
}
rows.push(row);
}
// Sets Current Position of maze generator
var currentPosition = new Position(1,1);
// Maze Logic
do {
var dirs = currentPosition.getPossibleDirections();
// Check for possible directions, if none are available back out of the maze
if (dirs.length == 0) {
stack.pop();
currentPosition.x = stack[stack.length - 1].x;
currentPosition.y = stack[stack.length - 1].y;
} else {
// Get list of directions and go a random available position
var direction = Math.floor(rng.uniform() * dirs.length);
var dirString = dirs[direction];
switch(dirString) {
case "up":
currentPosition.goUp();
break;
case "down":
currentPosition.goDown();
break;
case "left":
currentPosition.goLeft();
break;
case "right":
currentPosition.goRight();
break;
default:
break;
}
}
} while (stack.length > 1);
// Logs Maze for Debug purposes
//printRows(rows);
// Position Object
function Position (x, y) {
this.x = x;
this.y = y;
rows[y][x] = 1;
stack.push({x: this.x, y: this.y});
this.hasUp = function () {
return this.y + 2 < height;
};
this.hasDown = function () {
return this.y - 2 > -1;
};
this.hasLeft = function () {
return this.x - 2 > -1;
};
this.hasRight = function () {
return this.x + 2 < width;
};
this.getPossibleDirections = function() {
var directions = [];
if (this.hasUp() && rows[this.y + 2][this.x] == 0) {
directions.push("up");
}
if (this.hasDown() && rows[this.y - 2][this.x] == 0) {
directions.push("down");
}
if (this.hasLeft() && rows[this.y][this.x - 2] == 0) {
directions.push("left");
}
if (this.hasRight() && rows[this.y][this.x + 2] == 0) {
directions.push("right");
}
return directions;
};
this.goUp = function () {
rows[this.y + 1][this.x] = 1;
rows[this.y + 2][this.x] = 1;
this.y = this.y + 2;
stack.push({x: this.x, y: this.y});
};
this.goDown = function () {
rows[this.y - 1][this.x] = 1;
rows[this.y - 2][this.x] = 1;
this.y = this.y - 2;
stack.push({x: this.x, y: this.y});
};
this.goLeft = function () {
rows[this.y][this.x - 1] = 1;
rows[this.y][this.x - 2] = 1;
this.x = this.x - 2;
stack.push({x: this.x, y: this.y});
};
this.goRight = function () {
rows[this.y][this.x + 1] = 1;
rows[this.y][this.x + 2] = 1;
this.x = this.x + 2;
stack.push({x: this.x, y: this.y});
};
}
function printRows(array) {
var str = "";
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
str += array[i][j].toString();
}
str += "\n";
}
console.log(str);
}
// Draw logic
for (var i = 0; i < rows.length; i++) {
for (var j = 0; j < rows[i].length; j++) {
if (rows[i][j] == 0) {
context.beginPath();
context.rect(j*50, i*50, 50, 50);
context.fillStyle = 'black';
context.fill();
}
}
}
</script>
</body>
</html>