-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
209 lines (170 loc) · 5.14 KB
/
app.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
window.addEventListener('load', function () {
new init;
});
function init() {
drawCells();
bindEventsListeners();
}
const SETTINGS = {
free: 0,
wall: 1,
start: 2,
finish: 3,
rowsQuantity: 20,
columnsQuantity: 20,
gameArr: []
}
function drawCells() {
const CELLS_QUANTITY = SETTINGS.rowsQuantity * SETTINGS.columnsQuantity;
const $container = document.getElementById("container");
for (let i = 0; i < CELLS_QUANTITY; i++) {
let $cell = document.createElement("div");
$cell.classList = "cell";
$container.appendChild($cell);
}
}
function createMatrix() {
let matrix = [];
if (matrix.length === 0) {
for (let i = 0; i < SETTINGS.rowsQuantity; i++) {
matrix[i] = [];
for (let j = 0; j < SETTINGS.columnsQuantity; j++) {
matrix[i][j] = Math.round(Math.abs(Math.random() - .3));
}
}
matrix[0][0] = SETTINGS.start;
matrix[SETTINGS.rowsQuantity - 1][SETTINGS.columnsQuantity - 1] = SETTINGS.finish;
}
return SETTINGS.gameArr = matrix;
}
function getMatrix() {
if (SETTINGS.gameArr.length === 0) {
let tempMatrix = createMatrix();
return tempMatrix;
} else {
return SETTINGS.gameArr;
}
}
function drawMatrix() {
let currentMatrix = SETTINGS.gameArr;
let $cellContainers = document.getElementsByClassName("cell");
let stringMatrix = currentMatrix.flat();
for (let i = 0; i < $cellContainers.length; i++) {
let cellType = stringMatrix[i];
let classNames = ['cell', 'wall', 'startCell', 'finishCell'];
$cellContainers[i].classList.add(classNames[cellType]);
$cellContainers[i].innerHTML = stringMatrix[i];
}
return currentMatrix;
}
function bindEventsListeners() {
document.getElementById("generate").addEventListener("click", generateBtnClick);
document.getElementById("find-path-btn").addEventListener("click", findPathBtnClick);
}
function generateBtnClick(e) {
const $divError = document.querySelector(".alert");
if ($divError) {
$divError.remove();
}
clearMatrix();
let $findPathBtn = document.getElementById("find-path-btn");
e.target.setAttribute("disabled", "disabled");
$findPathBtn.removeAttribute("disabled");
createMatrix();
drawMatrix();
}
function findPathBtnClick(e) {
let $generateBtn = document.getElementById("generate");
e.target.setAttribute("disabled", "disabled");
$generateBtn.removeAttribute("disabled");
drawPath();
}
function clearMatrix(e) {
let $cellContainers = document.getElementsByClassName("cell");
for (i in $cellContainers) {
$cellContainers[i].innerHTML = '';
$cellContainers[i].classList = 'cell';
}
let $generateBtn = document.getElementById("generate");
$generateBtn.disabled = false;
}
function findShortestPath() {
let matrix = getMatrix();
let start = [0, 0];
let end = [19, 19];
function findWay(position, end) {
let queue = [];
queue.push([position]); // store a path, not just a position
while (queue.length > 0) {
let path = queue.shift(); // get the path out of the queue
let pos = path[path.length - 1]; // ... and then the last position from it
let direction = [
[pos[0] + 1, pos[1]],
[pos[0], pos[1] + 1],
[pos[0] - 1, pos[1]],
[pos[0], pos[1] - 1]
];
for (let i = 0; i < direction.length; i++) {
// Perform this check first:
if (direction[i][0] == end[0] && direction[i][1] == end[1]) {
// return the path that led to the find
return path.concat([end]);
}
if (direction[i][0] < 0 || direction[i][0] >= matrix[0].length ||
direction[i][1] < 0 || direction[i][1] >= matrix[0].length ||
matrix[direction[i][0]][direction[i][1]] != 0) {
continue;
}
matrix[direction[i][0]][direction[i][1]] = 1;
// extend and push the path on the queue
queue.push(path.concat([direction[i]]));
}
}
}
return findWay(start, end);
}
function drawPath() {
let $cellContainers = document.getElementsByClassName("cell");
let tempArr = getTempArr();
if (!tempArr) {
return;
}
for (i in $cellContainers) {
for (let m = 0; m < tempArr.length; m++) {
if (i === $cellContainers.length - 1) {
break;
}
if (i == Number(tempArr[m])) {
$cellContainers[i].className += " foundWay";
$cellContainers[i].innerHTML = tempArr[m];
tempArr.shift();
}
}
}
}
function getTempArr() {
let path = findShortestPath();
if (path === undefined) {
let text = "The path is undefined. Please try to re-generate labyrinth";
showMessage(text);
} else {
let gapArr = [];
for (let i = 0; i < path.length; i++) {
let temp = path[i];
let number = temp[0] * 20 + parseInt(temp[1]);
gapArr.push(number);
}
return gapArr;
}
}
function showMessage(someText) {
const div = document.createElement("div");
div.className = 'alert fail';
div.appendChild(document.createTextNode(someText));
const $wrapper = document.getElementById("wrapper");
const $title = document.getElementById("title");
$wrapper.insertBefore(div, $title);
setTimeout(function () {
document.querySelector(".alert").remove();
}, 5000);
}