Skip to content

Commit 132f791

Browse files
Arpit | GameOfLife directory | Added Game of Life project
1 parent 372a47d commit 132f791

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
lines changed

GameOfLife/README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Game Of Life
2+
3+
### Description
4+
5+
A less than 100 line of code simulation of the [Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) described by John Conway.
6+
The initial parameters are random, thus, you get different views each time you simulate the environment.
7+
8+
### Author
9+
10+
[Arpit Omprakash](https://github.com/aceking007)
11+
12+
### How to use
13+
14+
Install [Processing3](https://processing.org/download/).
15+
Download the source code provided in the folder named game_of_life.
16+
Run the `game_of_life.pde` file.
17+
Enjoy!
18+
19+
### Screenshots of the simulation
20+
21+
![First-Screenshot](/images/1.JPG)
22+
23+
![Second-Screenshot](/images/2.JPG)
24+
25+
![Third-Screenshot](/images/3.JPG)
26+
27+
![Fourth-Screenshot](/images/4.JPG)

GameOfLife/game_of_life/GOL.pde

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class GOL{
2+
3+
int w = 10;
4+
int columns, rows;
5+
6+
int[][] board;
7+
8+
GOL(){
9+
columns = width/w;
10+
rows = height/w;
11+
board = new int[columns][rows];
12+
init();
13+
}
14+
15+
void init(){
16+
for(int i = 0; i < columns - 1; i++){
17+
for(int j = 0; j < rows - 1; j++){
18+
board[i][j] = int(random(2));
19+
}
20+
}
21+
}
22+
23+
void generate(){
24+
int[][] next = new int[columns][rows];
25+
26+
for(int x = 1; x < columns - 1; x++){
27+
for(int y = 1; y < rows - 1; y++){
28+
29+
int neighbors = 0;
30+
for(int i = -1; i <= 1; i++){
31+
for(int j = -1; j <= 1; j++){
32+
neighbors += board[x + i][y + j];
33+
}
34+
}
35+
36+
neighbors -= board[x][y];
37+
38+
if ((board[x][y] == 1) && (neighbors < 2)) next[x][y] = 0;
39+
else if ((board[x][y] == 1) && (neighbors > 3)) next[x][y] = 0;
40+
else if ((board[x][y] == 0) && (neighbors == 3)) next[x][y] = 1;
41+
else next[x][y] = board[x][y];
42+
}
43+
}
44+
45+
board = next;
46+
}
47+
48+
void render(){
49+
for(int i = 0; i < columns; i++){
50+
for(int j = 0; j < rows; j++){
51+
if(board[i][j] == 1){
52+
fill(0);
53+
} else {
54+
fill(255);
55+
}
56+
int x = i * w;
57+
int y = j * w;
58+
rect(x, y, w, w);
59+
}
60+
}
61+
}
62+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
GOL game;
2+
3+
void setup(){
4+
frameRate(10);
5+
size(800, 600);
6+
game = new GOL();
7+
}
8+
9+
void draw(){
10+
background(255);
11+
game.render();
12+
game.generate();
13+
}

GameOfLife/images/1.JPG

228 KB
Loading

GameOfLife/images/2.JPG

230 KB
Loading

GameOfLife/images/3.JPG

227 KB
Loading

GameOfLife/images/4.JPG

228 KB
Loading

0 commit comments

Comments
 (0)