-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgol.js
221 lines (171 loc) · 5 KB
/
gol.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
210
211
212
213
214
215
216
217
218
219
220
221
var rowCount = 0;
var colCount = 0
var timeStepSeconds = 1;
var cells;
var aliveBuffer;
var tableBody;
var rowCountInput;
var colCountInput;
var timeStepSecondsInput;
var densityInput;
var play;
var pause;
var step;
var livingColor = '#000000';
var deadColor = '#ffffff';
var intervalId;
addEventListener("load", (ev) => {
tableBody = document.getElementById('gol-table-body');
rowCountInput = document.getElementById('row-count');
colCountInput = document.getElementById('col-count');
timeStepSecondsInput = document.getElementById('time-step');
densityInput = document.getElementById('density');
play = document.getElementById('play');
pause = document.getElementById('pause');
step = document.getElementById('step');
pause.disabled = true;
handleSettingsChange();
initTable();
});
function initTable()
{
tableBody.innerHTML = '';
cells = [];
aliveBuffer = [];
for (var i = 0; i < rowCount; i++) {
aliveBuffer.push(new Array(colCount).fill(0));
var cellArr = [];
cells.push(cellArr);
var row = tableBody.insertRow(i);
for (var j = 0; j < colCount; j++) {
var cell = row.insertCell(j);
cellArr.push(cell);
cell.style.backgroundColor = deadColor;
cell.onmousedown = (event) => handleCellClick(event);
cell.onmouseover = (event) => handleDrag(event);
}
}
}
function handleCellClick(event) {
if (event.buttons == 1) {
var cell = event.srcElement;
var row = cell.parentElement;
var i = row.rowIndex;
var j = cell.cellIndex;
var wasAlive = aliveBuffer[i][j];
aliveBuffer[i][j] = wasAlive ? 0 : 1;
cell.style.backgroundColor = wasAlive ? deadColor: livingColor;
}
}
function handleDrag(event) {
if (event.buttons == 1) {
var cell = event.srcElement;
var row = cell.parentElement;
var i = row.rowIndex;
var j = cell.cellIndex;
aliveBuffer[i][j] = 1;
cell.style.backgroundColor = livingColor;
}
}
function handleSettingsChange() {
handlePauseClick();
rowCount = rowCountInput.value;
colCount = colCountInput.value;
timeStepSeconds = timeStepSecondsInput.value;
var wasPlaying = intervalId != null;
if (wasPlaying)
{
handlePauseClick();
handlePlayClick();
}
}
function tick() {
var nextState = [];
for (var i = 0; i < rowCount; i++) {
var row = tableBody.rows.item(i);
nextState.push(new Array(colCount).fill(0));
for (var j = 0; j < colCount; j++) {
nextState[i][j] = getNextState(i, j);
var cell = row.cells.item(j);
cell.style.backgroundColor = nextState[i][j] ? livingColor: deadColor;
}
}
aliveBuffer = nextState;
}
function getNextState(rowIndex, colIndex) {
var currentlyAlive = aliveBuffer[rowIndex][colIndex];
var livingNeighbors = 0;
for (var i = rowIndex - 1; i <= rowIndex + 1; i++)
{
if (i < 0 || i > rowCount - 1) continue;
for (var j = colIndex - 1; j <= colIndex + 1; j++)
{
if (j < 0 || j > colCount - 1 || (i == rowIndex && j == colIndex)) continue;
if (aliveBuffer[i][j]) {
livingNeighbors++;
if (livingNeighbors > 3) return 0;
}
}
}
if (livingNeighbors < 2 || livingNeighbors > 3) {
return 0;
}
if (livingNeighbors == 3 || (livingNeighbors == 2 && currentlyAlive))
{
return 1;
}
return 0;
}
function handlePlayClick() {
play.disabled = true;
step.disabled = true;
if (intervalId == null) {
tick();
intervalId = setInterval(() => tick(), timeStepSeconds * 1000);
pause.disabled = false;
}
}
function handlePauseClick() {
pause.disabled = true;
if (intervalId != null)
{
clearInterval(intervalId);
intervalId = null;
play.disabled = false;
step.disabled = false;
}
}
function handleStepClick() {
if (intervalId == null)
{
tick();
}
}
function handleClearClick() {
handlePauseClick();
aliveBuffer = [];
for (var i = 0; i < rowCount; i++) {
var row = tableBody.rows.item(i);
aliveBuffer.push(new Array(colCount).fill(0));
for (var j = 0; j < colCount; j++) {
var cell = row.cells.item(j);
cell.style.backgroundColor = deadColor;
}
}
}
function handleRandomizeClick() {
handlePauseClick();
aliveBuffer = [];
var density = densityInput.value / 100;
for (var i = 0; i < rowCount; i++) {
var row = tableBody.rows.item(i);
aliveBuffer.push(new Array(colCount).fill(0));
for (var j = 0; j < colCount; j++) {
var random = Math.random();
var alive = random < density;
aliveBuffer[i][j] = alive;
var cell = row.cells.item(j);
cell.style.backgroundColor = alive ? livingColor : deadColor;
}
}
}