reassigning values in the game of life!

I'm writing code for the game of life, and if the googling I've done is any indicator, you're probably all very familiar with this.
I realize this is probably a terrible way to do it, but this is my code, laugh if you please.
At the end of most of this code, I'm trying to set array elements that are 0 to become 1 if they meet certain perameters, and vice-versa. I'm fairly certain this is where my problem lies. I've tried many different methods, and none of them work. I'm totally stuck.
I looked for a place on this site to test posting code, but I didn't find any so I hope this posts correctly. This is not the finished product, I'm going to change the 0s to spaces and 1s to *s, but I already know how to do that. More of my thoughts follow the code.


This is the problem section...

1
2
3
4
	      if(cells[i][j]==0 && sum==3)  //if the cell is vacant, these actions will occur.    
		cells[i][j]=1;   //birth
	      else if(cells[i][j]==1 && (sum-1<=1 || sum-1>=4))   // 1 is subtracted because the cell itself was counted, but its value is not useful
		cells[i][j]=0;       //death 




This is the whole code (including problem section)
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
//gameOfLife.cpp
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int main()
{
  ifstream inFile;
  inFile.open("config.txt");
  if (inFile.fail())         //in case the file is not found
    {
      cout << "File not found.";
    }
  int gen;
  int cells[10][20];
  int sum=0;
  int i=0;
  int j=0;
  int m=0;
  int n=0;
  inFile>>gen;
  for( i=0 ; i<10 ; i++)
    {
      for( j=0 ; j<20 ; j++)
	{
	  inFile >> cells[i][j];
	  cout << cells[i][j] << " ";           //prints generation 0
	}   
      cout<< endl;
    }
  cout << endl;
  for( int g=1 ; g<=gen ; g++)  //a loop to output each generation starting at 0 and ending whenever the config file specifies
    {
      for( i=0 ; i<10 ; i++)
	{
	  for( j=0 ; j<20 ; j++)
	    {
	      sum=0;          //setting sum equal to zero for the next generation
              if(i>=1 && i < 9 && j>=1 && j <19) //to handle the cells in the middle (not sides or corners)
		for ( m =i-1 ; m<=i+1 ; m++)
		  {
		    for( n = j-1 ; n <= j+1 ; n++)
		      {	
			sum+=cells[m][n];
		      }
		  } 
	      if(i==0 || i==9 || j==0 || j==19)  //to handle the cells on the sides and corners
		{
		  if(i==0 && j==0)              //to handle the cell [0][0] (top left corner)
		    for( m=0 ; m<=1; m++)   
		      {
			for( n=0 ; n<=1; n++)
			  {
			    sum+=cells[m][n];
			  }
		      }
		  if(i==0 && j<19 && j!=0)              //to handle cells [0][1-8] (far left side, except the corners)
		    for( m=0 ; m<=1 ; m++)
		      {
			for( n = j-1 ; n <= j+1 ; n++)
			  {	
			    sum+=cells[m][n];
			  }
		      }
		  if(i==9 && j<19 && j!=0)        //to handle cells [19][1-8] (far right side, except the corners)
		    for( m=9 ; m>=8 ; m--)
		      {
			for( n = j-1 ; n <= j+1 ; n++)
			  {	
			    sum+=cells[m][n];
			  }
		      }
		  if(i==0 && j==19)      //to handle cells [0][9] (bottom left corner)
		    for( m=0 ; m<=1 ; m++)
		      {
			for( n=19 ; n>=18 ; n--)
			  {
			    sum+=cells[m][n];
			  }
		      }
		  if(i==9 && j==0)        //to handle cells [19][0] (top right corner)
		    for( m=9 ; m>=8 ; m--)
		      {
			for( n=0 ; n<=1; n++)
			  {
			    sum+=cells[m][n];
			  }
		      }
		  if(i==9 && j==19)     //to handle cells [19][9] (bottom right corner)
		    for( m=9 ; m>=8 ; m--)
		      {
			for( n=19 ; n>=18 ; n--)
			  {
			    sum+=cells[m][n];
			  }
		      }
		  if(i<9 && j==0 && i!=0)          //to handle cells [1-18][0] (top of the array, except corners)
		    for ( m =i-1 ; m<=i+1 ; m++)
		      {
			for( n=0 ; n<=1; n++)
			  {
			    sum+=cells[m][n];
			  }
		      }
		  if(i<9 && j==19 && i!=0)      //to handle cells [1-18][9] (bottom of the array, except corners)
		    for ( m =i-1 ; m<=i+1 ; m++)
		      {
			for( n=19 ; n>=18 ; n--)
			  {
			    sum+=cells[m][n];
			  }
		      }
		}	
 //this is the test cout //cout << "(" << i << ", " << j << ") sum: " << sum << endl;
	      //PROBLEM IS HERE. It's summing correctly, but not reassigning 0 to 1 and 1 to 0 correctly.	
	      if(cells[i][j]==0 && sum==3)  //if the cell is vacant, these actions will occur.    
		cells[i][j]=1;   //birth
	      else if(cells[i][j]==1 && (sum-1<=1 || sum-1>=4))   // 1 is subtracted because the cell itself was counted, but its value is not useful
		cells[i][j]=0;       //death
	    }
	}
      for( i=0 ; i<10 ; i++)    //print altered generation
	{
	  for( j=0 ; j<20 ; j++)
	    {
	      cout << cells[i][j] << " ";   //prints first row, then ends line, then prints second row, etc.
	    }
	  cout << endl;
	}
      cout << endl;                    //puts a space between the last and next array
    }                                //loops back to beginning to compute next generation
  inFile.close();
  return 0;
}



I set up a test cout that tells me what the sum values for the array elements are, and cells[2][2] never comes out right. It should be 5, but it comes out to be 4 if i subtract 1 from sum in the part where I try to reassign 1s to 0s, or 6 if I don't subtract 1 from sum .I THINK it may be because a life is popping up (or taken) before that element's value is summed? That's all I got.
Any hints you can give me will be greatly appreciated.
Last edited on
You never initialise your array to zero.

EDIT: Oh, my bad, you read the values in from a file...
Last edited on
@bmop

Your program works fine for me!

Are you inputting some data into your config file?
Apologies, I forgot to show you the config.txt file that I'm drawing the values from.

This is the file-
3
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


The top number is the number of generations I need to go though, and the next 10x20 array is the 0th generation. the sum of cells[2][2] should be 5 but it never is.

The next generation SHOULD look like this

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


...but unfortunately this is my output


zero generation, same as input file
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0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