Game of Life need help displaying the next generation

Hello, so I'm new to this site and I'm sorry in advance if I'm not following the rules of any sort, but right now I'm doing a project on Conway's game of life and so far I got it to display. However, my code doesn't seem to display the correct Next generation it just shows random numbers. Could anybody please help me? Thank You!
[HERE IS MY CODE]:


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

using namespace std;

//For Testing purposes only
const bool DEBUG = false;

const int ROWS = 12;
const int COLUMNS = 12;


//void PrintGrid(int World[ROWS][COLUMNS], int Rows[], int Columns[]);

//1
void CreateWorld(int World[ROWS][COLUMNS]);
//2
void Print(int World[ROWS][COLUMNS], int Rows[], int Columns[]);
//3
void CountNeighbors(int World[ROWS][COLUMNS],int i, int j);
//4
void CopyWorld(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS]);
//5
void Step(int World[ROWS][COLUMNS]);




//NOTE TO SELF: r = rows, c = columns


int main()
{
    int World[ROWS][COLUMNS];
    int TempWorld[ROWS][COLUMNS];
    int Rows[ROWS], Columns[COLUMNS];

    //Begin world as dead
    CreateWorld(World);

    //Print grid and world
    Print(World, Rows, Columns);

    //Copy temp world into world
    CopyWorld(World, TempWorld);

    //Update world
    Step(World);

    //Print out updated world
    Print(World, Rows, Columns);

        return 0;

}

void CreateWorld(int World[ROWS][COLUMNS])
{

    for(int i = 0; i < ROWS; i++)
    {
        for(int j = 0; j < COLUMNS; j++)
        {
            World[i][j] = 0;
        }
    }
    //For testing purposes only
    World[6][4] = 1;
    World[6][5] = 1;
    World[6][6] = 1;
}

void Print(int World[ROWS][COLUMNS],int Rows[],int Columns[])
{
    for(int i = 0; i < ROWS; i++)
    {
        Rows[i] = i % 10;

    }
    for(int j = 0; j < COLUMNS; j++)
    {
        Columns[j] = j % 10;

    }
        cout<<"  ";

        for(int j = 0; j < COLUMNS; j++)
        {
            cout<<Columns[j]<<" ";

        }

        cout<<endl<<endl;

            for(int i = 0; i < ROWS ; i++)
            {
                cout<<Rows[i]<<" ";


                for(int j = 0; j < COLUMNS; j++)
                {
                    cout<<World[i][j]<<" ";
                }
                 cout<<endl;
            }

}
//  Return number of CountNeighbors of cell World[i][j]
void CountNeighbors(int World[ROWS][COLUMNS], int i, int j)
{
    //r is MAX_ROWS and c is MAX_COLUMNS
    int r, c;
    int Neighbors = 0;
    for (r = i - 1; r <= i + 1; r++)
    {
        for (c = j - 1; c <= j + 1; c++)
        {
           Neighbors += World[r][c];
        }
    }
    if(Neighbors < 2)
        World[r][c] = 0;
    else if  (Neighbors > 3)
        World[r][c] = 0;
    else if (Neighbors == 2 || Neighbors == 3)
        World[r][c] = 1;
    else if (Neighbors == 3)
        World[r][c] = 1;
        
    //add a (!) if you would like to test the count
        if(DEBUG) cout<<"Neighbor count is: "<<Neighbors<<endl;
}

void CopyWorld(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS])
{

    for (int i = 0; i < ROWS; i++)

            for (int j = 0; j < COLUMNS; j++)
            {

                World[i][j] = TempWorld[i][j];

            }

}

//Update everything
void Step(int World[ROWS][COLUMNS])
{
    int TempWorld[ROWS][COLUMNS];

    CopyWorld(World, TempWorld);

    for (int i = 0; i < ROWS - 1; i++)

            for (int j = 0; j < COLUMNS - 1; j++)
            {
                CountNeighbors(World, i, j);

            }

     CopyWorld(TempWorld, World);


}

[MY OUTPUT]

 
0 1 2 3 4 5 6 7 8 9 0 1

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
2 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 1 1 1 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0 0
9 0 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 0 0 0 0 0 0 0 0 0 0 0
  0 1 2 3 4 5 6 7 8 9 0 1

0 -2 4519892 1540561326 1 4519900 1 0 4519880 1540462390 1541615312 4519892 1540
560557
1 17 4519988 1540555736 1541600440 1540555725 -1225530124 0 0 2130567168 10 -1 1

2 3279472 3279472 0 0 0 0 0 0 0 0 0 0
3 0 4520004 0 0 0 0 0 0 0 0 0 0
4 0 15 0 0 0 0 0 0 0 0 0 0
5 0 4520312 0 0 0 0 0 0 0 0 0 0
6 0 1541615312 0 0 0 0 0 0 0 0 0 0
7 0 2130567168 0 0 0 0 0 0 0 0 0 0
8 0 1541615312 0 0 0 0 0 0 0 0 0 0
9 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
1 0 1819720742 0 0 0 0 0 0 0 0 0 0
Press <RETURN> to close this window...

You are mixing some functionality in CountNeighbors(), and you are also not quite understanding the idea of a TempWorld (sorry).

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

using namespace std;

//For Testing purposes only
const bool DEBUG = false;

const int ROWS = 12;
const int COLUMNS = 12;


//void PrintGrid(int World[ROWS][COLUMNS], int Rows[], int Columns[]);

//1
void CreateWorld(int World[ROWS][COLUMNS]);
//2
void Print(int World[ROWS][COLUMNS], int Rows[], int Columns[]);
//3
void CountNeighbors(int World[ROWS][COLUMNS],int i, int j);
//4
void CopyWorld(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS]);
//5
void Step(int World[ROWS][COLUMNS]);




//NOTE TO SELF: r = rows, c = columns


int main()
{
    int World[ROWS][COLUMNS];
    int TempWorld[ROWS][COLUMNS];
    int Rows[ROWS], Columns[COLUMNS];

    //Begin world as dead
    CreateWorld(World);
    
    //Initialize TempWorld as a duplicate of World
    CopyWorld(World,TempWorld);

    //Print grid and world
    Print(World, Rows, Columns);

    //Update world (using TempWorld as scratch workspace)
    Step(World,TempWorld);

    //Print out updated world
    Print(World, Rows, Columns);

    return 0;

}

void CreateWorld(int World[ROWS][COLUMNS])
{

    for(int i = 0; i < ROWS; i++)
    {
        for(int j = 0; j < COLUMNS; j++)
        {
            World[i][j] = 0;
        }
    }
    //For testing purposes only
    World[6][4] = 1;
    World[6][5] = 1;
    World[6][6] = 1;
}

void Print(int World[ROWS][COLUMNS],int Rows[],int Columns[])
{
    for(int i = 0; i < ROWS; i++)
    {
        Rows[i] = i % 10;

    }
    for(int j = 0; j < COLUMNS; j++)
    {
        Columns[j] = j % 10;

    }
        cout<<"  ";

        for(int j = 0; j < COLUMNS; j++)
        {
            cout<<Columns[j]<<" ";

        }

        cout<<endl<<endl;

            for(int i = 0; i < ROWS ; i++)
            {
                cout<<Rows[i]<<" ";


                for(int j = 0; j < COLUMNS; j++)
                {
                    cout<<World[i][j]<<" ";
                }
                 cout<<endl;
            }

}
//  Return number of CountNeighbors of cell World[i][j]
/******** 
  Your main problem is that you are updating World as you go. 
  
  Don't do that.
  
  Instead, update TempWorld as you examine World, 
  then copy TempWorld to World when you are done.
*********/
void CountNeighbors(int World[ROWS][COLUMNS], int i, int j, int TempWorld[ROWS][COLUMNS])
{
    //r is MAX_ROWS and c is MAX_COLUMNS
    int r, c;
    int Neighbors = 0;
    for (r = i - 1; r <= i + 1; r++)
    {
        for (c = j - 1; c <= j + 1; c++)
        {
           Neighbors += World[r][c];
        }
    }
    if(Neighbors < 2)
        TempWorld[r][c] = 0;
    else if  (Neighbors > 3)
        TempWorld[r][c] = 0;
    else if (Neighbors == 2 || Neighbors == 3)
        TempWorld[r][c] = 1;
    else if (Neighbors == 3)
        TempWorld[r][c] = 1;
        
    //add a (!) if you would like to test the count
        if(DEBUG) cout<<"Neighbor count is: "<<Neighbors<<endl;
}

void CopyWorld(int FromWorld[ROWS][COLUMNS], int ToWorld[ROWS][COLUMNS])
{

    for (int i = 0; i < ROWS; i++)

            for (int j = 0; j < COLUMNS; j++)
            {

                ToWorld[i][j] = FromWorld[i][j];

            }

}

//Update everything
void Step(int World[ROWS][COLUMNS])
{
//NO    int TempWorld[ROWS][COLUMNS];

//NO    CopyWorld(World, TempWorld);

    for (int i = 0; i < ROWS - 1; i++)

            for (int j = 0; j < COLUMNS - 1; j++)
            {
                CountNeighbors(World, i, j, TempWorld);

            }

     CopyWorld(TempWorld, World);


}

I haven't actually compiled and run this, so I could have made a mistake or two.

Hope this helps.
Last edited on
It helped a lot actually! Thank You!! however I'm still confused with the part where you told me



1
2
3
//NO    int TempWorld[ROWS][COLUMNS];

//NO    CopyWorld(World, TempWorld); 


I couldn't seem to figure that part out sorry!

besides that, with the help of your notes I finally got the 2nd display to show somewhat correctly. Here's my edit code

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

using namespace std;

//For Testing purposes only
const bool DEBUG = false;

const int ROWS = 12;
const int COLUMNS = 12;


//void PrintGrid(int World[ROWS][COLUMNS], int Rows[], int Columns[]);

//1
void CreateWorld(int World[ROWS][COLUMNS]);
//2
void Print(int World[ROWS][COLUMNS], int Rows[], int Columns[]);
//3
void CountNeighbors(int World[ROWS][COLUMNS],int i, int j);
//4
void CopyWorld(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS]);
//5
void Step(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS]);




//NOTE TO SELF: r = rows, c = columns


int main()
{
    int World[ROWS][COLUMNS];
    int TempWorld[ROWS][COLUMNS];
    int Rows[ROWS], Columns[COLUMNS];

    //Begin world as dead
    CreateWorld(World);

    //Initialize TempWorld as a duplicate of World
    CopyWorld(World,TempWorld);

    //Print grid and world
    Print(World, Rows, Columns);

    //Update world (using TempWorld as scratch workspace)
    Step(World, TempWorld);

    //Print out updated world
    Print(World, Rows, Columns);

    return 0;

}

void CreateWorld(int World[ROWS][COLUMNS])
{

    for(int i = 0; i < ROWS; i++)
    {
        for(int j = 0; j < COLUMNS; j++)
        {
            World[i][j] = 0;
        }
    }
    //For testing purposes only
    World[6][4] = 1;
    World[6][5] = 1;
    World[6][6] = 1;
}

void Print(int World[ROWS][COLUMNS],int Rows[],int Columns[])
{
    for(int i = 0; i < ROWS; i++)
    {
        Rows[i] = i % 10;

    }
    for(int j = 0; j < COLUMNS; j++)
    {
        Columns[j] = j % 10;

    }
        cout<<"  ";

        for(int j = 0; j < COLUMNS; j++)
        {
            cout<<Columns[j]<<" ";

        }

        cout<<endl<<endl;

            for(int i = 0; i < ROWS ; i++)
            {
                cout<<Rows[i]<<" ";


                for(int j = 0; j < COLUMNS; j++)
                {
                    cout<<World[i][j]<<" ";
                }
                 cout<<endl;
            }

}
//  Return number of CountNeighbors of cell World[i][j]
/********
  Your main problem is that you are updating World as you go.

  Don't do that.

  Instead, update TempWorld as you examine World,
  then copy TempWorld to World when you are done.
*********/
void CountNeighbors(int World[ROWS][COLUMNS], int i, int j, int TempWorld[ROWS][COLUMNS])
{
    //r is MAX_ROWS and c is MAX_COLUMNS
    int r, c;
    int Neighbors = 0;
    for (r = i - 1; r <= i + 1; r++)
    {
        for (c = j - 1; c <= j + 1; c++)
        {
           Neighbors += World[r][c];
        }
    }
    if(Neighbors < 2)
        TempWorld[r][c] = 0;
    else if  (Neighbors > 3)
        TempWorld[r][c] = 0;
    else if (Neighbors == 2 || Neighbors == 3)
        TempWorld[r][c] = 1;
    else if (Neighbors == 3)
        TempWorld[r][c] = 1;

    //add a (!) if you would like to test the count
        if(!DEBUG) cout<<"Neighbor count is: "<<Neighbors<<endl;
}

void CopyWorld(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS])
{

    for (int i = 0; i < ROWS; i++)

            for (int j = 0; j < COLUMNS; j++)
            {

                TempWorld[i][j] = World[i][j];

            }

}

//Update everything
void Step(int World[ROWS][COLUMNS], int TempWorld[ROWS][COLUMNS])
{
//NO    int TempWorld[ROWS][COLUMNS];

//NO    CopyWorld(World, TempWorld);

    for (int i = 0; i < ROWS - 1; i++)

            for (int j = 0; j < COLUMNS - 1; j++)
            {
                CountNeighbors(World, i, j, TempWorld);

            }

     CopyWorld(World, TempWorld);
}



and this is my output now:

  0 1 2 3 4 5 6 7 8 9 0 1

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
2 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 1 1 1 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0 0
9 0 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 0 0 0 0 0 0 0 0 0 0 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 1
Neighbor count is: 2
Neighbor count is: 3
Neighbor count is: 2
Neighbor count is: 1
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 1
Neighbor count is: 2
Neighbor count is: 3
Neighbor count is: 2
Neighbor count is: 1
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 1
Neighbor count is: 2
Neighbor count is: 3
Neighbor count is: 2
Neighbor count is: 1
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
Neighbor count is: 0
  0 1 2 3 4 5 6 7 8 9 0 1

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
2 0 0 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 0 0
6 0 0 0 0 1 1 1 0 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0 0
8 0 0 0 0 0 0 0 0 0 0 0 0
9 0 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 0 0 0 0 0 0 0 0 0 0 0
Press <RETURN> to close this window...



It looks like it's counting correctly, but for the 2nd display instead of deleting the dead cells from the previous world it duplicated instead and I don't really understand that part either.
Topic archived. No new replies allowed.