-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker_labyrinth.js
112 lines (98 loc) · 2.04 KB
/
worker_labyrinth.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
/* eslint-disable */
// worker_labyrinth.js
let a;
let rows;
let cols;
let finished;
let found;
let slow;
// finished: 1|-1 found: 1|-1 data: a
self.onmessage = (event) => {
// receiving: { a: a, rows: rows , cols: cols, slow: slow});
a = event.data.a;
rows = event.data.rows;
cols = event.data.cols;
slow = parseInt(event.data.slow);
found = wayout(0, 0);
if (found == 1) {
postMessage({
finished: 1,
found: 1,
element: 'green',
yy: 0,
xx: 0,
});
} else {
postMessage({
finished: 1,
found: -1,
element: 'green',
yy: 0,
xx: 0,
});
}
};
let wayout = (x, y) => {
console.log(`slow= ${slow}${1}`);
// REKURSIONSABBRUCH; REKURSIONSABBRUCH
// Abbruch, wenn ausserhalb des Labs
if (x < 0 || x >= cols) {
return -1;
}
if (y < 0 || y >= rows) {
return -1;
}
// Abbruch, wenn Wand
if (a[x][y] == 'black' || a[x][y] == 'red' || a[x][y] == 'green') { // Wand
return -1;
}
// Abbruch, wir sind nun am Ziel
if (x == cols - 1 && y == rows - 1) {
console.log('AM ZIEL!!!!');
return 1;
}
// markiere momentanen Weg
a[x][y] = 'green';
// inform caller to paint a green cell
postMessage({
finished: -1,
found: -1,
element: 'green',
yy: y,
xx: x,
});
wait(slow);
// suche einen Weg durch das Lab
if (wayout(x, y - 1) == 1) { // top
// a[x][y-1]= Color.red;
return 1;
} else if (wayout(x + 1, y) == 1) { // right
// a[x+1][y]= Color.green;
return 1;
} else if (wayout(x, y + 1) == 1) { // bottom
// a[x][y+1]= Color.red;
return 1;
} else if (wayout(x - 1, y) == 1) { // left
// a[x-1][y]= Color.red;
return 1;
}
// wenn wir bis hierher kommen gibts keinen weg
a[x][y] = 'red';
// inform caller for painting
postMessage({
finished: -1,
found: -1,
element: 'red',
yy: y,
xx: x,
});
wait(slow);
return -1;
}
let wait = (ms) => {
const start = new Date().getTime();
let end = start;
while (end < start + ms) {
end = new Date().getTime();
}
}