Skip to content

Commit e0d3208

Browse files
committed
Commit_1
1 parent cf9ac6a commit e0d3208

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

index.html

+2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
<meta charset="UTF-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<meta property = "og:image" content ="https://icons8.com/icon/ZdVivU3rI52R/external-maze-geek-those-icons-lineal-those-icons">
78
<title>Maze Path Finding Algo</title>
9+
<link rel="icon" href ="https://img.icons8.com/external-those-icons-lineal-those-icons/452/external-Maze-geek-those-icons-lineal-those-icons.png" type ="image/x-icon">
810
<link rel="stylesheet" href="style.css">
911
</head>
1012
<body>

obj.js

+44-21
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Maze{
66
this.matrix = this.preparemaze(this.rows, this.cols)
77
this.startNode = this.matrix[0][0]
88
this.endNode = this.matrix[1][1]
9+
910

1011

1112
this.el.addEventListener("toggleWall", () => {
@@ -49,7 +50,6 @@ class Maze{
4950
clearMaze(){
5051
this.matrix.forEach(row => {
5152
row.forEach(node => {
52-
node.removeClass("wall")
5353
node.removeClass("searching")
5454
node.removeClass("curr")
5555
node.removeClass("inWay")
@@ -105,6 +105,7 @@ class Node{
105105
this.isWall = false
106106
this.isStart = false
107107
this.isEnd = false
108+
this.temp = new Array(0, 0, 0, 0) // temp border for prev wall memory, can be used if old memory removed
108109

109110
this.el = document.createElement("td")
110111
this.el.id = getId(this.i, this.j)
@@ -119,14 +120,25 @@ class Node{
119120

120121
this.el.ondrop = (e) => this.drop(e)
121122

122-
this.el.onmousedown = (e) => {Node.clicked = true}
123-
this.el.onmouseover = (e) => {this.mouseEvent(e)}
124-
this.el.onmouseup = (e) => {Node.clicked = false}
123+
this.el.onmousedown = (e) => {this.mouseEvent(e, "down")}
124+
this.el.onmouseover = (e) => {this.mouseEvent(e, "over")}
125+
this.el.onmouseup = (e) => {this.mouseEvent(e, "up")}
125126
}
126127

127-
mouseEvent(event){
128-
if(Node.clicked){
129-
this.toggleWall()
128+
mouseEvent(event, type){
129+
// event.preventDefault()
130+
switch(type){
131+
case "down":
132+
if(!this.isStart && !this.isEnd) Node.clicked = true
133+
break
134+
case "up":
135+
Node.clicked = false
136+
break
137+
case "over":
138+
if(!this.isStart && !this.isEnd && Node.clicked)
139+
this.toggleWall()
140+
break
141+
130142
}
131143
}
132144

@@ -138,15 +150,13 @@ class Node{
138150

139151
drop(event){
140152
event.preventDefault()
141-
if(!this.isStart && !this.isEnd){
142-
let data = event.dataTransfer.getData("data")
143-
let loc = Maze.getLoc(event.target.id)
144-
if(data == "start"){
145-
this.maze.setStartNode(loc[0], loc[1])
146-
}
147-
if(data == "end"){
148-
this.maze.setEndNode(loc[0], loc[1])
149-
}
153+
let data = event.dataTransfer.getData("data")
154+
let loc = Maze.getLoc(event.target.id)
155+
if(data == "start" && !this.isEnd){
156+
this.maze.setStartNode(loc[0], loc[1])
157+
}
158+
if(data == "end" && !this.isStart){
159+
this.maze.setEndNode(loc[0], loc[1])
150160
}
151161
}
152162

@@ -157,16 +167,29 @@ class Node{
157167
this.el.classList.remove(name)
158168
}
159169

170+
160171
toggleWall(){
161172
if(this.isWall){
173+
// removing wall
162174
this.isWall = false
163175
this.removeClass("wall")
164-
for(let i = 0 ; i < this.sides ;++i) this.removeWall(i)
165-
return
176+
this.border = this.temp
177+
if(this.border.toString() != "0,0,0,0"){
178+
for(let i = 0 ; i < this.sides && !this.temp ; ++i)
179+
this.removeWall(i)
180+
}
181+
}
182+
else{
183+
// building wall
184+
if((this.border.toString() != "1,1,1,1")){
185+
console.log("here");
186+
this.temp = new Array(...this.border)
187+
}
188+
this.isWall = true
189+
this.addClass("wall")
190+
for(let i = 0 ; i < this.sides ; ++i)
191+
this.addWall(i)
166192
}
167-
this.isWall = true
168-
this.addClass("wall")
169-
for(let i = 0 ; i < this.sides ;++i) this.addWall(i)
170193
}
171194

172195
addWall(i){

0 commit comments

Comments
 (0)