game of life help

Hello I just need some help with the bool Life(BoardType Board, int Row, int Col)function. Im not looking for the answer looking for some guidance

Description: This program uses the rules from Conway's Game Of Life (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life)
It will Prompt the user for the number of generations (cycles) to run, pausing for a keystroke between each generation.
The program can be ended at anytime by pressing CTRL + C
Currently a 24 x 24 boolean array is hard coded in the program.

1 (true) represents a live cell
0 (false) represents a dead cell

The rules are as follows:

1. Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.
2. Any live cell with more than three live neighbors dies, as if by overcrowding.
3. Any live cell with two or three live neighbors lives on to the next generation.
4. Any dead cell with exactly three live neighbours becomes a live cell.
*/

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
#include <iostream>
#include <cstdlib>
#include <stdlib.h>  // You might be able to get away without this one.
using namespace std;

const int size = 24;

typedef bool BoardType[size][size]; 

// Function prototypes:

void display(BoardType Board, int & iteration);

bool Life(BoardType Board);

void populate(BoardType Board, BoardType Board2);

// A function prototype can't be used with an inline function (see below for function NumLiveNeighbors).  Just be careful to use it only after the function is defined.

int main()
   {
	int Iteration = 0; // needed here to count the use of the display function 
	int cycle;
	BoardType Board2;
	BoardType Board =
	   {
   		{0,1,0,0,1,1,0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,1,1,1,1,1,1,1,1,1,},
		   {0,1,1,1,0,0,0,0,0,1,0,1,0,1,0,1,0,0,0,1,0,1,0,1,},
		   {0,1,1,1,1,0,0,0,1,1,1,1,0,0,0,0,0,1,0,1,1,1,1,0,},
		   {1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,},
		   {0,1,0,0,1,1,0,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,0,},
	   };
	
	cout << "Enter the number of generations to run." << endl;
	cin >> cycle;
	cout << "You will need to press a key between cycles" << endl;
	cout << "Press CTRL + C to exit at any time.";
   	system("PAUSE");
	system("cls");
	display(Board, Iteration);
	cout << endl << endl << endl;
	
	for (int i = 1; i <= cycle; i++)
	   {
		// If system("PAUSE") does not work with your version of Visual Studio
		// try the method of prompting the user to press Enter to go on, then
  		// read that keypress into a char variable.  Similarly, if system("cls")
		// does not work for you, you might be able to clear the screen by
		// outputting the correct number of endl's.
		system("PAUSE");	//creates the pause between generations
		system("cls");		// Clears the screen
		populate(Board, Board2);
		display(Board, Iteration);
	   }
	   
	return 0;
   }

/* Given:   Board	A 2D array of type Bool, serving as the matrix for our game
            Row		An integer defining the row of the cell being checked.
            Col		An integer defining the column of the cell being checked.
   Task:	 Calculate the number of live neighboring cells.
   Return:  In the function name, return the count of the number of live neighbors.
*/
inline int NumLiveNeighbors(BoardType Board, int Row, int Col)
   {
	   NumLiveNeighbors=0
	
	int left, top, right, bottom;
	
	   if (row=0)
	   {
		   top=0;
		   bottom=1;
	   }

	  if (Board[row - 1][col - 1])
		NumLiveNeighborss++;
	if (Board[row - 1][col])
		NumLiveNeighbors++;
	if (Board[row - 1][col + 1])
		NumLiveNeighbors++;
	if (Board[row ][col - 1])
		NumLiveNeighbors++;
	if (Board[row ][col + 1])
		NumLiveNeighbors++;
	if (Board[row + 1][col - 1])
		NumLiveNeighbors++;
	if (Board[row + 1][col])
		NumLiveNeighbors++;
	if (Board[row + 1][col + 1])
		NumLiveNeighbors++
	
	// Write the code for this function.  There are several ways to cout the
	// number of neighbors.  Be careful not to go beyond the edges of the 
	// Board.  That means there are several special cases to consider, such
	// as when Row and Col are for one of the corners of the Board, or when Row
	// and Col specify another location on the edge of the Board.


   
   }

/*	Given:  Board	A 2D array of type Bool, serving as the matrix for our game
            Row		An integer defining the row of the cell being checked.
            Col		An integer defining the column of the cell being checked.
   Task:	 Calculate the number of live neighboring cells using checkmid,
            checktop, checkbottom. Determine if the cell being studied will be
            alive or dead for the next generation (See rules Above)
   Return:  True     if the cell will be live for the next generation
            False    if the cell will be dead for the next generation,
*/
bool Life(BoardType Board, int Row, int Col)
   {
	int live = NumLiveNeighbors(Board, Row, Col);

	if (Board[Row][Col])   // if Board[Row][Col] is true then...
	   {
		// If the number of live neigbors is too large or small (see the rules)
		// then return false to say that the cell will die.
		// Otherwise, return true to say the the cell will remain alive.
 
	   }
	else    // if Board[Row][Col] is false then...
	   {
		// If the number of live neighbors is just right, return true to say
		// that this dead cell will come to life.  Otherwise return false to say
		// that this dead cell stays dead.  (Again, refer to the rules.)
			
	   }
   }

// The rest of the program has been written for you.  Don't change it. ************

/*	Given: Board	A 2D array of type Bool, serving as the matrix for our game. 
            Board2	Board2 will be populated as the function Life determines
                     whether the cell is live or dead in the next generation,
                     after Board2 is populated, it will be copied into Board, to
                     represent itself as the current generation. 
   Task:	Populate Board2 with the values from Life, representing the next 
           generation. Copy the populated Board2 into Board, the contents of
           Board2 are now the current generation.
   Return:  Board    Containing the new generation of cells.
            Board2   Containing a copy of Board.
*/
void populate(BoardType Board, BoardType Board2)
   {
	for (int row = 0; row < size; row++)
		for (int col = 0; col < size; col++)
			Board2[row][col] = Life(Board, row, col);
			
	for (int row = 0; row < size; row++)
		for (int col = 0; col < size; col++)
			Board[row][col] = Board2[row][col];
   }

/*	Given:  Board     The Array to Display
            iteration  A count of cycles that the function has run.
   Task:	  Display the contents of Board in an easy-to-read format.
   Return:  Iteration   Updated count of number of generations displayed.
*/
void display(BoardType Board, int & iteration)
   {
	cout << "    ";
	for (int Col = 0; Col < size; Col++)
	   {
		if (Col < 10)
			cout << Col << "  " ;
		else
			cout << Col << " ";
	   }
	cout << endl << endl;

	for (int Row = 0; Row < size; Row++)
	   {
		if (Row < 10)
			cout << Row << "   ";
		else 
			cout << Row << "  ";
		for (int Col = 0; Col < size; Col++)
		   if (Board[Row][Col])
			   cout << "X  " ;
			else
			   cout << "   ";
		cout << endl;
	   }
	   
	cout << " GENERATION: " << iteration << endl;
	iteration++;
   }

   
Just follow the rules of the game. If number of live cells is a certain number, then the living or dead cell dies or comes to life according to the rules. There's not much more to it than what it seems.
1
2
3
4
5
6
7
8
9
10
11
/*	Given: Board	A 2D array of type Bool, serving as the matrix for our game. 
            Board2	Board2 will be populated as the function Life determines
                     whether the cell is live or dead in the next generation,
                     after Board2 is populated, it will be copied into Board, to
                     represent itself as the current generation. 
   Task:	Populate Board2 with the values from Life, representing the next 
           generation. Copy the populated Board2 into Board, the contents of
           Board2 are now the current generation.
   Return:  Board    Containing the new generation of cells.
            Board2   Containing a copy of Board.
*/
This should give you a fairly good indicator of what needs to be involved. One thing I would suggest is looking at the populate function which ends up calling the Life function. This may help you out with what information should be returned and what information is passed. I'd also suggest using pseudo code to get you started, i.e. simple human terms, explain what you think the function should do, write it out in comments, then use those comments to write in C++.

[Edit] I'd also suggest starting at main() and working you way down the program much like a compiler would, look at what each function does as you see it appear in each other function. Try plugging in your own values as well if you have the patience for it, see what should happen.[/Edit]
Last edited on
here is what i have but its wrong...

/* Given: Board A 2D array of type Bool, serving as the matrix for our game
Row An integer defining the row of the cell being checked.
Col An integer defining the column of the cell being checked.
Task: Calculate the number of live neighboring cells using checkmid,
checktop, checkbottom. Determine if the cell being studied will be
alive or dead for the next generation (See rules Above)
Return: True if the cell will be live for the next generation
False if the cell will be dead for the next generation,
*/
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
bool Life(BoardType Board, int Row, int Col)
   {
        int live = NumLiveNeighbors(Board, Row, Col);

        if (Board[Row][Col]>= live) && (live>2 || live=3)  // if Board[Row][Col] is true then...
           {
                  
                        return 1;
                // If the number of live neigbors is too large or small (see the rules)
                // then return false to say that the cell will die.
                // Otherwise, return true to say the the cell will remain alive.
                   /*1. Any live cell with fewer than two live neighbors dies, as if caused by underpopulation.
2. Any live cell with more than three live neighbors dies, as if by overcrowding.
3. Any live cell with two or three live neighbors lives on to the next generation.
4. Any dead cell with exactly three live neighbours becomes a live cell.
*/
 
           }
        else   (Board[Row][Col] < live) && (live < 2 || live == 3) // if Board[Row][Col] is false then...
           {
                   return 0;
                // If the number of live neighbors is just right, return true to say
                // that this dead cell will come to life.  Otherwise return false to say
                // that this dead cell stays dead.  (Again, refer to the rules.)
                       
           }
   }
The numbers held in Board are all ones and zeroes representing true and false, so if the number of live neighbors is 2, the first if won't ever pass. Try making the first group of selection structures just to check if the cell is alive or dead, then deciding what to do next within those. I don't think this will work, and the else will always run if the if isn't true (though I think I remember learning about a way to add parameters to the else that makes this untrue).
Sorry, I didn't notice the code in your If and Else statements, change them back to how they were, that is exactly what you needed. All you did was put things in that weren't needed, or that should have broken the compiler.

A bigger help would be to look at this code
1
2
3
	for (int row = 0; row < size; row++)
		for (int col = 0; col < size; col++)
			Board2[row][col] = Life(Board, row, col);

This will pass the current board to Life, will give you the row and column to check, and then your return value should be what happens to the value contained at rowxcolumn.

In the Life function, the first thing that happens, is the if statement checks to see if the value is true at row, column. If it is, that means the cell that Life received was alive, and within that if statement, you need to determine whether it lives or dies depending on the rules you've been given, which would be rules 1-3.

Rule 4 would be placed into the else statement. I hope this is a better push into the right direction.
Last edited on
Topic archived. No new replies allowed.