-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
127 lines (111 loc) · 3.54 KB
/
main.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
var canvas = document.querySelector("canvas")
canvas.width = 1000;
canvas.height = 1000;
var maze=canvas.getContext("2d");
maze.fillStyle = "white";
maze.fillRect(0,0,canvas.width,canvas.height);
maze.strokeStyle="blue";
var door=true;
//The cell prototype
function cell(i,j){
this.i = i;
this.j = j;
this.visited=false;
this.walls = [true,true,true,true];
this.show = function(){
maze.beginPath();
if(this.walls[0]){
maze.moveTo(i ,j)
maze.lineTo(i+40, j)
}
if(this.walls[1]){
maze.moveTo(i+40,j)
maze.lineTo(i+40,j+40)
}
if(this.walls[2]){
maze.moveTo(i+40,j+40)
maze.lineTo(i ,j+40)
}
if(this.walls[3]){
maze.moveTo(i ,j+40 )
maze.lineTo(i ,j)
}
maze.stroke();
}
this.visit=function(){
// maze.fillStyle="green";
// maze.fillRect(i,j,40,40);
this.visited=true;
}
this.visitoo=function(){
maze.fillStyle="red";
maze.fillRect(i,j,40,40);
}
}
// GRID MAKING
var h = 25;
var w = 25;
let grid = [];
for(var i = 0;i<h;i++)
for(var j = 0 ;j< w;j++){
var c = new cell(i*40,j*40);
grid.push(c);
}
//MAAAAAZ MAKING
var ic;
function maker(i,e){
ic = grid[i];
ic.walls[e]=false;
ic.visit();
let pos = [];
if(ic.i !== 0)
{pos.push(-w);console.log(1);}
if(ic.i !== (w-1)*40)
{pos.push(w);console.log(2)}
if(ic.j !== 0)
{pos.push(-1);console.log(3)}
if(ic.j !== (h-1)*40)
{pos.push(1);console.log(4)}
console.log("dis is pos befor => "+pos)
while(pos.length> 0){
let random =Math.floor(Math.random()*pos.length);
let next = pos.splice(random,1)
console.log("dis is random => "+random)
console.log("dis is i => "+i)
console.log("dis is pos => "+pos)
console.log("dis is next => "+next[0])
console.log(grid[i+next[0]].visited)
if( door && (grid[i].i == (h-1)*40 || grid[i].j == (w-1)*40))
if(grid[i].i == (h-1)*40){grid[i].walls[1]=false;door=false;}
else if (grid[i].j == (w-1)*40){grid[i].walls[2]=false;door=false;}
if( grid[i+next[0]].visited !== true)
switch (next[0]) {
case -w:
grid[i].walls[3]=false;
console.log("gauche",i,"====",next[0]+i)
maker(i-w,1)
console.log("===== wellina el ==> ",i,"====",next[0]+i);
break;
case w:
grid[i].walls[1]=false;
console.log("droit ",i,"====",next[0]+i)
maker(i+w,3)
console.log("===== wellina el ==> ",i,"====",next[0]+i);
break;
case -1:
console.log("haut",i,"====",next[0]+i);
grid[i].walls[0]=false;
maker(i-1,2)
console.log("===== wellina el ==> ",i,"====",next[0]+i);
break;
case 1:
console.log("bas ",i,"====",next[0]+i)
grid[i].walls[2]=false;
maker(i+1,0)
console.log("===== wellina el ==> ",i,"====",next[0]+i);
break;
}
}
}
(maker)(25,0)
grid.forEach(e => e.show());