-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.js
144 lines (132 loc) · 4.21 KB
/
Node.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
function Node(row, col, x, y, state, total_rows, total_cols) {
this.row = row;
this.col = col;
this.x = x;
this.y = y;
this.myGap = 1;
this.total_rows = total_rows;
this.total_cols = total_cols;
this.state = state;
this.neighbors = [];
this.distance = Infinity;
this.visited = false;
}
Node.prototype.draw_bg = function(ctx , gap){
ctx.fillStyle = '#AAAAAA';
ctx.beginPath();
ctx.rect(this.x, this.y, gap, gap);
ctx.closePath();
ctx.fill();
}
Node.prototype.draw_node = async function (ctx, gap) {
var midGap = parseInt(gap / 2) ;
if (this.state == 's') {
if (this.myGap != midGap)
this.draw_bg(ctx,gap) ;
ctx.fillStyle = '#00FF00';
ctx.beginPath();
ctx.rect(this.x + midGap - this.myGap, this.y + midGap - this.myGap, 2 * this.myGap, 2 * this.myGap);
ctx.closePath();
ctx.fill();
sleep(1);
if (this.myGap != midGap)
this.myGap += 1
}
else if (this.state == 'f') {
if (this.myGap != midGap)
this.draw_bg(ctx,gap) ;
ctx.fillStyle = '#FF0000';
ctx.beginPath();
ctx.rect(this.x + midGap - this.myGap, this.y + midGap - this.myGap, 2 * this.myGap, 2 * this.myGap);
ctx.closePath();
ctx.fill();
sleep(1);
if (this.myGap != midGap)
this.myGap += 1
}
else if (this.state == 'w') {
if (this.myGap != midGap)
this.draw_bg(ctx,gap) ;
ctx.fillStyle = '#0000FF';
ctx.beginPath();
ctx.rect(this.x + midGap - this.myGap, this.y + midGap - this.myGap, 2 * this.myGap, 2 * this.myGap);
ctx.closePath();
ctx.fill();
sleep(1);
if (this.myGap != midGap)
this.myGap += 1
}
else if (this.state == 'e') {
ctx.fillStyle = '#cbc0c0';
ctx.beginPath();
ctx.rect(this.x, this.y, gap, gap);
ctx.closePath();
ctx.fill();
this.myGap = 1;
// ctx.fillStyle = '#A78D84' ;
}
else if (this.state == 'd') {
ctx.fillStyle = '#FFD700';
ctx.beginPath();
ctx.rect(this.x, this.y, gap, gap);
ctx.closePath();
ctx.fill();
}
else if (this.state == 'p') {
if(this.myGap != midGap){
this.draw_bg(ctx,gap);
ctx.fillStyle = '#228B22';
ctx.beginPath();
ctx.arc(this.x + midGap , this.y + midGap , this.myGap , 0 ,2*Math.PI);
ctx.closePath();
ctx.fill();
sleep(0.1);
this.myGap += 1
}
else{
ctx.fillStyle = '#228B22';
ctx.beginPath();
ctx.rect(this.x , this.y , gap , gap);
ctx.closePath();
ctx.fill();
}
}
}
Node.prototype.updateNeighbors = function () {
var n = total_rows;
var m = total_cols;
//Checking if the cell above can be made neighbor
if (this.row > 0 && grid[this.row - 1][this.col].state != 'w') {
this.neighbors.push(grid[this.row - 1][this.col]);
}
//Checking if the cell below can be made neigbor
if (this.row < n - 1 && grid[this.row + 1][this.col].state != 'w') {
this.neighbors.push(grid[this.row + 1][this.col]);
}
//Checking if the cell on left can be made neigbor
if (this.col > 0 && grid[this.row][this.col - 1].state != 'w') {
this.neighbors.push(grid[this.row][this.col - 1]);
}
//Checking if the cell on right can be made neigbor
if (this.col < m - 1 && grid[this.row][this.col + 1].state != 'w') {
this.neighbors.push(grid[this.row][this.col + 1]);
}
}
const sleep = (time) => {
return new Promise(
(resolve) => setTimeout(resolve, time)
);
}
function cmp(nodeA, nodeB) {
return nodeA.distance - nodeB.distance;
}
function disable() {
document.getElementById("Algo-btn").disabled = true;
document.getElementById("makeGrid").disabled = true;
document.getElementById("resetBtn").disabled = true ;
}
function enable() {
document.getElementById("Algo-btn").disabled = false;
document.getElementById("makeGrid").disabled = false;
document.getElementById("resetBtn").disabled = false ;
}