Need some help with the "Game of life"

hello everyone,
i have been working on a uni project that's due in in the next day that requires me to remake this game / simulation http://www.bitstorm.org/gameoflife/

i have a prob that the array seems not to initialise and just returns blank (like there all 0's (when some should be 1's)) iv gone through my code countless times and i still cant see where im going wrong, so i was wondering if someone here would be kind enough to take a look at it for me?

here is the requirements (note: iv set the menu up but i haven't added any of the functions in yet, im just tring to get it to work 1st before i add in loading from file, variable speed etc)

• Variable speed
• At least two different starting configurations that load from disk
• User selectable number of generations to run, but stop automatically if no change made from one generation to the next
• Add one “special feature” of your own – ask in tutorial if not sure what to add


The Rules

For a space that is 'populated':
Each cell with one or no neighbors dies, as if by loneliness.
Each cell with four or more neighbors dies, as if by overpopulation.
Each cell with two or three neighbors survives.
For a space that is 'empty' or 'unpopulated'
Each cell with three neighbors becomes populated.


And here is my code (Note: im using classes since its required, i know this could be done without. Also please excuse the poor commenting and there is alot of work in here that isn't complete (like file.cpp) also Simulation::draw(int) case 4 is just for testing, case 1 draws the border and i use gotoxy() to move to the correct location in case 3 to add in the element (after the border has been drawn))

Code goes into post 2 as well (sorry post size was to big) if you guys have any questions please just post and ill get back to you as quick as i can

Many thanks.


Main.h

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
#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <windows.h>
#include <conio.h>


using namespace std;


class Simulation
{

private:
	int menuSnum;
	int state1[21][76];
	int state2[21][76];
	int neighborCount;
public:
	
	Simulation();
	~Simulation();
	void run();
	void draw(int);
	char getinput();
	int countNeighbor(int, int);
	void swap();
};

class File
{
private:
	Simulation simulation;
public:
	File();
	~File();
	void read();
	void write(bool);
};

class Console
{
private:

public:
	Console();
	~Console();
	void clrscr();
	void gotoxy(int, int);
	void setrgb(int);
};



Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "main.h"


Simulation simulation;

void main()
{

simulation.draw(2);


// sort out the draw size and the array size and hooppeee...


system("PAUSE");
}


Console.cpp

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
// console.cpp
//
#include "main.h"

//
//Constructor
//
Console::Console()
{
}
//
//Deconstrucor
//
Console::~Console()
{
}

//
// Clears the screen
//
void Console::clrscr()
{
	COORD coordScreen = { 0, 0 }; 
	DWORD cCharsWritten; 
	CONSOLE_SCREEN_BUFFER_INFO csbi; 
	DWORD dwConSize; 
	HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
  
	GetConsoleScreenBufferInfo(hConsole, &csbi); 	dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 
	FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); 
	GetConsoleScreenBufferInfo(hConsole, &csbi); 
	FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); 
	SetConsoleCursorPosition(hConsole, coordScreen); 
}

//
// Moves the cursor to x, y in console window
// ie x=left\right y=top\bottom
//
void Console::gotoxy(int x, int y)
{
	COORD point;
	point.X = x; point.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), point);
}

//
// Set text and background colors
//
void Console::setrgb(int color)
{
	switch (color)
	{
	case 0:	// White on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	case 1:	// Red on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_RED);
		break;
	case 2:	// Green on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_GREEN);
		break;
	case 3:	// Yellow on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_RED | FOREGROUND_GREEN);
		break;
	case 4:	// Blue on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_BLUE);
		break;
	case 5:	// Magenta on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_RED | FOREGROUND_BLUE);
		break;
	case 6:	// Cyan on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	case 7:	// Black on Gray
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			BACKGROUND_INTENSITY);
		break;
	case 8:	// Black on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
		break;
	case 9:	// Red on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
			FOREGROUND_RED);
		break;
	case 10: // Green on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
			FOREGROUND_GREEN);
		break;
	case 11: // Yellow on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
			FOREGROUND_RED | FOREGROUND_GREEN);
		break;
	case 12: // Blue on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
			FOREGROUND_BLUE);
		break;
	case 13: // Magenta on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
			FOREGROUND_RED | FOREGROUND_BLUE);
		break;
	case 14: // Cyan on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
			FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	case 15: // White on White
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY |
			FOREGROUND_INTENSITY | BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE |
			FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	default : // White on Black
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY |
			FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
		break;
	}
}


File.cpp

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
#include "main.h"


File::File()
{
}

File::~File()
{

}


void File::read()
{
	
}

void File::write(bool data) // need to pass it as a pointer
{

	

	ofstream output("output.txt");
 
	//for(int y = 0; y < 21; y++)
	//{
		//for(int x = 0; x < 75; x++)
		//{	
				output << data;
		//}
	}
//} 


code continues on post 2 :)
Last edited on
Simulation.cpp

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

#include "main.h"

Simulation::Simulation()
{

						
	//this->draw(2);
}
Simulation::~Simulation()
{

}
void Simulation::run()
{
	this->state1[10][30] = 1;
	this->state1[11][31] = 1;
	this->state1[12][31] = 1;
	this->state1[12][30] = 1;
	this->state1[12][29] = 1;

	while(1)
	{
		for(int y = 0; y < 22; y++)
		{
			for(int x = 0; x < 77; x++)
			{
				// if its populated
				if(this->state1[y][x] == 1)
				{
					if(this->countNeighbor(y, x) <= 1 || this->countNeighbor(y, x) >= 4)
					{
						this->state2[y][x] = 0;
					}
				}
				// if its not populated
				if(this->state1[y][x] == 0)
				{
					if(this->countNeighbor(y, x) == 3)
					{
						this->state2[y][x] = 1;
					}
				}
				// swap them over
				this->swap();
				//spit them out
				this->draw(4);
				Sleep(500);
			}
		}
	}
}
void Simulation::draw(int menuSnum)
{
	Console console;
	File file;
//
////Draws board, board size:
////x = 1 - 77 = 76
////y = 1 - 23 = 22
//
	switch (menuSnum)
	{
		// board
	case 1:
		console.clrscr();
		console.setrgb(2);
		for(int y = 0; y < 24; y++)
		{

			for(int x = 0; x < 79; x++)
			{
			
				if(y == 0 && x == 0)
				{
					cout << "+";
				}
				else if(y == 23 && x == 0)
				{
					cout << "+";
				}
				else if(y == 0 && x == 78)
				{
					cout << "+";
				}
				else if(y == 23 && x == 78)
				{
					cout << "+";
				}
				else if(y == 0)
				{
					cout << "-";
				}
				else if(y == 23)
				{
					cout << "-";
				}
				else if(x == 0)
				{
					cout << "|";
				}
				else if(x == 78)
				{
					cout << "|";
				}
				else
				{
					cout << " ";
				}
			}
			cout << endl;
		}
		console.setrgb(0);
	break;

		// menu
	case 2:


		this->draw(1);
		console.gotoxy(37,2);
		console.setrgb(0);
		cout << "MENU";
		console.gotoxy(20,4);
		cout << "Press"; console.setrgb(2); cout << " L "; console.setrgb(0); cout << "to load a shape.";
		console.gotoxy(20,5);
		cout << "Press"; console.setrgb(2); cout << " S "; console.setrgb(0); cout << "to change the speed.";
		console.gotoxy(20,6);
		cout << "Press"; console.setrgb(2); cout << " G "; console.setrgb(0); cout << "to set a fixed no of generations.";

		//waiting for the key to be pressed
		while(1)
		{
			if (this->getinput() == 'l')
			{
				this->run();
				break;
			}
		}
	break;

	case 3:

		this->draw(1);

		for(int y = 0; y < 22; y++) // 21 by 76 // 77 by 22?
		{
			for(int x = 0; x < 77; x++)
			{
				if(this->state1[y][x] == 1)
				{
					console.gotoxy(x + 1, y + 1); // here is where we need to change and the array size?
					console.setrgb(2);
					cout << "X";
					console.setrgb(0);
				}
			}
		}
		break;

	case 4: /// AHHHHHHHHHHHHHHHHHH FFS!
		
	

		console.clrscr();

		for(int y = 0; y < 22; y++) //test
		{
			for(int x = 0; x < 77; x++)
			{
				//if(this->state1[y][x] == 1)
				//{
				//	console.setrgb(2);
				//	cout << "X";
				//	console.setrgb(0);
			//	}
			//	else
				//{
				//	cout << " ";
				//}
				cout << this->state1[y][x];
			}
			cout << endl;
		}
	


		//cout << this->state1[12][31];

		break;


	default:
		break;


	}
}

char Simulation::getinput()
{
    if (_kbhit())
    {
        _getch(); // edit : if you want to check the arrow-keys you must call getch twice because special-keys have two values
        return _getch();
    }
    return 0; // if no key is pressed
}

int Simulation::countNeighbor(int y, int x)
{
	neighborCount = 0;

	if(state1[y + 1][x] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	if(state1[y + 1][x + 1] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	if(state1[y + 1][x - 1] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	if(state1[y][x + 1] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	if(state1[y][x - 1] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	if(state1[y - 1][x + 1] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	if(state1[y - 1][x] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	if(state1[y - 1][x - 1] == 1)
	{
		neighborCount = neighborCount + 1; 
	}
	
	return neighborCount;
}

void Simulation::swap()
{
	for(int y = 0; y < 22; y++)
	{
		for(int x = 0; x < 77; x++)
		{
			this->state1[y][x] = this->state2[y][x];
			this->state2[y][x] = 0;
		}
	}
}



any help would be greatly welcomed,

Kind regards, Makka
On Lines 20 and 21, you have declared state1 and state2 as having dimensions of 21,76

That means your indexes should go from 0..20, 0..75

Just glancing at your code, I see violations all over the place.
Your countNeighbour() function also will cause segmentation faults with
if(state1[y - 1][x - 1] == 1)
for example if x and/or y = 0.
Topic archived. No new replies allowed.