game of life

Hello,
I am having a problem with my code here. The program does not work properly. I think the problem come from setDimension or the loop. World[a][b] does not work correctly.

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>
#include "Windows.h"
using namespace std;

typedef unsigned short sizeM;

class GOL
{
public:
    void setDimension(sizeM x, sizeM y);
    void printframe();
    void random();
    void grid();
    void life();


private:
    sizeM r, c;
     char **field;
     char **world;

};

void GOL::setDimension(sizeM x, sizeM y)
{
    field = new  char *[x];
    for (int i = 0; i < x; i++)
        field[i] = new  char[y];

        r = x;
        c = y;

}




void GOL::grid()
{
 int a = 0, b = 0;
 for (a = 0; a<r; a++)
 {
    for (b = 0; b<c; b++)
    {
     field[a][b] = ' ';

    }
 }

}

void GOL::printframe()
{
 for (int a = 0; a < r; a++)
 {
    for (int b = 0; b < c; b++)
    {
     if (field[a][b] == '1')
     {
        cout << '*';
     }
     else
     {
        cout << '-';
     }
    }
    cout << endl;
 }
}

void GOL::random() {
 int a,b;
 char life[2]={' ','1'};
 for (a = 0; a<r; a++)
 {
    for (b = 0; b<c; b++)
    {
     field[a][b] = life[rand() % 2];
    }
 }
}
void GOL::life()
{
 int numSurrounding = 0;

 for (int a = 0; a < r; a++)
 {
    for (int b = 0; b < c; b++)
    {
     if ((a + 1) < r && field[a + 1][b] == '1')
     {
        numSurrounding++;
     }
     if ((a - 1) >= 0 && field[a - 1][b] == '1')
     {
        numSurrounding++;
     }
     if ((b + 1) < c && field[a][b + 1] == '1')
     {
        numSurrounding++;
     }
     if ((b - 1) >= 0 && field[a][b - 1] == '1')
     {
        numSurrounding++;
     }
     if ((a + 1) < r && (b + 1) < c && field[a + 1][b + 1] == '1')
     {
        numSurrounding++;
     }
     if ((a + 1) < r && (b - 1) >= 0 && field[a + 1][b - 1] == '1')
     {
        numSurrounding++;
     }
     if ((a - 1) >= 0 && (b + 1) < c && field[a - 1][b + 1] == '1')
     {
        numSurrounding++;
     }
     if ((a - 1) >= 0 && (b - 1) >= 0 && field[a - 1][b - 1] == '1')
     {
        numSurrounding++;
     }

     if (numSurrounding < 2 || numSurrounding > 3)
     {
        world[a][b] = '-';
     }
     if (numSurrounding == 2)
     {
        world[a][b] = '1';
     }
     if (numSurrounding == 3)
     {
        world[a][b] = '1';
     }
     numSurrounding = 0;
    }
 }

 for (int a = 0; a < r; a++)
 {
    for (int b = 0; b < c; b++)
    {
     field[a][b] = world[a][b];
    }
 }
}

int main()
{
 {
    GOL gol;
    sizeM rows, cols;
    cout << "Enter a dimension of your choice" << endl << "-mxn-" << endl;
    cin >> rows >> cols;
    gol.setDimension(rows, cols);
    gol.grid();
    gol.random();
    gol.printframe();
    Sleep(1000);

    do
    {
     cout << "==============================================================" << endl;
     gol.life();
     gol.printframe();
     Sleep(1000);
    } while (true);
    return 0;
 }


 system("pause");
}



I appreciate any help.
Special thanks to whitenite1!!!
Last edited on
prog.cpp: In member function ‘void GOL::random()’:
prog.cpp:75:26: error: ‘rand’ was not declared in this scope
             int x = rand() % 2;
                          ^


You need to include the header that contains the rand function. You might also want to seed the random function at the beginning of main so you don't get the same random number each time.

Last edited on
Look at this page for more information.

http://www.cplusplus.com/reference/cstdlib/rand/
i have added now:
1
2
#include "cstdlib"
#include "time.h" 

and wrote
srand(time(NULL));
in the beginning of the code.
Now i am getting different random number but the program still wont continue in the loop.

edit: the debugger is showing me that in these lines something goes wrong:
1
2
3
4
5
		
	if (numSurrounding < 2 || numSurrounding > 3)
		{
			world[a][b] = '-';
		}
Last edited on
You didn't say what was going wrong with those lines. A better description would help us debug your problems.

However, I think you can find your problem by comparing lines 57 and 77 (0 vs. 1) with lines 45, 91 (etc.), 126 and 134 (' ', '-', and '*'). You are inconsistent with how you represent a living cell.
Thanks for the answer.
The problem is i dont know what is wrong with the lines. VS13 is showing me an arrow on line 126 with the note "memory cannot be read" (translated).
@stavi93

The main problem I saw, was that you never created a world. So, the program stopped when trying to access it. I fiddled with your code, and it seems to be working now. You may want to use a gotoXY() function to start drawing the colony always in the same place, so the screen doesn't scroll, or, use a clearscreen() function. Anyway, here's the code I corrected.

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
// Life.cpp : main project file.

#include <iostream>
#include "cstdlib"
#include "time.h" 
#include <Windows.h>

using namespace std;

typedef unsigned short sizeM;

class GOL
{
public:
 void setDimension(sizeM x, sizeM y);
 void printframe();
 void random();
 void grid();
 void life();


private:
 sizeM r, c;
 char **field;
 char **world;

};

void GOL::setDimension(sizeM x, sizeM y)
{
 field = new  char *[x];
 world = new  char *[x];
 for (int i = 0; i < x; i++)
 {
	field[i] = new  char[y];
	world[i] = new  char[y];
 }
 r = x;
 c = y;
}

void GOL::grid()
{
 int a = 0, b = 0;
 for (a = 0; a<r; a++)
 {
	for (b = 0; b<c; b++)
	{
	 field[a][b] = ' ';
	 world[a][b] = ' ';
	}
 }

}

void GOL::printframe()
{
 for (int a = 0; a < r; a++)
 {
	for (int b = 0; b < c; b++)
	{
	 if (field[a][b] == '1')
	 {
		cout << '*';
	 }
	 else
	 {
		cout << '-';
	 }
	}
	cout << endl;
 }
}

void GOL::random() {
 int a,b;
 char life[2]={' ','1'};
 for (a = 0; a<r; a++)
 {
	for (b = 0; b<c; b++)
	{
	 field[a][b] = life[rand() % 2];
	}
 }
}

void GOL::life()
{
 int numSurrounding = 0;

 for (int a = 0; a < r; a++)
 {
	for (int b = 0; b < c; b++)
	{
	 if ((a + 1) < r && field[a + 1][b] == '1')
	 {
		numSurrounding++;
	 }
	 if ((a - 1) >= 0 && field[a - 1][b] == '1')
	 {
		numSurrounding++;
	 }
	 if ((b + 1) < c && field[a][b + 1] == '1')
	 {
		numSurrounding++;
	 }
	 if ((b - 1) >= 0 && field[a][b - 1] == '1')
	 {
		numSurrounding++;
	 }
	 if ((a + 1) < r && (b + 1) < c && field[a + 1][b + 1] == '1')
	 {
		numSurrounding++;
	 }
	 if ((a + 1) < r && (b - 1) >= 0 && field[a + 1][b - 1] == '1')
	 {
		numSurrounding++;
	 }
	 if ((a - 1) >= 0 && (b + 1) < c && field[a - 1][b + 1] == '1')
	 {
		numSurrounding++;
	 }
	 if ((a - 1) >= 0 && (b - 1) >= 0 && field[a - 1][b - 1] == '1')
	 {
		numSurrounding++;
	 }

	 if (numSurrounding < 2 || numSurrounding > 3)
	 {
		world[a][b] = '-';
	 }
	 if (numSurrounding == 2)
	 {
		world[a][b] = '1';
	 }
	 if (numSurrounding == 3)
	 {
		world[a][b] = ' ';
	 }
	 numSurrounding = 0;
	}
 }

 for (int a = 0; a < r; a++)
 {
	for (int b = 0; b < c; b++)
	{
	 field[a][b] = world[a][b];
	}
 }
}

int main()
{
 {
	GOL gol;
	sizeM rows, cols;
	srand((unsigned) time(0));
	cout << "Enter a dimension of your choice" << endl << "-mxn-" << endl;
	cin >> rows >> cols;
	gol.setDimension(rows, cols);
	gol.grid();
	gol.random();
	gol.printframe();
	Sleep(1000);

	do
	{
	 cout << "==============================================================" << endl;
	 gol.life();
	 gol.printframe();
	 Sleep(1000);
	} while (true);
	return 0;
 }


 system("pause");
}
Thanks for your help and your suggestions, I really appreciate it .
Now i am able to improve it.
Topic archived. No new replies allowed.