Game of Life

Coding Conway's Game of Life is my final project for my C++ class. I have pretty much assembled the code I'm just having a few logistical issues.

The board is eighty by eighty with sixty generations and requires input from a data file. The program needs to output each generation with a blank line between each generation and needs to include the generation number for each board.

For the input data, the first number (in my data file) is the number of lines to be read in. Second to whatever lines contains two integers, the first is the row, the second is the column where a cell is to be alive at start - thus explaining why I have input for numcell and a for loop placing the values into the board array.

First off, I'm receiving an error at the spot I marked with a comment - "Exception thrown at 0x00F7CD8A in Lab 6.exe: 0xC0000005: Access violation writing location 0x00D00016." Additionally, when the program did compile and didn't give me this error message, the game was not printing the boards correctly. It repeatedly gave me Generation 60 with a blank board below it.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void print(ofstream &output, bool board[60][82][82])
{
	for (int r = 1; r < 81; r++)
	{
		for (int c = 1; c < 81; c++)
		{
			if (board[r][c] == false)
				output << 'x';
			else
				output << 'o';
		}
		output << endl;
	}
}
int neighbors(int row, int col, int level)
{
	bool board[60][82][82];
	int r, c, count = 0;
	for (r = row - 1; r <= row + 1; ++r)
		for (c = col - 1; c <= col + 1; ++c)
			if (r == row && c == col)
				continue;
	if (board[r][c][level])
		++count;

	return count;
}
void life(ofstream &output, bool board[60][82][82])
{
	int c, r, level, count = 0;
	for (level = 0; level < 59; ++level)
	{
		for (r = 1; r < 81; ++r)
			for (c = 1; c < 81; ++c)
			{

				count = neighbors(r, c, level);

				if (board[r][c][level])
				{
					if (count == 2 || count == 3)
						board[r][c][level + 1] = true;
				}
				else if (count == 3)
				{
					board[r][c][level + 1] = true;
				}
			}


	}
}

int main()
{
	bool board[60][82][82]{ false };
	int c, r, level, cellnum, x, y;
	string gen;

	ifstream input("start.dat");
	ofstream output("boards.txt");

	input >> cellnum;

	while (!input.eof())
	{

		for (r = 0; r < 82; ++r)
			for (c = 0; c < 82; ++c)
				for (level = 0; level < 60; ++level)
					board[r][c][level] = false; // ISSUE SPOT

for (int i = 0; i < cellnum; i++) {
		input >> x >> y;
		board[0][y][x] = true;
	}

		if (level < 10)
			gen = "#############";
		else if (level >= 10 && level < 100)
			gen = "##############";

		output << gen << endl << "Generation " << level;
		output << ":" << endl << gen << endl << endl;

		if (level == 0)

		print(output, board);
		life(output, board);
		level++;

		input >> cellnum;
	}
		system("pause");
		return 0;
	
}
Last edited on
You need to be consistent with the order of the array indices.

In the print function on line 13 you have forgot the generation number. board[?][r][c]

On line 29, 45, 48, 52 and 77 you are passing the generation number as the last index when it should be the first. If your current order is indeed what you prefer you'll have to change the definition of board so that the dimensions are correct.
Take a look at the function neighbors(...) line 23: It doesn't make sense to have a local uninitialized array board. You need to pass it as an additional paramter.
Topic archived. No new replies allowed.