-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.js
More file actions
168 lines (118 loc) · 4.34 KB
/
Copy pathgrid.js
File metadata and controls
168 lines (118 loc) · 4.34 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
const height = window.innerHeight
const width = window.innerWidth
const sizeInput = document.querySelector('.size-input')
let size = sizeInput.value
let nrows = Math.round((height - 30) / size)
let ncols = Math.round((width - 30) / size)
let startx;
let starty;
let endx;
let endy;
// An array to store the last move to be able to undo
let lastMove = []
var graph = document.querySelector(".graph")
sizeInput.addEventListener('change', () => {
size = sizeInput.value
nrows = Math.round((height - 30) / size)
ncols = Math.round((width - 30) / size)
generateGrid()
})
//we can make a wall by changing the background color or adding a new class to it
const makeWall = (e) => {
e.preventDefault()
if(e.target.classList.contains("wall")) {
return
}
lastMove.push([e.target, mouseDownCount])
e.target.classList.add('node-wall')
}
//mouse down flag
let mouseDown = false
let mouseDownCount = 0
//add an event to know when the mouse is clicked so we can draw walls
window.onmousedown = (e) => {
mouseDownCount++
//check if it clicked on a grid cell since this event is on the window
if (e.target.classList.contains('cells') && e.buttons == 1) {
mouseDown = true
}
lastMove.push([e.target, mouseDownCount])
}
window.onmouseup = () => {
mouseDown = false
}
// adding a keylistner that can undo the last move
window.addEventListener('keydown', (e) => {
e.preventDefault()
//if the pressed key is u then remove whatever we have in the stack and reset the background
if(e.key === 'u'){
let move = lastMove[lastMove.length - 1][1]
while(lastMove.length > 0){
if(move != lastMove[lastMove.length - 1][1]){
return
}
lastMove.pop()[0].classList.remove('node-wall')
}
}
})
function generateGrid(){
//if we regenereated the grid we need to reset the inner html
graph.innerHTML = ''
for (let i = 0; i < nrows; i++) {
let row = document.createElement("div")
row.classList.add("rows")
for (let j = 0; j < ncols; j++) {
let col = document.createElement("div")
col.style.width = size + "px"
col.style.height = size + "px"
col.addEventListener('click',
makeWall)
// when the mouse enters it will call this function with a paremeter of the event that has the target and position and other details
col.addEventListener('mouseover', (e) => {
if (mouseDown) {
makeWall(e);
}
})
col.addEventListener('mousedown', (e) => {
makeWall(e);
})
col.classList.add("cells")
row.appendChild(col)
}
graph.appendChild(row)
}
placeStartEnd()
makegraphRefrence()
}
function placeStartEnd(){
startx = Math.floor(Math.random() * ncols / 4)
starty = Math.floor(Math.random() * nrows / 4)
endx = Math.floor(Math.random() * ncols * 3 / 4)
endy = Math.floor(Math.random() * nrows* 3 / 4)
while(graph.children[starty].children[startx].classList.contains("node-wall")){
startx = Math.floor(Math.random() * ncols / 4)
starty = Math.floor(Math.random() * nrows / 4)
}
graph.children[starty].children[startx].innerHTML = '<i class="fa-solid fa-play play"></i>'
graph.children[starty].children[startx].classList.add('startDiv')
while(graph.children[endy].children[endx].classList.contains("node-wall") || (endx == startx && endy == starty)){
endx = Math.floor(Math.random() * ncols / 4)
endy = Math.floor(Math.random() * nrows / 4)
}
graph.children[endy].children[endx].innerHTML = '<i class="fa-solid fa-forward-step end"></i>'
graph.children[endy].children[endx].classList.add('endDiv')
}
function removeStartEnd(){
graph.children[starty].children[startx].innerHTML = ''
graph.children[starty].children[startx].classList.remove('startDiv')
graph.children[endy].children[endx].innerHTML = ''
graph.children[endy].children[endx].classList.remove('endDiv')
}
let graphRefrence = []
function makegraphRefrence(){
graphRefrence = []
for (let i = 0; i < graph.children.length; i++) {
graphRefrence.push(graph.children[i])
}
}
generateGrid();