-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript.js
361 lines (317 loc) · 9.98 KB
/
Script.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
const questionDiv = document.getElementById('question');
let Exits = [];
var max = 80;
rownum = parseInt(((window.innerHeight)/100)*20);
colnum = parseInt(((window.innerWidth)/100)*19.8);
// rownum=colnum=max;
reset = true;
if(rownum%2==0){
rownum+=1;
}
if(colnum%2==0){
colnum+=1;
}
function inputInsert() {
const grid = Array.from({ length: rownum }, () => Array(colnum).fill('wall'));
questionDiv.style.gridTemplateColumns = `repeat(${colnum}, 1fr)`;
Exits = [
[1, 1],
[rownum - 2, colnum - 2]
];
function carvePath(row, col) {
const stack = [[row, col]];
grid[row][col] = 'path';
while (stack.length) {
const [currentRow, currentCol] = stack[stack.length - 1];
const directions = [
[0, 1], [1, 0], [0, -1], [-1, 0]
].sort(() => Math.random() - 0.5);
let moved = false;
for (const [dr, dc] of directions) {
const newRow = currentRow + dr * 2;
const newCol = currentCol + dc * 2;
if (
newRow > 0 && newRow < rownum - 1 &&
newCol > 0 && newCol < colnum - 1 &&
grid[newRow][newCol] === 'wall'
) {
grid[currentRow + dr][currentCol + dc] = 'path';
grid[newRow][newCol] = 'path';
stack.push([newRow, newCol]);
moved = true;
break;
}
}
if (!moved) {
stack.pop();
}
}
}
carvePath(Exits[0][0], Exits[0][1]);
const fragment = document.createDocumentFragment();
for (let row = 0; row < rownum; row++) {
for (let col = 0; col < colnum; col++) {
const div = document.createElement('div');
div.id = `${row},${col}`;
div.classList.add('cell-input', grid[row][col]);
div.addEventListener('click', () => wallData(row, col));
fragment.appendChild(div);
}
}
questionDiv.innerHTML = '';
questionDiv.appendChild(fragment);
const startDiv = document.getElementById(`${Exits[0][0]},${Exits[0][1]}`);
startDiv.classList.remove('wall');
startDiv.classList.add('Start');
const endDiv = document.getElementById(`${Exits[1][0]},${Exits[1][1]}`);
endDiv.classList.remove('wall');
endDiv.classList.add('End');
let rands = 0;
const cleared = new Set();
let WhileCount = 0;
let WallsData=document.querySelectorAll(".wall");
targetClears=parseInt((WallsData.length/100)*12)
// console.log("TotalWall: "+(WallsData.length)+" Deletes: "+targetClears)
while (rands < targetClears) {
if (WhileCount > WallsData.length) {
break;
}
let tempWall=document.querySelectorAll(".wall");
randomIndex = Math.floor(Math.random() * tempWall.length);
let ChoosenObject=tempWall[randomIndex];
let id = ChoosenObject.id.split(",");
x=id[0];
y=id[1];
if(x!=0 && y!=0 && x!=rownum-1 && y!=colnum-1){
if(ChoosenObject.classList.contains("wall")){
ChoosenObject.classList.remove("wall")
}
rands++;
}
WhileCount++;
}
let tempPath = document.querySelectorAll(".path");
tempPath.forEach((el) => {
el.classList.remove("path");
});
}
inputInsert()
function wallData(row,col){
if(row==0 || col==0 || row==rownum-1 || col==colnum-1){
// document.getElementById(row+","+col).style.backgroundColor="yellow"
}else{
resetall()
object=document.getElementById(row+","+col);
if(object.classList.contains("wall")){
object.classList.remove("wall")
}else{
object.classList.add("wall")
}
}
}
async function findWay(){
resetall()
if(!reset){
return
}
reset=false;
start=Exits[0];
if(!(await nextMove(start[0],start[1]))){
alert('No Path Found')
reset=true;
}else{
finished()
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function nextMove(row,col){
let nextvalue=nonWall(row,col);
await sleep(1)
for(let i=0;i<nextvalue.length;i++){
if(await finishCheck(nextvalue[i][0],nextvalue[i][1])){
return true;
}
}
for(let i=0;i<nextvalue.length;i++){
let nextobject=document.getElementById((nextvalue[i][0])+","+(nextvalue[i][1]));
nextobject.classList.add("Path");
if(await nextMove(nextvalue[i][0],nextvalue[i][1])){
return true
}
nextobject.classList.remove("Path");
nextobject.classList.add("wrong");
}
return false
}
function nonWall(row,col){
let NoWalls=[];
let nextvalue=[[row,(col+1)],[(row+1),col],[(row-1),col],[row,(col-1)],];
for(let i=0;i<nextvalue.length;i++){
if(nextvalue[i][0]>=0 && nextvalue[i][1]>=0){
let object=document.getElementById((nextvalue[i][0])+","+(nextvalue[i][1]));
if(!(object.classList.contains('wall'))&& !(object.classList.contains('Path'))&& !(object.classList.contains('wrong'))){
NoWalls.push([nextvalue[i][0],nextvalue[i][1]])
}
}
}
return NoWalls;
}
function finishCheck(row,col){
if(document.getElementById((row)+","+col).classList.contains('End')){
return true
}
}
async function finished(){
for(ii=rownum-1;ii>=0;ii--){
await sleep(1)
for(iii=colnum-1;iii>=0;iii--){
let object=document.getElementById((ii)+","+(iii));
if(object.classList.contains('Path')){
object.classList.add('pass')
}
}
}
for(ii=rownum-1;ii>=0;ii--){
await sleep(1)
for(iii=colnum-1;iii>=0;iii--){
let object=document.getElementById((ii)+","+(iii));
if(object.classList.contains('Path')){
object.classList.remove('pass')
}
}
}
reset=true;
}
function resetall(){
if(reset){
Paths=[];
objects=document.querySelectorAll(".Path");
objects.forEach((elemant)=>{
elemant.classList.remove('Path')
})
objects=document.querySelectorAll(".wrong");
objects.forEach((elemant)=>{
elemant.classList.remove('wrong')
})
objects=document.querySelectorAll(".Finish");
objects.forEach((elemant)=>{
elemant.classList.remove('Finish')
})
}
}
async function removeNonPath(correctPath) {
await sleep(500);
reset=true
resetall()
reset=false
correctPath.forEach(element => {
document.getElementById((element[0]) + "," + (element[1])).classList.add('Finish');
});
await sleep(5);
finished()
}
async function fastWay() {
resetall()
if(!reset){
return
}
reset=false
let start = [];
start.push(Exits[0]);
let doubles=[]
if (await fastMove(start,doubles)) {
const path = findPath(Exits[1],doubles);
await removeNonPath(path);
} else {
alert('No Path Found')
reset=true;
}
}
async function fastMove(Paths,doubles) {
let tempPath = [];
await sleep(10);
for (const element of Paths) {
let nextvalue = nonWall(element[0], element[1]);
if(nextvalue.length==0){
if(!(document.getElementById(element[0]+","+element[1]).classList.contains("Start"))){
doubles=await doublesClear(doubles,element)
document.getElementById(element[0]+ "," + element[1]).classList.add("wrong")
}
}
for (let i = 0; i < nextvalue.length; i++) {
if (await finishCheck(nextvalue[i][0], nextvalue[i][1])) {
doubles.push([[element[0], element[1]],[nextvalue[i][0], nextvalue[i][1]]])
return true;
}
}
for (let i = 0; i < nextvalue.length; i++) {
let nextobject = document.getElementById((nextvalue[i][0]) + "," + (nextvalue[i][1]));
nextobject.classList.add("Path");
doubles.push([[element[0], element[1]],[nextvalue[i][0], nextvalue[i][1]]])
tempPath.push([nextvalue[i][0], nextvalue[i][1]]);
}
}
if (tempPath.length === 0) {
return false;
}
if (await fastMove(tempPath,doubles)) {
return true;
}
return false;
}
function areArraysEqual(arr1, arr2) {
return arr1[0] === arr2[0] && arr1[1] === arr2[1];
}
function findPath(start,data) {
let result = [start];
let current = start;
while (!areArraysEqual(current, Exits[0])) {
let found = false;
for (let i = 0; i < data.length; i++) {
if (areArraysEqual(data[i][1], current)) {
result.unshift(data[i][0]);
current = data[i][0];
found = true;
break;
}
}
if (!found) break;
}
return result;
}
function doublesClear(doubles,element){
let terminated=element;
k=0
while(true){
count=0;
for (let i = 0; i < doubles.length; i++) {
if (areArraysEqual(doubles[i][0], terminated)) {
count++;
}
}
if(count==0){
for (let i = 0; i < doubles.length; i++) {
if (areArraysEqual(doubles[i][1], terminated)) {
terminated=doubles[i][0]
index=doubles.indexOf(doubles[i])
if (index !== -1) {
doubles.splice(index, 1);
}
}
}
}else{
break;
}
k++
}
return doubles
}
function newArraysEqual(arr1, arr2) {
if (arr1.length !== arr2.length) return false;
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) return false;
}
return true;
}