Checking neighbors in GoL

I am doing a version of the Game of Life and i have everything working except the checking for neighbors/apply the rules for the next generation. The code compiles and starts to run but then it crashes/stops running unless i comment out the if statements i have for checking neighbors. Below is the code in my main functions file. Any help would be appreciated.

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
 #include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <cstdlib>

//Including the header file
#include "life.h"

using namespace std;



//Declaring global variables
string *cells;

int numRows;
int numCols;

const char LIVE = 'x';
const char DEAD = 'o';


/* FUNCTION - void populateWorld

 Reads the input file into an array

 input parms - the file name

 output parms - none
*/
void populateWorld(string fileName)
{

    const int SIZE_INCREMENT = 3;
    char line[80];
    numRows = 0;
    numCols = 0;

    ifstream fin;
    fin.open(fileName.data());

    cells = new string[SIZE_INCREMENT];

    while ( fin.getline(line,80) )
    {
        cells[numRows] = line;
        numCols = cells[numRows].length();
        numRows ++;

        if (numRows % SIZE_INCREMENT == 0)
        {   //time to resize
            string *tempCells = new string[numRows + SIZE_INCREMENT];

            for (int i = 0; i < numRows; i++)
            {
                tempCells[i] = cells[i];
            }

            //free the text memory
            delete [] cells;

            cells  = tempCells;
        }
    }

    for(int row = 0; row < numRows; row++)
    {
        for (int col=0; col < numCols; col++)
        {
            if (cells[row][col] == '1')
            {
                cells[row][col] = LIVE;
            }
            else cells[row][col] = DEAD;

        }

        cout << endl;
    }
}


/* FUNCTION - void showWorld

 Prints out the structure as if it were 2D array of characters

 input parms - none

 output parms - none
*/

void showWorld() {

    //usleep(0.04);

     //show the text

    for(int row = 0; row < numRows; row++)
    {
        for (int col=0; col < numCols; col++)
        {
           cout << cells[row][col];
        }

        cout << endl;
    }

    cout << endl;

}


/* FUNCTION - iterateGeneration

 Checks all cells neighbors and they live or die according to the rules

 input parms - none

 output parms - none
*/
void iterateGeneration()
{

    string *newCells;
    newCells = new string[numRows];

    for (int i = 0; i < numRows; i++)
	{
		for (int j = 0; j < numCols; j++)
		{

			int liveCounter = 0;

			if ((cells[i-1][j-1] == LIVE) && (cells[i-1][j-1] >= cells[0][0]))
			{
				liveCounter++;
			}
			if ((cells[i-1][j] == LIVE) && (cells[i-1][j] >= cells[0][0]))
			{
				liveCounter++;
			}
			if ((cells[i-1][j+1] == LIVE) && (cells[i-1][j+1] >= cells[0][0]) && (cells[i-1][j+1] < cells[0][numCols]))
			{
				liveCounter++;
			}
			if ((cells[i][j-1] == LIVE) && (cells[i][j-1] >= cells[0][0]))
			{
				liveCounter++;
			}
			if ((cells[i][j+1] == LIVE) && (cells[i][j+1] < cells[0][numCols]))
			{
				liveCounter++;
			}
			if ((cells[i+1][j-1] == LIVE) && (cells[i+1][j-1] >= cells[0][0]) && (cells[i+1][j-1] < cells[numRows][0]))
			{
				liveCounter++;
			}
			if ((cells[i+1][j] == LIVE) && (cells[i+1][j] < cells[numRows][0]))
			{
				liveCounter++;
			}
			if ((cells[i+1][j+1] == LIVE) && (cells[i+1][j+1] < cells[numRows][numCols]))
			{
				liveCounter++;
			}

			if (cells[i][j] == LIVE && liveCounter < 2) //rule 1
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == DEAD;
			}
			else if (cells[i][j] == LIVE && liveCounter == 2 || liveCounter == 3) //rule 2
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == LIVE;
			}
			else if (cells[i][j] == LIVE && liveCounter > 3 ) //rule 3
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == DEAD;
			}
			else if (cells[i][j] == DEAD && liveCounter == 3) //rule 4
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == LIVE;
			}
		}
	}
    delete [] cells;

    cells = newCells;

}
Last edited on
So i tried the following as well, and the program still compiles and runs at first but then "Stops Working"/crashes and i have to close it. It is all because of lines 128-192

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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdlib.h>
#include <cstdlib>

//Including the header file
#include "life.h"

using namespace std;



//Declaring global variables
string *cells;

int numRows;
int numCols;

const char LIVE = 'x';
const char DEAD = 'o';


/* FUNCTION - void populateWorld

 Reads the input file into an array

 input parms - the file name

 output parms - none
*/
void populateWorld(string fileName)
{

    const int SIZE_INCREMENT = 3;
    char line[80];
    numRows = 0;
    numCols = 0;

    ifstream fin;
    fin.open(fileName.data());

    cells = new string[SIZE_INCREMENT];

    while ( fin.getline(line,80) )
    {
        cells[numRows] = line;
        numCols = cells[numRows].length();
        numRows ++;

        if (numRows % SIZE_INCREMENT == 0)
        {   //time to resize
            string *tempCells = new string[numRows + SIZE_INCREMENT];

            for (int i = 0; i < numRows; i++)
            {
                tempCells[i] = cells[i];
            }

            //free the text memory
            delete [] cells;

            cells  = tempCells;
        }
    }

    for(int row = 0; row < numRows; row++)
    {
        for (int col=0; col < numCols; col++)
        {
            if (cells[row][col] == '1')
            {
                cells[row][col] = LIVE;
            }
            else cells[row][col] = DEAD;

        }

        cout << endl;
    }
}


/* FUNCTION - void showWorld

 Prints out the structure as if it were 2D array of characters

 input parms - none

 output parms - none
*/

void showWorld() {

    //usleep(0.04);

     //show the text

    for(int row = 0; row < numRows; row++)
    {
        for (int col=0; col < numCols; col++)
        {
           cout << cells[row][col];
        }

        cout << endl;
    }

    cout << endl;

}


/* FUNCTION - iterateGeneration

 Checks all cells neighbors and they live or die according to the rules

 input parms - none

 output parms - none
*/
void iterateGeneration()
{

    string *newCells;
    newCells = new string[numRows];

    for (int i = 0; i < numRows; i++)
	{
		for (int j = 0; j < numCols; j++)
		{

			int liveCounter = 0;

			if ((i-1 >= 0) && (j-1 >= 0) && (cells[i-1][j-1] == LIVE))
			{
				liveCounter++;
			}
			if ((i-1 >= 0) && (cells[i-1][j] == LIVE))
			{
				liveCounter++;
			}
			if ( (i-1 >= 0) && (j+1 < numCols) && (cells[i-1][j+1] == LIVE))
			{
				liveCounter++;
			}
			if ((j-1 >= 0) && (cells[i][j-1] == LIVE))
			{
				liveCounter++;
			}
			if ((j+1 < numCols) && (cells[i][j+1] == LIVE))
			{
				liveCounter++;
			}
			if ((i+1 < numRows) && (j-1 >= 0) && (cells[i+1][j-1] == LIVE))
			{
				liveCounter++;
			}
			if ((i+1 < numRows) && (cells[i+1][j] == LIVE))
			{
				liveCounter++;
			}
			if ((i+1 < numRows) && (j+1 < numCols) && (cells[i+1][j+1] == LIVE))
			{
				liveCounter++;
			}

			if (cells[i][j] == LIVE && liveCounter < 2) //rule 1
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == DEAD;
			}
			else if (cells[i][j] == LIVE && liveCounter == 2 || liveCounter == 3) //rule 2
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == LIVE;
			}
			else if (cells[i][j] == LIVE && liveCounter > 3 ) //rule 3
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == DEAD;
			}
			else if (cells[i][j] == DEAD && liveCounter == 3) //rule 4
			{
				newCells[i][j] = cells[i][j];
				newCells[i][j] == LIVE;
			}
		}
	}
    delete [] cells;

    cells = newCells;

}
Last edited on
So i think the neighbor checker part is working...it seems that it is the rule applying (lines 168-187) that arent working and causing it to crash.

I tried a more simplified version, shown below, but it is not applying any of the rules:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

			if (liveCounter < 2 || liveCounter > 3) 
			{
				newCells[i][j] == DEAD;
			}
			if (liveCounter == 2) 
			{
				newCells[i][j] = cells[i][j];
            }
			if (liveCounter == 3) 
			{
				newCells[i][j] == LIVE;
			}
        
Topic archived. No new replies allowed.