Conway's Game of Life

Write your question here.
I am trying to write down Conway's game of life on a 90 by 90 board which goes on for 30 generations. It reads in from a .dat file and outputs to a .txt file.
Using 8 inputs for rows and columns respectively:
61 61
60 60
59 60
48 50
47 50
46 50
51 50
52 50

I can't seem to read in from the file correctly. It gives me eight X's in a straight line with no table or order


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

using namespace std;

int count_the_neighbours (int r, int c, int level);

bool life [92][92][31];

int main()
{
	ifstream fin ("start.dat");
	ofstream fout ("output.txt");
	int row, col, level;

	for (level = 0; level < 31; ++level)
	{	
		for (row = 0; row < 92; ++row)
		{	
			for (col = 0; col < 92; ++col)
			{
				life [row][col][level] = false;
			}
		}
	}
	if(fin.is_open() && !fin.eof())
	{
	while(fin >> row >> col)
	{
		
		life[row][col][0] = true;
		if (life[row][col][0] == true)
		{
			cout << "X";
			fout << "X";
		}
		else 
		{ 
			cout << ".";
			fout << ".";
		}
		
	}
	fin.close();
	}
	
	return 0;
}

/*int count_the_neighbours(int r, int c, int level)
{
	int count = 0; int row, col;

	for (row = r - 1; row < r + 2; ++ row)
		for (col = c - 1; col < c + 2; ++ col)
		{
			if (row == r && col == c)
				continue;
			if(life[row][col][level])
				++count;
		}
		return count;
}*/
@moose05

You weren't printing the whole array of life, just checking if what was in the current location, which you just filled, was true. Then printed the 'X'. Here is printing the life array. I made it a little smaller, so it fits on the screen better.

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

using namespace std;
using namespace System;

int count_the_neighbours (int r, int c, int level);

bool life [92][80][31]; // Changed numbers so whole level shows on screen

int main()
{
Console::SetWindowSize(96, 80 );
 ifstream fin ("start.dat");
 ofstream fout ("output.txt");
 int row, col, level;

 for (level = 0; level < 31; ++level)
 {	
	for (row = 0; row < 80; ++row)
	{	
	 for (col = 0; col < 94; ++col)
	 {
		life [row][col][level] = false;
	 }
	}
 }
 if(fin.is_open())
 {
	while(fin >> row >> col)
	{
	 life[row][col][0] = true; // Populate board first
	}
 }
 fin.close();
 
	for (row = 0; row < 80; ++row) // Now show Board, level 0
	{	
	 for (col = 0; col < 94; ++col)
	 {
		if (life[row][col][0] == true)
		{
		 cout << "X";
		 fout << "X";
		}
		else 
		{ 
		 cout << ".";
		 fout << ".";
		}
}
cout << endl;
 }
 return 0;
}

/*int count_the_neighbours(int r, int c, int level)
{
int count = 0; int row, col;

for (row = r - 1; row < r + 2; ++ row)
for (col = c - 1; col < c + 2; ++ col)
{
if (row == r && col == c)
continue;
if(life[row][col][level])
++count;
}
return count;
}*/
I got till here but can't output the neighbours and generations correctly.




/*
#include <iostream>
#include <fstream>
using namespace std;


int count_the_neighbours (int r, int c, int level);


bool life [92][92][31]; // Changed numbers so whole level shows on screen

int main()
{
int neighbours;



ifstream fin ("start.dat");
ofstream fout ("output.txt");
int row, col, level;

for (level = 0; level < 31; ++level)
{
for (row = 0; row < 92; ++row)
{
for (col = 0; col < 92; ++col)
{
life [row][col][level] = false;
}
}
}

for(level = 1; level < 31; level++)
{
for(row = 1; row < 92; row++)
{
for(col = 1; col < 92; col++)
{
neighbours = count_the_neighbours(row, col, level - 1);
if(neighbours < 2 || neighbours > 3)
life[row][col][level] = false;
if(neighbours == 2 || neighbours == 3)
life[row][col][level];
if(neighbours == 3)
life[row][col][level];;
}
}
}


if(fin.is_open())
{
while(fin >> row >> col)
{
life[row][col][0] = true; // Populate board first
}
}




fin.close();
do
{
level = 0;
cout << "Generation " << level << endl;
fout << "Generation " << level << endl;
level++;
for (row = 0; row < 92; ++row) // Now show Board, level 0
{

for (col = 0; col < 92; ++col)
{

if (life[row][col][level] == true)
{
cout << "X";
fout << "X";
}
else
{
cout << ".";
fout << ".";
}
}
}
cout << endl;
fout << endl;






}while(level <= 31);
return 0;
}


int count_the_neighbours(int row, int col, int level)
{
int count = 0;
int colplus1 = col + 1;
int colminus1 = col - 1;
int rowplus1 = row + 1;
int rowminus1 = row - 1;
if(col == 0)
colminus1 = 90;
if (col == 90)
colplus1 = 1;
if (row == 0)
rowminus1 = 90;
if (row == 90)
rowplus1 = 1;
if(life[rowminus1][colminus1][level] == true)
count++;
if(life[rowminus1][col][level] == true)
count++;
if(life[rowminus1][colplus1][level] == true)
count++;
if(life[row][colminus1][level] == true)
count++;
if(life[row][colplus1][level] == true)
count++;
if(life[rowplus1][colminus1][level] == true)
count++;
if(life[rowplus1][col][level] == true)
count++;
if(life[rowplus1][colplus1][level] == true)
count++;

return count;
}
*/
Topic archived. No new replies allowed.