forked from jeffrileyjr/GameOfLife
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
159 lines (146 loc) · 4.16 KB
/
Copy pathscript.js
File metadata and controls
159 lines (146 loc) · 4.16 KB
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
const columns = Math.round(Math.round(window.innerWidth) / 10);
const rows = Math.round(Math.round(window.innerHeight) / 10);
const btn = document.getElementById("mainBTN");
const infoBox = document.getElementById('modal')
let current = [rows];
let nextGen = [rows];
let speed = 500;
let gameStarted = false;
let interval;
createGameboard();
create2DArrays();
randomizeCells();
function showInfo() {
infoBox.classList.toggle("showModal")
}
function createGameboard() {
const grid = document.getElementById("gameboard");
for (let y = 0; y < rows; y++) {
let row = document.createElement("div");
row.classList.add("row");
grid.append(row);
for (x = 0; x < columns; x++) {
let cell = document.createElement("div");
cell.setAttribute("id", y + "_" + x);
cell.classList.add("cell");
row.append(cell);
}
}
}
function create2DArrays() {
for (let i = 0; i < rows; i++) {
current[i] = new Array(columns);
nextGen[i] = new Array(columns);
}
}
function randomizeCells() {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
nextGen[i][j] = 0;
current[i][j] = 0;
if (Math.random() < 0.4) current[i][j] = 1;
}
}
let randomCell = "";
for (row in current) {
for (column in current[row]) {
randomCell = document.getElementById(row + "_" + column);
if (current[row][column] == 1) {
randomCell.classList.add("alive");
}
}
}
}
function nextGeneration() {
for (row in current) {
for (column in current[row]) {
let neighbors = countNeighbors(row, column);
if (current[row][column] == 1) {
if (neighbors < 2) {
nextGen[row][column] = 0;
} else if (neighbors == 2 || neighbors == 3) {
nextGen[row][column] = 1;
} else if (neighbors > 3) {
nextGen[row][column] = 0;
}
} else if (current[row][column] == 0) {
if (neighbors == 3) {
nextGen[row][column] = 1;
}
}
}
}
updateCurrent();
updateBoard();
}
function updateCurrent() {
for (row in current) {
for (column in current[row]) {
current[row][column] = nextGen[row][column];
nextGen[row][column] = 0;
}
}
}
function updateBoard() {
let cell = "";
for (row in current) {
for (column in current[row]) {
cell = document.getElementById(row + "_" + column);
if (current[row][column] == 0) {
cell.classList.remove("alive");
} else {
cell.classList.add("alive");
}
}
}
}
function evolve() {
gameStarted = !gameStarted;
if (gameStarted) {
btn.innerText = 'Reset';
interval = setInterval(nextGeneration, speed);
} else {
btn.innerText = "Start";
clearInterval(interval);
create2DArrays();
randomizeCells();
}
}
function countNeighbors(row, col) {
let count = 0;
let numRow = Number(row);
let numCol = Number(col);
if (numRow - 1 >= 0) {
if (current[numRow - 1][numCol] == 1)
count++;
}
if (numRow - 1 >= 0 && numCol - 1 >= 0) {
if (current[numRow - 1][numCol - 1] == 1)
count++;
}
if (numRow - 1 >= 0 && numCol + 1 < columns - 1) {
if (current[numRow - 1][numCol + 1] == 1)
count++;
}
if (numCol - 1 >= 0) {
if (current[numRow][numCol - 1] == 1)
count++;
}
if (numCol + 1 < columns) {
if (current[numRow][numCol + 1] == 1)
count++;
}
if (numRow + 1 < rows && numCol - 1 >= 0) {
if (current[numRow + 1][numCol - 1] == 1)
count++;
}
if (numRow + 1 < rows && numCol + 1 < columns - 1) {
if (current[numRow + 1][numCol + 1] == 1)
count++;
}
if (numRow + 1 < rows) {
if (current[numRow + 1][numCol] == 1)
count++;
}
return count;
}