This repository has been archived by the owner on May 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_script.js
132 lines (112 loc) · 2.89 KB
/
old_script.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
const cols = 40;
const rows = 20;
const H_PAGE_PERCENT = 90;
const V_PAGE_PERCENT = 100;
var cells = [];
// Builds table on page load
function build(table) {
for (var r = 0; r < rows; r++) {
let row = table.insertRow();
for (var c = 0; c < cols; c++) {
let cell = row.insertCell();
// Build cell
let id = r + "." + c;
cell.setAttribute("id", id);
cell.setAttribute("class", "dead");
cell.onclick = function () {
toggleCell(id);
};
// let text = document.createTextNode(id);
// cell.appendChild(text);
cells.push(cell);
}
}
}
function toggleCell(id) {
let cell = document.getElementById(id);
if (alive(id)) {
cell.setAttribute("class", "dead");
} else if (!alive(id)) {
cell.setAttribute("class", "alive");
}
}
function killCell(id) {
let cell = document.getElementById(id);
cell.setAttribute("class", "dead");
}
function clearScreen() {
// console.log("hi");
for (c of cells) {
let id = c.getAttribute("id");
killCell(id);
}
}
// function birthCell(id) {
// let cell = document.getElementById(id);
// cell.setAttribute("class", "alive");
// }
function alive(id) {
let cell = document.getElementById(id);
if (cell.getAttribute("class") == "alive") {
return true;
} else if (cell.getAttribute("class") == "dead") {
return false;
} else {
console.log("error! null cell class");
return null;
}
}
function stepGame() {
var lifeMap = [];
// Build life map
for (var r = 0; r < rows; r++) {
lifeMap[r] = [];
for (var c = 0; c < cols; c++) {
let id = r + "." + c;
lifeMap[r][c] = alive(id);
}
}
//Run game
gameOfLife(lifeMap);
}
function gameOfLife(lifeMap) {
// Iterate through all cells
for (var r = 0; r < rows; r++) {
for (var c = 0; c < cols; c++) {
// Cell id
let id = r + "." + c;
// Count alive neighbors
let neighbors = countNeighbors(r, c, lifeMap, alive(id));
if (neighbors > 0) {
console.log(neighbors);
}
// Apply Conway's game of life rules
if (alive(id) && (neighbors < 2 || neighbors > 3)) {
toggleCell(id);
} else if (!alive(id) && neighbors == 3) {
toggleCell(id);
}
}
}
}
function countNeighbors(r, c, lifeMap, alive) {
// Bounds for neighbor calculations
let bounds = [
r > 0 ? r - 1 : 0,
c > 0 ? c - 1 : 0,
r < rows - 1 ? r + 1 : rows - 1,
c < cols - 1 ? c + 1 : cols - 1,
];
// If alive, decrease sum by 1 to not include self
var sum = alive ? -1 : 0;
// Iterates through range of neighbor bounds, increments sum if any cell is alive within range.
for (var r = bounds[0]; r <= bounds[2]; r++) {
for (var c = bounds[1]; c <= bounds[3]; c++) {
sum += lifeMap[r][c] ? 1 : 0;
}
}
return sum;
}
// Build table
let table = document.getElementById("bodyTable"); // id table body table
build(table);