-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgol.php
More file actions
executable file
·75 lines (68 loc) · 1.91 KB
/
cgol.php
File metadata and controls
executable file
·75 lines (68 loc) · 1.91 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
#!/usr/bin/env php
<?php
$width = 50;
$height = 30;
$density = 0.2;
// Function to initialize the grid with random alive (true) or dead (false) cells
function initializeGrid() {
global $width, $height, $density;
$grid = [];
for ($y = 0; $y < $height; $y++) {
$grid[$y] = [];
for ($x = 0; $x < $width; $x++) {
$grid[$y][$x] = mt_rand() / mt_getrandmax() < $density;
}
}
return $grid;
}
// Function to count the alive neighbors around a given cell
function countNeighbors($grid, $x, $y) {
global $width, $height;
$count = 0;
for ($dx = -1; $dx <= 1; $dx++) {
for ($dy = -1; $dy <= 1; $dy++) {
if ($dx == 0 && $dy == 0) continue;
$nx = ($x + $dx + $width) % $width;
$ny = ($y + $dy + $height) % $height;
if ($grid[$ny][$nx]) {
$count++;
}
}
}
return $count;
}
// Function to compute the next generation of the grid
function computeNextState($grid) {
global $width, $height;
$newGrid = [];
for ($y = 0; $y < $height; $y++) {
$newGrid[$y] = [];
for ($x = 0; $x < $width; $x++) {
$neighbors = countNeighbors($grid, $x, $y);
if ($grid[$y][$x]) {
$newGrid[$y][$x] = ($neighbors == 2 || $neighbors == 3);
} else {
$newGrid[$y][$x] = ($neighbors == 3);
}
}
}
return $newGrid;
}
// Function to print the grid to the console
function printGrid($grid) {
global $width, $height;
for ($y = 0; $y < $height; $y++) {
for ($x = 0; $x < $width; $x++) {
echo $grid[$y][$x] ? '█' : ' ';
}
echo PHP_EOL;
}
}
$grid = initializeGrid();
while (true) {
printGrid($grid);
$grid = computeNextState($grid);
usleep(100000); // Sleep for 100 ms
system('clear'); // Clear the console (Linux/macOS) or use 'cls' on Windows
}
?>