Conway's The Game of Life

Hello everyone, I finished writing the code for Conway's game of life. I used 0s as empty lives and 1s as cells with lives. I'm not sure If it's correct or not determining from the output because of the results. Could someone please look over my code.

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
#include<iostream>
#include<iomanip>
#include<ctime>
using namespace std;

#define ROWS 12 // Definted amount of rows
#define COLS 12 // Defined amount of columns

int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols);
void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count);
void generateArray(int world[ROWS][COLS], int& rows, int& cols);

int main()
{
	int world[ROWS][COLS]={0}; // Original World/ Also helps print border of zeroes
	int world_2[ROWS][COLS]={0}; //New world generated 
	int rows = 12;
	int cols = 12;
	int n_count = 0;
	cout << "Welcome to The Game of Life!" << endl;
	generateArray(world, rows, cols);
	display(world, world_2, rows, cols);
	cout << "This is the original world generated with 0s as no life and 1s as a single life";
	scanArray(world, rows, cols, n_count);
	world_2[ROWS][COLS] = world[ROWS][COLS];
	display(world, world_2, rows, cols);
	system("pause");

	return 0;
}

void generateArray(int world[ROWS][COLS], int& rows, int& cols)
{
	for(int row = 1; row < rows-1; row++) //Minus one and set equal to one so that broder is not included.
	{
		for(int col = 1; col < cols-1; col++)
		{
			world[row][col] = rand()%2;
			
		}
	}
}

void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols)
{
	for(int row = 0; row < rows; row++)
	{
		for(int col = 0; col < cols; col++)
		{
			cout << setw(5) << world[row][col];
		}
		cout << endl;
	}
	cout << endl;
}

void scanArray(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
{
	int neighbor_count = 0;
	for(int row = 1; row < rows-1; row++)
	{
		for(int col = 1; col < cols-1; col++)
		{
			neighbor_count = generation(world, rows, cols, n_count);
			if(world[row][col] == 1){
				if(neighbor_count == 1 || neighbor_count == 0)
					world[col][row] = 0;
				if(neighbor_count >= 4)
					world[col][row] = 0;
				if(neighbor_count == 2 || neighbor_count == 3)
					world[col][row] = 1;
				}
			if(world[row][col] == 0){
				if(neighbor_count == 3)
					world[row][col] = 1;
			}

		}
	}
}

int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
{
	
	if(world[cols][rows-1] == 1) // The cell above
	n_count++;
	if(world[cols-1][rows] == 1) // The cell to the left
		n_count++;
	if(world[cols+1][rows] == 1) // The cell to the right
		n_count++;
	if(world[cols][rows+1] == 1) // The cell below
		n_count++;
	if(world[cols-1][rows-1] == 1) // The cell diagonally upper left
		n_count++;
	if(world[cols-1][rows+1] == 1) // The cell diagonally lower left
		n_count++;
	if(world[cols+1][rows-1] == 1) // The cell diagonally upper right
		n_count++;
	if(world[cols+1][rows+1] == 1) // The cell diagonally lower right
		n_count++;

	return n_count++;

}

													


			
Any help would be greatly appreciated.
world_2[ROWS][COLS] = world[ROWS][COLS];
That is not how you copy an array.
Either iterate the array an copy element by element or check memcpy http://www.cplusplus.com/reference/clibrary/cstring/memcpy/

void display(int world[ROWS][COLS], int world_2[ROWS][COLS], int& rows, int& cols)
You aren't using world_2 in that function, why do you pass it?
Also, there is no need in pass by reference rows and cols (you aren't changing them)

int generation(int world[ROWS][COLS], int& rows, int& cols, int& n_count)
You are changing n_count, but I think you shouldn't (you are already returning its value).
And please use a loop, it is hard to debug as it is.
Last edited on
A few thoughts:
Your variable names are not accurate. rows and cols should be y and x, respectively.
generation() should be named "count_neighbours" or something.
scanArray should be named "generation".
Topic archived. No new replies allowed.