forked from ozhi/graph-visualizer
/
board.js
74 lines (58 loc) · 1.45 KB
/
board.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
'use strict';
class Board {
constructor(rows, cols) {
this.rows = rows;
this.cols = cols;
this.board = [];
for (let i = 0; i < rows; i++) {
this.board[i] = [];
for (let j = 0; j < cols; j++) {
this.board[i][j] = new Cell(i, j);
}
}
this.endReached = false;
}
getCell(row, col) {
return this.board[row][col];
}
}
function drawBoard() {
// console.log('Drawing board...')
for (let i = 0; i < world.board.rows; i++) {
for (let j = 0; j < world.board.cols; j++) {
drawCell(world.board.getCell(i, j));
}
}
}
function getNeighbors(cell) {
const neighbors = [];
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (i * i + j * j === 1) {
const row = cell.row + i;
const col = cell.col + j;
if (isValidCell(row, col)) {
neighbors.push(world.board.getCell(row, col));
}
}
}
}
return neighbors;
}
function isValidCell(row, col) {
return 0 <= row && row < world.board.rows &&
0 <= col && col < world.board.cols;
}
function resetBoard() {
world.board = new Board(world.config.rows, world.config.cols);
console.log('The board was reset');
}
function colorFoundWay(cell) {
if (!cell) {
cell = world.board.getCell(world.config.end.row, world.config.end.col);
}
cell.status = CellStatus.MARKED;
if (cell.parent) {
setTimeout(() => colorFoundWay(cell.parent), world.config.stepWait);
}
}