The Game of Life

Hello everyone. I have an assignment in a C++ class and I feel like I am very close but I keep erring out on a segmentation fault. I am not sure where the error is but 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

#include<iostream>
#include<vector>
#include<unistd.h> // linux / Mac OS X
// #include<windows.h> // Windows
#include<cstdlib>
using namespace std;

// You can change the size of your world matrix in your entire code
// just by changing the values of ROWS and COLS here.
#define ROWS 21
#define COLS 80

// You can change the characters used to represent DEAD and ALIVE cells here
#define DEAD  ' '
#define ALIVE '*'

void generation(vector< vector<char> > &world, vector< vector<char> > &world_copy);
void display(vector< vector<char> > &world);


int main()
{
    vector< vector<char> > world(ROWS, vector<char>(COLS, DEAD));
    vector< vector<char> > copy(ROWS, vector<char>(COLS, DEAD));
    
    // set some cells of world to ALIVE for initial configuration
    //Hard code the initial conditions.
      world[1][1] = world[1][2] =  world[1][3] = ALIVE; 
    
    while(true)
    {
        // clear the screen and display the world
        system("clear"); // linux / Mac OS X
        // system("cls"); // Windows
        display(world);
        
        // wait for some time
        usleep(800000); // linux / Mac OS X
        //sleep(800) //windows
        
        // update the world
        generation(world, copy);
    }
    return 0;
}
void generation(vector< vector<char> > &world, vector< vector<char> > &world_copy)
{
    // copy the contents of world into world_copy
    for(int i=0;i<ROWS;i++)
    {
        for(int j=0;j<COLS;j++)
        {
            
            world_copy[i][j]=world[i][j];
        }
    }
  
    for(int i = 0; i < ROWS; i++)
    {
        for(int j = 0; j < COLS; j++)
        {
            //        look at world_copy[i][j]'s neighbors and count ones that are alive
            //        update world[i][j] based on it
            //        Be careful when dealing with cells on the boundary since they
            //        do not have eight neighbors
            int count=0;
            
            if(i==0) //Checking the First Row
            {
                if(j==0) //Checking Top Left Corner
                {
                    if(world_copy[i][j+1]=='*') //Check the Right
                    {
                        count++;
                    }
                    if(world_copy[i+1][j+1]=='*') //Check Bottom Right Corner
                    {
                        count++;
                    }
                    if(world_copy[i+1][j]=='*') //Check Bottom
                    {
                        count++;
                    }
                    if(world_copy[i+1][COLS]=='*')//Check the Bottom Left
                    {
                        count++;
                    }
                    if(world_copy[i][COLS]=='*') //Check the Left
                    {
                        count++;
                    }
                    if(world_copy[ROWS][COLS]=='*') //Check the Top Left Corner
                    {
                        count++;
                    }
                    if(world_copy[ROWS][j]=='*') //Check the Top
                    {
                        count++;
                    }
                    if(world_copy[ROWS][j+1]=='*') //Check the Top Right
                    {
                        count++;
                    }
                }
                if(j==COLS) //Checking Top Right Corner
                {
                    //I have this part filled in and I'm sure it is right
                }
                else
                {
                   //I have this part filled 
                }
            }
            if((i!=0)||(i!=ROWS))
            {
                if(j==0)
                {
                    //I have this filled
 
                }
                if(j==COLS)
                {
                    //I have this filled
                }
                else
                {
                    //This part is filled 
                }
            }
            if(i==ROWS)
            {
                if(j==0)
                {
                    if(world_copy[i][j+1]=='*') //Check the Right
                    {
                        count++;
                    }
                    if(world_copy[0][j+1]=='*') //Check Bottom Right Corner
                    {
                        count++;
                    }
                    if(world_copy[0][j]=='*') //Check Bottom
                    {
                        count++;
                    }
                    if(world_copy[0][COLS]=='*')//Check the Bottom Left
                    {
                        count++;
                    }
                    if(world_copy[i][COLS]=='*') //Check the Left
                    {
                        count++;
                    }
                    if(world_copy[i-1][COLS]=='*') //Check the Top Left Corner
                    {
                        count++;
                    }
                    if(world_copy[i-1][j]=='*') //Check the Top
                    {
                        count++;
                    }
                    if(world_copy[i-1][j+1]=='*') //Check the Top Right
                    {
                        count++;
                    }
                }
                if(j==COLS)
                {
                    if(world_copy[i][0]=='*') //Check the Right
                    {
                        count++;
                    }
                    if(world_copy[0][0]=='*') //Check Bottom Right Corner
                    {
                        count++;
                    }
                    if(world_copy[0][j]=='*') //Check Bottom
                    {
                        count++;
                    }
                    if(world_copy[0][j-1]=='*')//Check the Bottom Left
                    {
                        count++;
                    }
                    if(world_copy[i][j-1]=='*') //Check the Left
                    {
                        count++;
                    }
                    if(world_copy[i-1][j-1]=='*') //Check the Top Left Corner
                    {
                        count++;
                    }
                    if(world_copy[i-1][j]=='*') //Check the Top
                    {
                        count++;
                    }
                    if(world_copy[i-1][0]=='*') //Check the Top Right
                    {
                        count++;
                    }
                }
                else
                {
                    if(world_copy[i][j+1]=='*') //Check the Right
                    {
                        count++;
                    }
                    if(world_copy[0][j+1]=='*') //Check Bottom Right Corner
                    {
                        count++;
                    }
                    if(world_copy[0][j]=='*') //Check Bottom
                    {
                        count++;
                    }
                    if(world_copy[0][j-1]=='*')//Check the Bottom Left
                    {
                        count++;
                    }
                    if(world_copy[i][j-1]=='*') //Check the Left
                    {
                        count++;
                    }
                    if(world_copy[i-1][j-1]=='*') //Check the Top Left Corner
                    {
                        count++;
                    }
                    if(world_copy[i-1][j]=='*') //Check the Top
                    {
                        count++;
                    }
                    if(world_copy[i-1][j+1]=='*') //Check the Top Right
                    {
                        count++;
                    }
                }
            }
        
            if(count==3 && world[i][j]==' ')
            {
                world_copy[i][j]='*';
            }
            else
            {
                if((count==2 || count ==3) && world[i][j]=='*')
                {
                    world_copy[i][j]='*';
                }
                else
                {
                    world_copy[i][j]=' ';
                }
            }
        }
    }
}
void display(vector< vector<char> > &world)
{
    // display the 2D matrix
    // You can add more code to 'beautify' the display of the matrix
    for(int i = 0; i < ROWS; i++)
    {
        for(int j = 0; j < COLS; j++)
        {
            cout << world[i][j];
        }
        cout << endl;
    }
}
Last edited on
1
2
vector< vector<char> > world(ROWS, vector<char>(COLS, DEAD));
if(world_copy[ROWS][COLS]=='*') //Check the Top Left Corner 
out of bounds
Thank you for catching that. I have made everything in bound now. However, I get this message in the output saying "TERM environment variable not set." (what I don't want) along with my output (which i want)

Do you have any idea what that means?
Last edited on
Topic archived. No new replies allowed.