It still works!!!... It still works???

I have implemented Conway's Game of Life (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) into a C++ program and here it is:
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
#include <iostream>

using namespace std;

const int HEIGHT = 45;
const int WIDTH = 39;

void nextGeneration(char grid[][WIDTH]);
void printGrid(char grid[][WIDTH]);

int main()
{
	char grid[HEIGHT][WIDTH];

	for (int row = 0; row < HEIGHT; row++)
		for (int column = 0; column < WIDTH; column++)
			grid[row][column] = '_';

	grid[3][5] = 'O'; grid[3][6] = 'O'; grid[4][5] = 'O';
	grid[4][6] = 'O'; grid[14][3] = 'O'; grid[14][4] = 'O';
	grid[14][8] = 'O'; grid[14][9] = 'O'; grid[16][4] = 'O';
	grid[16][8] = 'O'; grid[17][5] = 'O'; grid[17][6] = 'O';
	grid[17][7] = 'O'; grid[18][5] = 'O'; grid[18][6] = 'O';
	grid[18][7] = 'O'; grid[21][8] = 'O'; grid[22][7] = 'O';
	grid[22][8] = 'O'; grid[22][9] = 'O'; grid[23][6] = 'O';
	grid[23][10] = 'O'; grid[24][8] = 'O'; grid[25][5] = 'O';
	grid[25][11] = 'O'; grid[26][5] = 'O'; grid[26][11] = 'O';
	grid[27][6] = 'O'; grid[28][7] = 'O'; grid[28][8] = 'O';
	grid[28][9] = 'O'; grid[27][10] = 'O'; grid[37][7] = 'O';
	grid[37][8] = 'O'; grid[38][7] = 'O'; grid[38][8] = 'O';

	for (int iteration = 0; iteration < 5; iteration++)
	{
		printGrid(grid);
		nextGeneration(grid);
	}

	cout << endl;

	return 0;
}

void nextGeneration(char grid[][WIDTH])
{
	int row, column;
	char newGrid[HEIGHT][WIDTH];
	int counter = 0;

	for (column = 0; column < WIDTH; column++)
		for (row = 0; row < HEIGHT; row++)
			newGrid[row][column] = '_';

	for (column = 0; column < WIDTH; column++)
	{
		for (row = 0; row < HEIGHT; row++)
		{
			if (row == 0 && column == 0)
			{
				if (grid[row + 1][column] == 'O')
					counter++;
				if (grid[row][column + 1] == 'O')
					counter++;
				if (grid[row + 1][column + 1] == 'O')
					counter++;
			}
			else if (row == HEIGHT - 1 && column == 0)
			{
				if (grid[row - 1][column] == 'O')
					counter++;
				if (grid[row][column + 1] == 'O')
					counter++;
				if (grid[row - 1][column + 1] == 'O')
					counter++;
			}
			else if (row == 0 && column == WIDTH - 1)
			{
				if (grid[row + 1][column] == 'O')
					counter++;
				if (grid[row][column - 1] == 'O')
					counter++;
				if (grid[row + 1][column - 1] == 'O')
					counter++;
			}
			else if (row == HEIGHT - 1 && column == WIDTH - 1)
			{
				if (grid[row - 1][column] == 'O')
					counter++;
				if (grid[row][column - 1] == 'O')
					counter++;
				if (grid[row - 1][column - 1] == 'O')
					counter++;
			}
			else if (row == 0)
			{
				if (grid[row + 1][column] == 'O')
					counter++;
				if (grid[row][column + 1] == 'O')
					counter++;
				if (grid[row + 1][column + 1] == 'O')
					counter++;
				if (grid[row][column - 1] == 'O')
					counter++;
				if (grid[row + 1][column - 1] == 'O')
					counter++;
			}
			else if (row == HEIGHT - 1)
			{
				if (grid[row - 1][column] == 'O')
					counter++;
				if (grid[row][column + 1] == 'O')
					counter++;
				if (grid[row - 1][column + 1] == 'O')
					counter++;
				if (grid[row][column - 1] == 'O')
					counter++;
				if (grid[row - 1][column - 1] == 'O')
					counter++;
			}
			else if (column == 0)
			{
				if (grid[row - 1][column] == 'O')
					counter++;
				if (grid[row][column + 1] == 'O')
					counter++;
				if (grid[row - 1][column + 1] == 'O')
					counter++;
				if (grid[row + 1][column] == 'O')
					counter++;
				if (grid[row + 1][column + 1] == 'O')
					counter++;
			}
			else if (column == WIDTH - 1)
			{
				if (grid[row - 1][column] == 'O')
					counter++;
				if (grid[row][column - 1] == 'O')
					counter++;
				if (grid[row - 1][column - 1] == 'O')
					counter++;
				if (grid[row + 1][column] == 'O')
					counter++;
				if (grid[row + 1][column - 1] == 'O')
					counter++;
			}
			else
			{
				if (grid[row - 1][column] == 'O')
					counter++;
				if (grid[row][column + 1] == 'O')
					counter++;
				if (grid[row - 1][column + 1] == 'O')
					counter++;
				if (grid[row + 1][column] == 'O')
					counter++;
				if (grid[row + 1][column + 1] == 'O')
					counter++;
				if (grid[row - 1][column - 1] == 'O')
					counter++;
				if (grid[row + 1][column - 1] == 'O')
					counter++;
				if (grid[row][column - 1] == 'O')
					counter++;
			}
			if (counter < 2 || counter > 3)
				newGrid[row][column] = '_';
			if (counter == 3)
				newGrid[row][column] = 'O';
			if (counter == 2 && grid[row][column] == 'O')
				newGrid[row][column] = 'O';
			counter = 0;
		}
	}
	for (column = 0; column < WIDTH; column++)
		for (row = 0; row < HEIGHT; row++)
			grid[row][column] = newGrid[row][column];
}

void printGrid(char grid[][WIDTH])
{
	for (int row = 0; row < HEIGHT; row++)
	{
		cout << '|';
		for (int column = 0; column < WIDTH; column++)
			cout << grid[row][column] << '|';
		cout << endl;
	}
	cout << endl;
}
This code works fine until the value of WIDTH is changed to 40 or greater. The code still works fine BUT the grid is not printed as expected. One should note that at 40 or greater, the width of the grid is larger than the width of the viewing window. Is there something wrong with my code or is this just something that the compiler does when text is printed beyond the boundary of the viewing window? Please help, thanks in advance! :)

Note: I'm using Visual C++ 2008 Express Edition if that is of any importance.
Topic archived. No new replies allowed.