-
Notifications
You must be signed in to change notification settings - Fork 2
/
life.html
247 lines (232 loc) · 6.96 KB
/
life.html
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Conway's Game of Life</title>
<style>
body {
/* font-family: Arial, sans-serif; */
/* text-align: center; */
margin: 0;
padding: 0;
height: 100%;
width: 100%;
display: flex;
/* flex-direction: column; */
justify-content: center;
align-items: center;
}
.cell {
background-color: grey;
width: 100%;
height: 100%;
}
.cell.alive {
background-color: rgb(0, 0, 255);
}
.cell.dead {
background-color: wheat;
}
#grid-container {
display: grid;
grid-template-columns: repeat(44, 1fr);
grid-template-rows: repeat(44, 1fr);
width: 100vw;
height: 100vh;
gap: 0px;
margin: 0;
border: 0.5px solid #ccc;
box-sizing: border-box;
}
</style>
</head>
<body>
<div id="grid-container"></div>
<script>
const cols = 44;
const rows = 44;
let clickCount = 0;
let iClicked = -1;
let jClicked = -1;
let changes = [];
const gridContainer = document.getElementById('grid-container');
let grid = createEmptyGrid();
let interval;
gridContainer.addEventListener("click", (ev) => {
clickCount++;
const rect = gridContainer.getBoundingClientRect();
const x = ev.clientX - rect.left;
const y = ev.clientY - rect.top;
const cellWidth = gridContainer.offsetWidth / cols;
const cellHeight = gridContainer.offsetHeight / rows;
jClicked = Math.floor(x / cellWidth);
iClicked = Math.floor(y / cellHeight);
console.log("onClick: iClicked, jClicked=", iClicked, jClicked);
if (clickCount % 2 === 1) {
stopGame();
} else {
start();
}
});
function createEmptyGrid() {
return Array.from({ length: rows }, () => Array.from({ length: cols }, () => 0));
}
function aliveProb(i, j) {
if (iClicked < 0) {
p = 0.5
} else {
p = ((i - iClicked)**2 + (j - jClicked)**2) / (cols**2 + rows**2)
}
// console.log("aliveProb: i, j, iClicked, jClicked, p=", i, j, iClicked, jClicked, p);
// return 0.5 + (0.5-p)/2;
return 0.5;
}
function initializeGrid() {
gridContainer.innerHTML = '';
// const rmin = rows/2 - 5;
// const rmax = rows/2 + 5;
// const cmin = cols/2 - 5;
// const cmax = cols/2 + 5;
const r0 = rows/2;
const c0 = cols/2;
const r2 = 11**2;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
const p = aliveProb(i, j);
// if ((rmin < i) && (i < rmax) && (cmin < j) && (j < cmax)) {
if ((i-r0)**2 + (j-c0)**2 < r2) {
grid[i][j] = Math.random() < p ? 1 : 0;
// grid[i][j] = 1;
} else {
grid[i][j] = 0;
}
gridContainer.appendChild(cell);
}
}
renderGrid();
}
function renderGrid() {
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const cell = gridContainer.children[i * cols + j];
cell.className = grid[i][j] ? 'cell alive' : 'cell dead';
}
}
}
function startGame() {
interval = setInterval(updateGrid, 150);
}
function stopGame() {
clearInterval(interval);
}
function old_updateGrid() {
const newGrid = createEmptyGrid();
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const neighbors = countNeighbors(i, j);
if (grid[i][j] === 1) {
if (neighbors < 2 || neighbors > 3) {
newGrid[i][j] = 0;
} else {
newGrid[i][j] = 1;
}
} else {
if (neighbors === 3) {
newGrid[i][j] = 1;
} else {
newGrid[i][j] = 0;
}
}
}
}
grid = newGrid;
renderGrid();
}
function updateGrid() {
const newGrid = createEmptyGrid();
let change = 0; // count how many cells changed value
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const n = countNeighbors(i, j);
let nij = 0;
if (grid[i][j] === 1) {
nij = (n < 2 || n > 3) ? 0 : 1;
} else {
nij = (n === 3) ? 1 : 0;
}
newGrid[i][j] = nij;
change += (nij === grid[i][j]) ? 0 : 1;
}
}
const maxlen = 32;
changes.push(change); // store change in changes array
if (changes.length > maxlen) {
changes.shift()
}
// console.log(`updateGrid: change/(rows*cols)= ${change/(rows*cols)}`);
// console.log(`updateGrid: changes= ${changes}`);
grid = newGrid;
renderGrid();
// if ((changes.length === maxlen) && (Math.min(...changes) === Math.max(...changes))) {
// start new game if changes array reached maxlen length and it is constant
if ((changes.length === maxlen) && (new Set(changes)).size === 1) {
// console.log(`updateGrid: new game! changes= ${changes}`);
start();
}
}
// wrap-around add function for counting neighbors
// i.e.
// if (i > 0) && (i < imax) neighbors are i-1 and i+1
// if i === 0 neighbors are imax-1 and i+1
// if i === imax-1 neighbors are i-1 and 0
// function add(ix, i, imax) {
// let v = ix + i;
// v = (v < 0) ? imax-1 : v;
// v = (v > imax-1) ? 0 : v;
// return v;
// }
// wrap-around nbor utility function for countNeighbors()
function nbor(ix, i, imax) {
let v = ix + i;
v = (v < 0) ? imax-1 : v;
v = (v > imax-1) ? 0 : v;
return v;
}
// counts "alive" naighbors i.e. with grid value == 1
function countNeighbors(row, col) {
let count = 0;
// for (let i = -1; i <= 1; i++) {
// for (let j = -1; j <= 1; j++) {
// if (i === 0 && j === 0) continue;
// // const newRow = row + i;
// // const newCol = col + j;
// // if (newRow >= 0 && newRow < rows && newCol >= 0 && newCol < cols) {
// // count += grid[newRow][newCol];
// // }
// const newRow = add(row, i, rows);
// const newCol = add(col, j, cols);
// count += grid[newRow][newCol];
// }
// }
[[-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1]].forEach(([i,j]) => {
count += grid[nbor(row, i, rows)][nbor(col, j, cols)];
});
return count;
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function start() {
stopGame();
initializeGrid();
await sleep(1000); // show initial grid for 1 sec
startGame();
}
// Start the game when the page loads
start();
</script>
</body>
</html>