Game of Life implementation help

So I am having trouble getting the game of life to run. Our instructor gave us the first code, the second code is what we were supposed to right which is an implementation run by the third code which was also given by the instructor.

Right now when ever I try to run the 3rd code in visual studio, my command window is just blank and doesn't not output anything. You can pretty much ignore the first code it just tells sets the rows and columns to 25. The 3rd code cannot be changed so I guess it is just trying to get the 2nd code (my code) to be used correctly to implement the 3rd code.

Any ideas would be awesome.

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
 #ifndef LIFE_H
#define LIFE_H
const int ROWS = 25;
const int COLS = 25;

// Simulate one generation of Conways Game of Life
// 
// Given: 
//        a constant 2D bool array called "current" where each true element
//        indicates a live cell and each false element indicates a dead cell.
//
//        an empty  2D bool array called "next"
//
// Desired:
//        the next generation of the game of life written into the "next" array
//
//        "current" should remain unchanged
//
// The rules of the Game Of Life are as follows:
//
//        1. Any live cell with fewer than two live neighbours dies, as if
//           caused by under-population.
//
//        2. Any live cell with two or three live neighbours lives on to the
//           next generation.
//
//        3. Any live cell with more than three live neighbours dies, as if by
//           overcrowding.
//
//        4. Any dead cell with exactly three live neighbours becomes a live cell,
//           as if by reproduction.

void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]);
#endif


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

using namespace std;


void life(const bool current[ROWS][COLS], bool next[ROWS][COLS]) {

	for (int i = 0; i < ROWS; ++i) {
		for (int j = 0; j < COLS; ++j) {

			int counter;
			counter = 0;

			//Alive Cells
			if (current[i][j] == 1) {
		//checks for alive cells on game board		//up
				if (i - 1 >= 0 && current[i - 1][j] == 1)
					counter++;
				//down 
				if (i + 1 < ROWS  && current[i + 1][j] == 1)
					counter++;
				//left
				if (j - 1 >= 0 && current[j - 1][i] == 1)
					counter++;
				//right
				if (j + 1 < COLS && current[j + 1][j] == 1)
					counter++;
				//upRight
				if (i - 1 >= 0 && j + 1 < COLS && current[i - 1][j + 1] == 1)
					counter++;
				//upLeft
				if (j - 1 >= 0 && i - 1 > COLS && current[i - 1][j - 1] == 1)
					counter++;
				//downRight
				if (i + 1 >= 0 && i + 1 < ROWS && current[i + 1][j + 1] == 1)
					counter++;
				//downLeft
				if (j + 1 >= 0 && i + 1 > ROWS && current[i + 1][j - 1] == 1)
					counter++;

				if (counter < 2) next[i][j] = 0;
				if (counter == 2) next[i][j] = 1;
				if (counter == 3) next[i][j] = 0;
			}
			//Dead Cells
			if (current[i][j] == 0) {
			//checks for dead cells on game board	//up
				if (i - 1 >= 0 && current[i - 1][j] == 1)
					counter++;
				//down 
				if (i + 1 < ROWS  && current[i + 1][j] == 1)
					counter++;
				//left
				if (j - 1 >= 0 && current[j - 1][i] == 1)
					counter++;
				//right
				if (j + 1 < COLS && current[j + 1][j] == 1)
					counter++;
				//upRight
				if (i - 1 >= 0 && j + 1 < COLS && current[i - 1][j + 1] == 1)
					counter++;
				//upLeft
				if (j - 1 >= 0 && i - 1 > COLS && current[i - 1][j - 1] == 1)
					counter++;
				//downRight
				if (i + 1 >= 0 && i + 1 < ROWS && current[i + 1][j + 1] == 1)
					counter++;
				//downLeft
				if (j + 1 >= 0 && i + 1 > ROWS && current[i + 1][j - 1] == 1)
					counter++;

				if (counter == 0) next[i][j] = 1;

			}
		}
	}
	return;
}


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

const int GENERATIONS = 100;

using namespace std;

//make a random array of initial living cells
void gen(bool a[ROWS][COLS]) {
	for (int i = 0; i<ROWS; ++i) {
		for (int j = 0; j<COLS; ++j) {
			if (rand() % 100<10)a[i][j] = true;
			else a[i][j] = false;
		}
	}
	a[5][5] = true;
	a[5][6] = true;
	a[5][7] = true;
	return;
}

// check to see if two arrays are equal
bool equal(const bool a[ROWS][COLS], const bool b[ROWS][COLS]) {
	int i, j;
	for (i = 0; i<ROWS; ++i)for (j = 0; j<COLS; ++j)if (a[i][j] != b[i][j])return false;
	return true;
}

//copy the b array into the a array
void copy(bool a[ROWS][COLS], const bool b[ROWS][COLS]) {
	for (int i = 0; i<ROWS; ++i) {
		for (int j = 0; j<COLS; ++j) {
			a[i][j] = b[i][j];
		}
	}
	return;
}

//print out the array
void print(const bool a[ROWS][COLS]) {
	for (int i = 0; i<ROWS; ++i) {
		for (int j = 0; j<COLS; ++j) {
			if (a[i][j])cout << 'X';
			else       cout << ' ';
		}
		cout << endl;
	}
	return;
}


int main() {
	bool current[ROWS][COLS];
	bool next[ROWS][COLS];
	int i = 0;

	//initialze the cell array and print it out
	gen(current);
	print(current);

	while (i<GENERATIONS) {
		//get a carriage return before the next generation
		cin.get();

		//give the current generation to life()
		//it puts the next generation into next
		life(current, next);

		//copy the next generation into the current
		copy(current, next);

		//print it out
		print(current);
		i++;
	}

	return 0;
}
Topic archived. No new replies allowed.