/
gameoflife.pde
161 lines (127 loc) · 4.03 KB
/
gameoflife.pde
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
World world = new World(25, 25);
void setup() {
size(400,400);
//world.initializeSpecific();
world.initialize();
}
void draw() {
background(0);
world.draw();
world.updateState();
}
class World {
int rows;
int columns;
Cell[][] map;
World(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.map = new Cell[this.columns][this.rows];
}
void initialise() {
boolean isAlive = false;
for (int i = 0; i < this.columns; i++) {
for (int j = 0; j < this.rows; j++) {
// Initialize each object
isAlive = int(random(2)) == 1 ? true : false;
map[i][j] = new Cell(i*10, j*10, 10, 10, isAlive);
}
}
}
void initialiseSpecific() {
for (int i = 0; i < this.columns; i++) {
for (int j = 0; j < this.rows; j++) {
// Initialize each object
map[i][j] = new Cell(i*10, j*10, 10, 10, false);
}
}
map[13][13].isAlive = true;
map[13][14].isAlive = true;
map[13][15].isAlive = true;
}
void draw() {
for (int i = 0; i < this.columns; i++) {
for (int j = 0; j < this.rows; j++) {
map[i][j].display();
}
}
}
void updateState() {
Cell [][] nextMapState = new Cell[this.columns][this.rows];
for (int i = 0; i < this.columns; i++) {
for (int j = 0; j < this.rows; j++) {
// Initialize each object
nextMapState[i][j] = new Cell(i*10, j*10, 10, 10, false);
}
}
int liveNeighbourCount = 0;
for (int i = 0; i < this.columns; i++) {
for (int j = 0; j < this.rows; j++) {
liveNeighbourCount = this.getLiveNeighbourCount( i, j);
// Check status based on 4 rules:
// 1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
if (liveNeighbourCount < 2) {
nextMapState[i][j].isAlive = false;
// 2. Any live cell with two or three live neighbours lives on to the next generation.
}else if (map[i][j].isAlive == true && (liveNeighbourCount == 2 || liveNeighbourCount == 3)){
nextMapState[i][j].isAlive = true;}
// 3. Any live cell with more than three live neighbours dies, as if by overcrowding.
else if (map[i][j].isAlive == true && liveNeighbourCount > 3){
nextMapState[i][j].isAlive = false;}
// 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
else if (map[i][j].isAlive == false && liveNeighbourCount == 3){
nextMapState[i][j].isAlive = true;}
}
}
this.map = nextMapState;
}
int getLiveNeighbourCount(int col, int row) {
int count = 0;
//North-West
if (row != 0 && col != 0 && this.map[col-1][row-1].isAlive)
count++;
//North
if (row != 0 && this.map[col][row-1].isAlive)
count++;
//North-East
if (row != 0 && col != (this.columns - 1) && this.map[col+1][row-1].isAlive)
count++;
//East
if (col != (this.columns - 1) && this.map[col+1][row].isAlive)
count++;
//South-East
if (row != (this.rows - 1) && col != (this.columns - 1) && this.map[col+1][row+1].isAlive)
count++;
//South
if (row != (this.rows - 1) && this.map[col][row+1].isAlive)
count++;
//South-West
if (row != (this.rows -1) && col != 0 && this.map[col-1][row+1].isAlive)
count++;
//West
if (col != 0 && this.map[col-1][row].isAlive)
count++;
return count;
}
class Cell {
float x,y; // x,y location
float width, height;
boolean isAlive; // is this organism alive
Cell(float x, float y, float width, float height, boolean isAlive) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.isAlive = isAlive;
}
void display() {
// Color the cells black if alive or white if dead.
stroke(255);
if(this.isAlive)
fill(0);
else
fill(255);
rect(x,y,w,h);
}
}
}