-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.php
139 lines (126 loc) · 3.71 KB
/
gameoflife.php
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
<?php
class GameOfLife {
private $grid;
private $rows;
private $cols;
public function __construct($rows, $cols) {
$this->rows = $rows;
$this->cols = $cols;
$this->grid = array();
$this->initializeGrid();
}
private function initializeGrid() {
for ($i = 0; $i < $this->rows; $i++) {
for ($j = 0; $j < $this->cols; $j++) {
$this->grid[$i][$j] = rand(0, 1);
}
}
}
public function getGrid() {
return $this->grid;
}
private function countNeighbors($row, $col) {
$count = 0;
for ($i = -1; $i <= 1; $i++) {
for ($j = -1; $j <= 1; $j++) {
if ($i == 0 && $j == 0) continue;
$newRow = ($row + $i + $this->rows) % $this->rows;
$newCol = ($col + $j + $this->cols) % $this->cols;
$count += $this->grid[$newRow][$newCol];
}
}
return $count;
}
public function nextGeneration() {
$newGrind = array();
for ($i = 0; $i < $this->rows; $i++) {
for ($j = 0; $j < $this->cols; $j++) {
$neighbors = $this->countNeighbors($i, $j);
$newGrid[$i][$j] = $this->grid[$i][$j];
if ($this->grid[$i][$j] == 1) {
if ($neighbors < 2 || $neighbors > 3) {
$newGrid[$i][$j] = 0;
}
} else {
if ($neighbors == 3) {
$newGrid[$i][$j] = 1;
}
}
}
}
$this->grid = $newGrid;
}
}
session_start();
if (!isset($_SESSION['game']) || isset($_POST['reset'])) {
$_SESSION['game'] = new GameOfLife(60, 60);
}
if (isset($_POST['next'])) {
$_SESSION['game']->nextGeneration();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Game of Life in 20 mins</title>
<style>
.grid {
display: grid;
grid-template-columns: repeat(30, 20px);
gap: 1px;
background-color: #ccc;
padding: 10px;
margin: 20px auto;
width: fit-content;
}
.cell {
width: 20px;
height: 20px;
background-color: white;
}
.alive {
background-color: black;
}
.controls {
text-align: center;
margin: 20px;
}
button {
padding: 10px 20px;
margin: 0 10px;
font-size: 16px;
cursor: pointer;
}
h1 {
text-align: center;
color: #333;
}
</style>
</head>
<body>
<h1>Game of Life in 20mins</h1>
<div class="grid">
<?php
$grid = $_SESSION['game']->getGrid();
for ($i = 0; $i < 30; $i++) {
for ($j = 0; $j < 30; $j++) {
echo '<div class="cell ' . ($grid[$i][$j] ? 'alive' : '') . '"></div>';
}
}
?>
</div>
<div class="controls">
<form method="post">
<button type="submit" name="next">Click me to fuck react</button>
<button type="submit" name="reset">Click me to destory JS</button>
</form>
</div>
<div style="text-align: center; margin-top: 20px;">
<h3>Rules:</h3>
<p>1. Any live cell with fewer than two live neighbors dies (underpopulation)</p>
<p>2. Any live cell with two or three live neighbors lives on to the next generation</p>
<p>3. Any live cell with more than three live neighbors dies (overpopulation)</p>
<p>4. Any dead cell with exactly three live neighbors becomes a live cell (reproduction)</p>
</div>
</body>
</html>