Conway's Game of Life Code

Hey guys, I have this project as an assignment and I've run into a problem I just can't seem to solve. When I run the program, it does one iteration of all periods ('.'), which symbolize dead cells. No new cells are manipulated.


#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

//Global Variables
int sleep = 300;
int MAX_X = 49;
int MAX_Y = 29;
char arr[50][30];
char temp[50][30];


//Functions in the order they're used
void seed(char[50][30], char[50][30]);
void copy(char[50][30], char[50][30]);
void life(char[50][30], char[50][30]);
int same(char[50][30], char[50][30]);


int _tmain(int argc, _TCHAR* argv[])
{
string key;


//Intro
do
{
cout << "Welcome to the Game of Life Simulator. Press enter to continue.";
getline(cin, key);
} while (key == "\n");

life(arr, temp);

return 0;
}

void seed(char arr[50][30], char temp[50][30])
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
arr[x][y] = '.';
}
}

for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
temp[x][y] = '.';
}
}
}

void copy(char arr[50][30], char temp[50][30])
{
for (int x = 0; x < 50; x++)
{
for (int y = 0; y < 30; y++)
{
arr[x][y] = temp[x][y];
}
}
}

void life(char arr[50][30], char temp[50][30])
{
char answer = 'n';

seed(arr, temp);
arr[25][15] = '0';
arr[24][15] = '0';
arr[23][15] = '0';

do
{
system("cls");
copy(arr, temp);

int count = 0;

//Checks all cells around it and adds them together
for (int j = 1; j < MAX_X; j++)
{
for (int i = 1; i < MAX_Y; i++)
{
int total = 0;

total =
arr[j - 1][i] +
arr[j][i - 1] +
arr[j + 1][i] +
arr[j][i + 1] +
arr[j - 1][i - 1] +
arr[j + 1][i + 1] +
arr[j - 1][i + 1] +
arr[j + 1][i - 1];

if (total < 2 || count > 3) //Dies
{
temp[j][i] = '.';
}

if (total == 2) //Lives
{
temp[j][i] = arr[j][i];
}

if (total == 3) //Stays alive or is created
{
temp[j][i] = '0';
}
}
}
copy(arr, temp);

for (int j = 1; j < MAX_X; j++)
{
for (int i = 1; i < MAX_Y; i++)
{
cout << arr[j][i];
}
}


Sleep(100);

if (same(arr, temp) == 1)
cout << "Would you like to run another simulation?(Y/N): ";
cin >> answer;

} while (answer != 'y' || answer != 'Y');
}

int same(char arr[50][30], char temp[50][30]) //Checks if both arrays are the same
{
int count = 0;

for (int j = 0; j < 50; j++) //Compares arr and temp and increases count every time they're the same
{
for (int i = 0; i < 30; i++)
{
if (arr[j][i] == temp[j][i])
count++;
}
}

if (count == (50 * 30)) //Since count checks every cell, arrays should both be the same when the total cell count is reached
{
return 1;
}
else
{
return 0;
}
}
Remember to use code tags.

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
#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

//Global Variables
int sleep = 300;
int MAX_X = 49;
int MAX_Y = 29;
char arr[50][30];
char temp[50][30];


//Functions in the order they're used
void seed(char[50][30], char[50][30]);
void copy(char[50][30], char[50][30]);
void life(char[50][30], char[50][30]);
int same(char[50][30], char[50][30]);


int _tmain(int argc, _TCHAR* argv[])
{
	string key;


	//Intro
	do
	{
		cout << "Welcome to the Game of Life Simulator. Press enter to continue.";
		getline(cin, key);
	} while (key == "\n");

	life(arr, temp);

	return 0;
}

void seed(char arr[50][30], char temp[50][30])
{
	for (int x = 0; x < 50; x++)
	{
		for (int y = 0; y < 30; y++)
		{
			arr[x][y] = '.';
		}
	}

	for (int x = 0; x < 50; x++)
	{
		for (int y = 0; y < 30; y++)
		{
			temp[x][y] = '.';
		}
	}
}

void copy(char arr[50][30], char temp[50][30])
{
	for (int x = 0; x < 50; x++)
	{
		for (int y = 0; y < 30; y++)
		{
			arr[x][y] = temp[x][y];
		}
	}
}

void life(char arr[50][30], char temp[50][30])
{
	char answer = 'n';

	seed(arr, temp);
	arr[25][15] = '0';
	arr[24][15] = '0';
	arr[23][15] = '0';

	do
	{
		system("cls");
		copy(arr, temp);

		int count = 0;

		//Checks all cells around it and adds them together
		for (int j = 1; j < MAX_X; j++)
		{
			for (int i = 1; i < MAX_Y; i++)
			{
				int total = 0;

				total =
					arr[j - 1][i] +
					arr[j][i - 1] +
					arr[j + 1][i] +
					arr[j][i + 1] +
					arr[j - 1][i - 1] +
					arr[j + 1][i + 1] +
					arr[j - 1][i + 1] +
					arr[j + 1][i - 1];

				if (total < 2 || count > 3) //Dies
				{
					temp[j][i] = '.';
				}

				if (total == 2) //Lives
				{
					temp[j][i] = arr[j][i];
				}

				if (total == 3) //Stays alive or is created
				{
					temp[j][i] = '0';
				}
			}
		}
		copy(arr, temp);

		for (int j = 1; j < MAX_X; j++)
		{
			for (int i = 1; i < MAX_Y; i++)
			{
				cout << arr[j][i];
			}
		}


		Sleep(100);

		if (same(arr, temp) == 1)
			cout << "Would you like to run another simulation?(Y/N): ";
		cin >> answer;

	} while (answer != 'y' || answer != 'Y');
}

int same(char arr[50][30], char temp[50][30]) //Checks if both arrays are the same
{
	int count = 0;

	for (int j = 0; j < 50; j++) //Compares arr and temp and increases count every time they're the same
	{
		for (int i = 0; i < 30; i++)
		{
			if (arr[j][i] == temp[j][i])
				count++;
		}
	}

	if (count == (50 * 30)) //Since count checks every cell, arrays should both be the same when the total cell count is reached
	{
		return 1;
	}
	else
	{
		return 0;
	}
}
Last edited on
Topic archived. No new replies allowed.