-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.js
182 lines (166 loc) · 6.03 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
170
171
172
173
174
175
176
177
178
179
180
181
182
// Set up the canvas
const maze = document.querySelector('#myCanvas')
let ctx = maze.getContext("2d");
ctx.strokeStyle = "#000000";
const boarderSize = 3
class Maze {
constructor(sizeInPixels, numCells) {
this.size = sizeInPixels
this.numCells = numCells;
this.grid = []; //backend info on each cell in the grid; stores a cell
}
// Set up grid for our backend use
setUp() {
for (let row = 0; row < this.numCells; row++) {
let cellArray = [];
for (let column = 0; column < this.numCells; column ++) {
let cell = new Cell(row, column, this.numCells, this.size)
cellArray.push(cell)
}
this.grid.push(cellArray)
}
}
// Draw the cells onto the page
draw() {
maze.width = this.size
maze.height = this.size
for (let row = 0; row < this.numCells; row++) {
for (let column = 0; column < this.numCells; column ++) {
this.grid[row][column].show()
}
}
// draw where four cells meet so there's no weird cut
for (let row = 0; row < this.numCells; row++) {
for (let column = 0; column < this.numCells; column ++) {
ctx.fillStyle = `rgb(0, 0, 0)`;
let scale = this.size / this.numCells
ctx.fillRect(row * scale - boarderSize / 2, column * scale - boarderSize / 2, boarderSize, boarderSize)
}
}
}
reset () {
for (let row = 0; row < this.numCells; row++) {
for (let column = 0; column < this.numCells; column ++) {
let currentCell = this.grid[row][column]
currentCell.walls.topWall = true
currentCell.walls.rightWall = true
currentCell.walls.bottomWall = true
currentCell.walls.leftWall = true
currentCell.visited = false
currentCell.visitedByMazeGenerator = false
currentCell.distanceFromStart = 0
currentCell.visitedByDistanceAlgo = false
currentCell.inPath = false
}
}
}
// apparently on most browsers you can just redraw the whole thing
// revisit this optimization if it fails later on
removeWalls(cellOne, cellTwo) {
const xDiff = cellOne.column - cellTwo.column
if (xDiff === 1) {
cellOne.walls.leftWall = false;
cellTwo.walls.rightWall = false;
} else if (xDiff === -1) {
cellOne.walls.rightWall = false;
cellTwo.walls.leftWall = false
}
const yDiff = cellOne.row - cellTwo.row
if (yDiff === 1) {
cellOne.walls.topWall = false
cellTwo.walls.bottomWall = false
} else if (yDiff === -1) {
cellOne.walls.bottomWall = false
cellTwo.walls.topWall = false
}
}
}
// issue with walls looking too thin. Maybe Can have double walls initially?
// i.e. when draw draw it twice
class Cell {
constructor(row, column, numCells, mazeSize) {
this.row = row
this.column = column
this.cellWidth = mazeSize / numCells;
this.visited = false;
this.walls = {
topWall: true,
rightWall: true,
bottomWall: true,
leftWall: true
}
this.startX = this.column * this.cellWidth
this.startY = this.row * this.cellWidth
this.visitedByMazeGenerator = false
this.distanceFromStart = 0
this.visitedByDistanceAlgo = false
this.inPath = false
}
// Methods to draw the walls
drawTopWall() {
ctx.beginPath();
ctx.moveTo(this.startX, this.startY)
ctx.lineTo(this.startX + this.cellWidth, this.startY)
ctx.lineWidth = boarderSize
ctx.stroke()
}
drawRightWall() {
ctx.beginPath();
ctx.moveTo(this.startX + this.cellWidth, this.startY)
ctx.lineTo(this.startX + this.cellWidth, this.startY + this.cellWidth)
ctx.lineWidth = boarderSize
ctx.stroke()
}
drawBottomWall() {
ctx.beginPath();
ctx.moveTo(this.startX, this.startY + this.cellWidth)
ctx.lineTo(this.startX + this.cellWidth, this.startY + this.cellWidth)
ctx.lineWidth = boarderSize
ctx.stroke()
}
drawLeftWall() {
ctx.beginPath();
ctx.moveTo(this.startX, this.startY)
ctx.lineTo(this.startX, this.startY + this.cellWidth)
ctx.lineWidth = boarderSize
ctx.stroke()
}
show() {
if (this.inPath) {
ctx.fillStyle = "rgb(80,80,80)";
ctx.fillRect(this.startX, this.startY, this.cellWidth, this.cellWidth)
}
if (this.visited) {
// ctx.fillStyle = "#F8F8F8";
ctx.fillStyle = "rgb(110,110,110)";
ctx.fillRect(this.startX, this.startY, this.cellWidth, this.cellWidth)
}
if (this.visitedByDistanceAlgo) {
ctx.fillStyle = '#F8F8F8'; // set background grey to support opacity
ctx.fillRect(this.startX, this.startY, this.cellWidth, this.cellWidth)
const intensity = 2 * this.distanceFromStart / (numCells ** 2) //betting very few times do we ever have a path that is 80% of total num cells
ctx.fillStyle = `rgba(0, 0, 255, ${intensity})`;
ctx.fillRect(this.startX, this.startY, this.cellWidth, this.cellWidth)
}
if (this.walls.topWall) {
this.drawTopWall()
}
if (this.walls.rightWall) {
this.drawRightWall()
}
if (this.walls.bottomWall) {
this.drawBottomWall()
}
if (this.walls.leftWall) {
this.drawLeftWall()
}
}
highlight() {
ctx.fillStyle = "pink";
ctx.fillRect(this.startX, this.startY, this.cellWidth, this.cellWidth)
}
highlightWallBreak() {
ctx.fillStyle = "rgb(105,105,105)";
ctx.fillRect(this.startX, this.startY, this.cellWidth, this.cellWidth)
}
}