Personal Contribution : Game of Life

This is the simplest example how a Game of Life can be implemented. The game features three set of cells that continue to move until they are out of screen.

https://en.wikipedia.org/wiki/Conway's_Game_of_Life

Hope it helps someone who are interested in this game.

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <thread>
#include <chrono>

const int MAX_BOARD = 300;

void delay(int milliseconds)
{
	std::this_thread::sleep_for(std::chrono::duration<unsigned int, std::milli> (milliseconds));
}

class Cell
{
	bool state;
	public : 

	Cell() {state = false;}
	~Cell() {state = false;}

	bool getState() {return state;}
	void setState(bool _state) {state = _state;}

	char getChar() {if(state) return 'o'; else return '-';}
};

class Board
{
	int viewX, viewY;
	int maxBoard;

	public : 
	Cell currentLife[MAX_BOARD][MAX_BOARD];

	Board(int maxBoard = MAX_BOARD, int viewX = 0, int viewY = 0) 
	{
		Board::maxBoard = maxBoard;

		if(Board::maxBoard < 4) Board::maxBoard = 4;
		if(Board::maxBoard > MAX_BOARD) Board::maxBoard = MAX_BOARD;

		setViewX(viewX);
		setViewY(viewY);
	}

	void setViewX(int newViewX) 
	{ 
		if(newViewX >= 0 && newViewX <= MAX_BOARD - maxBoard) viewX = newViewX;
		else 
		{
			throw("Invalid X view for Board");
		}
	}

	void setViewY(int newViewY) 
	{ 
		if(newViewY >= 0 && newViewY <= MAX_BOARD - maxBoard) viewY = newViewY;
		else 
		{
			throw("Invalid Y view for Board");
		}
	}

	void update()
	{
		Board backup_board = (*this);
		int i, j;
		
		for(i = 0; i < MAX_BOARD; i++)
		{
			for(j = 0; j < MAX_BOARD; j++)
			{
				int countAlive = backup_board.countNeighborhood(i, j);
				currentLife[i][j].setState(executeRules(countAlive, currentLife[i][j].getState()));
			}
		}
	}

	void display()
	{
		int i, j;
		for(i = viewY; i < viewY + maxBoard; i++)
		{
			std::cout << "  ";

			for(j = viewX; j < viewX + maxBoard; j++)
			{
				std::cout << currentLife[i][j].getChar();
			}

			std::cout << std::endl;
		}
	}

	private : 

	bool executeRules(unsigned int countAlive, bool currentState)
	{
		if(currentState == true)
		{
			// Any live cell with fewer than two live neighbours dies, as if caused by under-population.
			if(countAlive < 2) return false; 	

			// Any live cell with two or three live neighbours lives on to the next generation.
			if(countAlive == 2 || countAlive == 3) return true;

			// Any live cell with more than three live neighbours dies, as if by overcrowding.
			if(countAlive > 3) return false;
		}
		else
		{
			// Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
			if(countAlive == 3) return true;
			else 
			return false;
		}
	}

	int countNeighborhood(int i, int j) // i is row, j is column
	{
		int count = 0;

		if(i > 0 && j > 0) 
			if(currentLife[i - 1][j - 1].getState() == true) count++;

		if(i > 0) 
			if(currentLife[i - 1][j].getState() == true) count++;

		if(i > 0 && j < MAX_BOARD - 1) 
			if(currentLife[i - 1][j + 1].getState() == true) count++;

		if(j > 0) 
			if(currentLife[i][j - 1].getState() == true) count++;

		if(j > 0 && i < MAX_BOARD - 1) 
			if(currentLife[i + 1][j - 1].getState() == true) count++;

		if(i < MAX_BOARD - 1) 
			if(currentLife[i + 1][j].getState() == true) count++;
	
		if(i < MAX_BOARD - 1 && j < MAX_BOARD - 1) 
			if(currentLife[i + 1][j + 1].getState() == true) count++;

		if(j < MAX_BOARD - 1) 
			if(currentLife[i][j + 1].getState() == true) count++;	

		return count;
	}
};


int main()
{
	Board board(24, 20, 20);

	// First set of cells
	board.currentLife[0 + 2][1 + 2].setState(true);
	board.currentLife[1 + 2][2 + 2].setState(true);
	board.currentLife[2 + 2][0 + 2].setState(true);
	board.currentLife[2 + 2][1 + 2].setState(true);
	board.currentLife[2 + 2][2 + 2].setState(true);

	// Second set of cells
	board.currentLife[0 + 12][1 + 12].setState(true);
	board.currentLife[1 + 12][2 + 12].setState(true);
	board.currentLife[2 + 12][0 + 12].setState(true);
	board.currentLife[2 + 12][1 + 12].setState(true);
	board.currentLife[2 + 12][2 + 12].setState(true);

	// Third set of cells
	board.currentLife[0 + 28][1 + 28].setState(true);
	board.currentLife[1 + 28][2 + 28].setState(true);
	board.currentLife[2 + 28][0 + 28].setState(true);
	board.currentLife[2 + 28][1 + 28].setState(true);
	board.currentLife[2 + 28][2 + 28].setState(true);
	
	while(true)
	{
		system("cls");
		board.display();
		board.update();
		delay(400);
	}

	return 0;
}
Last edited on
With little bit modification, it is even possible to recreate the sample "single Gosper's glider gun" without problem. Trust me, it only takes roughly 30 minutes to do it.

https://upload.wikimedia.org/wikipedia/commons/e/e5/Gospers_glider_gun.gif

Good luck!
Topic archived. No new replies allowed.