-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConwayGameOfLifeScene.js
160 lines (137 loc) · 3.99 KB
/
ConwayGameOfLifeScene.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
class ConwayGameOfLifeScene extends Phaser.Scene {
constructor() {
super({ key: "ConwayGameOfLifeScene" });
this.timer = 0; // Initialize timer to keep track of time elapsed
this.delay = 10; // Delay in milliseconds (3 seconds in this example)
this.numRows = 200;
this.numCols = 200;
this.gridSize = 2;
this.gameGrid = [];
}
create() {
this.initGrid();
this.rectangleGroup = this.add.group();
this.drawGridOnCanvas();
}
initGrid() {
for (let y = 0; y < this.numRows; y++) {
let row = [];
for (let x = 0; x < this.numCols; x++) {
const value = Math.random() < 0.2;
row.push(value);
}
this.gameGrid.push(row);
}
}
drawGridOnCanvas() {
// remove all current rectangles
if (this.rectangleGroup !== undefined) {
this.rectangleGroup.clear(true, true);
}
for (let y = 0; y < this.numRows; y++) {
for (let x = 0; x < this.numRows; x++) {
if (this.gameGrid[x][y] === true) {
// create a rectangle at this location and add it to a group to keep track
this.rectangleGroup.add(
this.add.rectangle(
this.gridSize / 2 + x * this.gridSize,
this.gridSize / 2 + y * this.gridSize,
this.gridSize,
this.gridSize,
0xff0000
)
);
}
}
}
}
shouldBeAliveNextRound(x, y) {
/*
-1,-1 0,-1 +1,-1
-1,0 xxxx +1,0
-1,+1 0,+1 +1,+1
*/
// make sure the gameGrid is defined before checking any values within
// todo: this check may not be necesary
if (this.gameGrid === undefined) {
return false;
}
// bounds check the coordinates and return false if out of bounds
if (x < 0 || y < 0 || x >= this.numCols || y >= this.numRows) {
return false;
}
var shouldBeAliveNextRound = false;
var numberOfNeighbors = 0;
var thisCellIsAlive = this.gameGrid[x][y];
if (x > 0 && y > 0) {
if (this.gameGrid[x - 1][y - 1] === true) {
numberOfNeighbors += 1;
}
}
if (y > 0) {
if (this.gameGrid[x][y - 1] === true) {
numberOfNeighbors += 1;
}
}
if (x < this.numCols - 1 && y > 0) {
if (this.gameGrid[x + 1][y - 1] === true) {
numberOfNeighbors += 1;
}
}
if (x > 0) {
if (this.gameGrid[x - 1][y] === true) {
numberOfNeighbors += 1;
}
}
if (x < this.numCols - 1) {
if (this.gameGrid[x + 1][y] === true) {
numberOfNeighbors += 1;
}
}
if (x > 0 && y < this.numRows - 1) {
if (this.gameGrid[x - 1][y + 1] === true) {
numberOfNeighbors += 1;
}
}
if (y < this.numRows - 1) {
if (this.gameGrid[x][y + 1] === true) {
numberOfNeighbors += 1;
}
}
if (x < this.numCols - 1 && y < this.numRows - 1) {
if (this.gameGrid[x + 1][y + 1] === true) {
numberOfNeighbors += 1;
}
}
if (thisCellIsAlive && (numberOfNeighbors == 2 || numberOfNeighbors == 3)) {
shouldBeAliveNextRound = true;
}
if (!thisCellIsAlive && numberOfNeighbors == 3) {
shouldBeAliveNextRound = true;
}
return shouldBeAliveNextRound;
}
updateGrid() {
// create a deep copy of the gameGrid
var newGrid = JSON.parse(JSON.stringify(this.gameGrid));
// determine state of each cell in the grid and record in temp array
// do this so that each cell's next state is determined all in one tick
for (let y = 0; y < this.numRows; y++) {
for (let x = 0; x < this.numRows; x++) {
newGrid[x][y] = this.shouldBeAliveNextRound(x, y);
}
}
this.gameGrid = JSON.parse(JSON.stringify(newGrid));
this.drawGridOnCanvas();
}
update(time, delta) {
// Update timer with time elapsed since last frame
this.timer += delta;
// Check if the delay has elapsed
if (this.timer >= this.delay) {
this.updateGrid();
// Reset timer
this.timer -= this.delay;
}
}
}