-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
117 lines (94 loc) · 1.89 KB
/
sketch.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
var rows, cols;
var w = 30;
var grid = [];
var current;
var stack = [];
var canvas;
var color;
var bigSize;
function setup() {
bigSize = true;
resetSketch();
var buttonSize = createButton("Change size");
buttonSize.parent('options');
var buttonColor = createButton("Change color");
buttonColor.parent('options');
var buttonReset = createButton("Reset");
buttonReset.parent('options');
buttonColor.mousePressed(changeColor);
buttonReset.mousePressed(resetSketch);
buttonSize.mousePressed(resize);
color = false;
}
function resize() {
bigSize = !bigSize;
resetSketch();
}
function changeColor() {
color = !color;
}
function resetSketch() {
if(bigSize)
canvas = createCanvas(1200, 600);
else {
canvas = createCanvas(300, 300);
}
canvas.parent('sketch');
cols = floor(width / w);
rows = floor(height / w);
//frameRate(20);
stack = [];
grid = [];
for(var j = 0; j < rows; j++) {
for (var i = 0; i < cols; i++) {
var cell = new Cell(i,j);
grid.push(cell);
}
}
current = grid[0];
}
function draw() {
background(44, 119, 152);
for(var i = 0; i < grid.length; i++) {
grid[i].show();
}
current.visited = true;
current.highlight();
// step 1
var next = current.checkNeighbors();
if (next) {
next.visited = true;
// step 2
stack.push(current);
// step 3
removeWalls(current, next);
// step 4
current = next;
} else if (stack.length > 0) {
current = stack.pop();
}
}
function removeWalls(a, b) {
var diffx = a.i - b.i;
if(diffx === 1) {
a.walls[3] = false;
b.walls[1] = false;
} else if (diffx === -1) {
a.walls[1] = false;
b.walls[3] = false;
}
var diffy = a.j - b.j;
if(diffy === 1) {
a.walls[0] = false;
b.walls[2] = false;
} else if (diffy === -1) {
a.walls[2] = false;
b.walls[0] = false;
}
}
function index(i, j) {
if(i < 0 || j < 0 || i > cols-1 || j > rows-1) {
return -1;
}
return i + j * cols;
}