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
|
function countNeighbors(grid, x, y) {
// Define the offsets for the neighboring cells
const neighborOffsets = [
[-1, -1], [-1, 0], [-1, 1],
[0, -1], [0, 1],
[1, -1], [1, 0], [1, 1]
];
// Use reduce to iterate over the neighborOffsets and count the number of live neighbors
return neighborOffsets.reduce((count, [dx, dy]) => {
// Calculate the coordinates of the neighboring cell
const nx = x + dx;
const ny = y + dy;
// Check if the neighboring cell is within the grid boundaries and if it is alive
if (nx >= 0 && ny >= 0 && nx < grid.length && ny < grid[0].length && grid[nx][ny]) {
// If the neighboring cell is alive, increment the count
return count + 1;
}
// If the neighboring cell is not alive or is outside the grid boundaries, return the current count
return count;
}, 0);
}
function step(grid) {
// Use map to iterate over each cell in the grid and calculate the next state based on the rules of the game
return grid.map((row, x) =>
row.map((cell, y) => {
// Count the number of live neighbors for the current cell
const neighbors = countNeighbors(grid, x, y);
// Apply the rules of the game to determine the next state of the cell
// If the cell has 3 live neighbors or has 2 live neighbors and is already alive, it stays alive
// Otherwise, it becomes dead
return neighbors === 3 || (neighbors === 2 && cell) ? 1 : 0;
})
);
}
function printGrid(grid) {
grid.forEach(row => console.log(row.map(cell => cell ? '🟢' : '⚪️').join('')));
console.log('\n');
}
function simulate(initial, steps) {
let grid = initial;
Array.from({ length: steps }).forEach(() => {
printGrid(grid);
grid = step(grid);
});
}
const initialBoard = [
[0, 1, 0],
[0, 0, 1],
[1, 1, 1],
[0, 0, 0],
];
simulate(initialBoard, 10);
const rpentomino = [
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];
// simulate(rpentomino, 10);
// big glider
const bigGlider = [
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
];
// simulate(bigGlider, 10);
const randomBoard = Array.from({ length: 22 }, () =>
Array.from({ length: 22 }, () => Math.round(Math.random()))
);
simulate(randomBoard, 50);
|