-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
146 lines (131 loc) · 4.88 KB
/
index.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
// table
let rowsNum = 20 , colsNum = 60;
let grid = document.createElement('table');
const tablebody = document.createElement('tbody');
grid.appendChild(tablebody);
for (let i = 0 ; i<rowsNum ; i++) {
let row = document.createElement('tr');
for (let j = 0 ; j<colsNum ; j++) {
let cell = document.createElement('td');
cell.classList.add("dead");
cell.onclick = () => {
if (cell.classList.contains("alive")) {
cell.classList.remove("alive");
cell.classList.add("dead");
}
else {
cell.classList.remove("dead");
cell.classList.add("alive");
}
};
row.appendChild(cell);
}
grid.appendChild(row);
}
document.body.appendChild(grid);
// buttons
let barMaxSpeed = 1000 , barMinSpeed = 0 , barAvgValue = 500;
const buttonsdiv = document.createElement("div");
const startbutton = document.createElement("button");
startbutton.innerHTML = "start";
const stopbutton = document.createElement("button");
stopbutton.innerHTML = "stop";
const resetbutton = document.createElement("button");
resetbutton.innerHTML = "reset";
const slidebar = document.createElement("input");
slidebar.labels
slidebar.type = "range";
slidebar.min = barMinSpeed;
slidebar.max = barMaxSpeed;
slidebar.value = barAvgValue;
buttonsdiv.appendChild(startbutton);
buttonsdiv.appendChild(stopbutton);
buttonsdiv.appendChild(resetbutton);
buttonsdiv.appendChild(slidebar);
document.body.appendChild(buttonsdiv);
let isworking = -1;
startbutton.onclick = () => {
let aliveNeighboursNum = document.getElementsByClassName("alive").length;
if (isworking != -1 || aliveNeighboursNum<=0) return;
let speed = barMaxSpeed - slidebar.value;
isworking = setInterval(change , speed);
};
stopbutton.onclick = () => {
if (isworking != -1) {
clearInterval(isworking);
isworking = -1;
}
};
slidebar.addEventListener("change" , () => {
if (isworking != -1) {
let speed = barMaxSpeed - slidebar.value;
clearInterval(isworking);
isworking = setInterval(change , speed);
}
});
resetbutton.onclick = () => {
if (isworking == -1) {
let aaliveNeighbours = document.getElementsByClassName("alive");
for (let i = 0 ; i<aaliveNeighbours.length ; i++) {
aaliveNeighbours[i].classList.add("dead");
}
for (let i = 0 ; i<aaliveNeighbours.length ; i++) {
aaliveNeighbours[i].classList.remove("alive");
}
}
}
function change() {
let cellschanged = [];
let arrofRows = grid.children;
for (let rowIndx = 1 ; rowIndx < arrofRows.length; rowIndx++) {
let curRow = arrofRows[rowIndx].children;
let rowBefore = [];
let rowAfter = [];
// checking for valid rows
if (rowIndx - 1 >= 1) rowBefore = arrofRows[rowIndx - 1].children;
if (rowIndx + 1 < arrofRows.length) rowAfter = arrofRows[rowIndx + 1].children;
for (let cell = 0 ; cell < curRow.length ; cell++) {
let validNeibours = [];
// checking for valid validNeibours
if (rowBefore.length>0) validNeibours.push(rowBefore[cell]);
if (rowAfter.length>0) validNeibours.push(rowAfter[cell]);
if (cell - 1 >= 0) {
validNeibours.push(curRow[cell - 1]);
if (rowBefore.length>0) validNeibours.push(rowBefore[cell - 1]);
if (rowAfter.length>0) validNeibours.push(rowAfter[cell - 1]);
}
if (cell + 1 < curRow.length) {
validNeibours.push(curRow[cell + 1]);
if (rowBefore.length>0) validNeibours.push(rowBefore[cell + 1]);
if (rowAfter.length>0) validNeibours.push(rowAfter[cell + 1]);
}
// counting alive neighbours
let aliveNeighboursNum = 0;
for (let neighbour = 0 ; neighbour < validNeibours.length ; neighbour++) {
if (validNeibours[neighbour].classList.contains("alive")) aliveNeighboursNum++;
}
// pushing cells that need to be changed in cellschanged array
if (curRow[cell].classList.contains("alive")) {
if (aliveNeighboursNum < 2 || aliveNeighboursNum > 3) {
cellschanged.push(curRow[cell]);
}
}
else {
if (aliveNeighboursNum == 3) {
cellschanged.push(curRow[cell]);
}
}
}
}
// actualy changing required cells
for (let i = 0 ; i < cellschanged.length ; i++) {
if (cellschanged[i].classList.contains("alive")) {
cellschanged[i].classList.remove("alive");
cellschanged[i].classList.add("dead");
}
else {
cellschanged[i].classList.remove("dead");
cellschanged[i].classList.add("alive");
}
}
}