Game of life beginners help. inline function, counting

I don't know how to code the inline function to count the number of living cells.

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
#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,0,1,1,0,0,0,0,1,1,0,0,1,1,0,0,1,0,},
	   	{1,0,1,1,0,1,0,1,0,1,0,1,0,1,1,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,},
		   {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)
   {
	// 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++;
   }

   
I love the game of life. I had to program something like this for national competitions in C++. I got tenth place. The only problem with my program was this function, but I've figured out how to fix it.

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
{
     int minx;
     int miny;
     int maxx;
     int maxy;
     int alive;

     //prevent counting cell with neighbors
     if(Board[column][row] == 0)
          alive = 0;
     else
          alive = -1;

     switch(row)
     {
     case 0:
          miny = 0;
          maxy = 1;
          break;
     case MAXROWS:  //I don't know max rows
          miny = -1;
          maxy = 0;
          break;
     default:
          miny = -1;
          maxy = 1;
     }//end switch

     switch(column)
     {
     case 0:
          minx = 0;
          maxx = 1;
          break;
     case MAXCOLUMNS:  //I don't know max columns
          minx = -1;
          maxx = 0;
          break;
     default:
          minx = -1;
          maxx = 1;
     }//end switch

     for(int y = miny; y < maxy; y++)
     {
          for(int x = minx; x < maxx; x++)
          {
               alive += Board[x][y]
          }//end for
     }//end for
}//end function 
Last edited on
is that all for just the inline function?
Yes it is. You can either use that, or you can use a crap load of decision structure for loops.

EDIT: I forgot break statements in part of the code, so if you're using old code, it won't work.
Last edited on
Topic archived. No new replies allowed.