### Game of Life - Code help

Hi, I'm programming Conway's Game of Life for my c++ class
. I've been working for hours trying to figure out why it's not working, here's the function that checks the neighboring cells

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758`` `````` void life::playGame() { //Sets array for 2 iterations ago to check for a repeating pattern for (int i = 0; i < 10; i++) { for (int l = 0; l < 10; l++) { pastArr[i][l] = lastArr[i][l]; } } //Sets the last iterations array, so the lifeArray can change without affecting current loop for (int i = 0; i < 10; i++) { for (int l = 0; l < 10; l++) { lastArr[i][l] = lifeArr[i][l]; } } //Loop checks the nieghbors of each iteration, if in bounds, for life, adds to counter for (int i = 0; i < 10; i++) { for (int l = 0; l < 10; l++) { int n = 0; if (i - 1 >= 0 && l - 1 >= 0 && lastArr[i - 1][l - 1] == true) { n++; } if (i - 1 >= 0 && lastArr[i - 1][l] == true) { n++; } if (i - 1 >= 0 && l + 1 < 10 && lastArr[i - 1][l + 1] == true) { n++; } if (l - 1 >= 0 && lastArr[i][l - 1] == true) { n++; } if (l + 1 < 10 && lastArr[i][l + 1] == true) { n++; } if (l - 1 >= 0 && i + 1 < 10 && lastArr[i + 1][l - 1] == true) { n++; } if (i + 1 < 10 && lastArr[i + 1][l] == true) { n++; } if (i + 1 < 10 && l + 1 < 10 && lastArr[i + 1][l + 1]) { n++; } // Counter changes lifeArr value based on neighbor count and rules if (lastArr[i][l] == true && n > 3) { lifeArr[i][l] = false; } else if (lastArr[i][l] == true && n < 2) { lifeArr[i][l] = false; } else if (lastArr[i][l] == false && n == 3) { lifeArr[i][l] = true; } } } }``````

Here's the output when i try to play it

1 2 3 4 5 6 7 8 9 10
1 - - - - - - - - - -
2 - X - - - - - - X -
3 - - X - - - - X - -
4 - - - X - - X - - -
5 - - - - X X - - - -
6 - - - - X X - - - -
7 - - - X - - X - - -
8 - - X - - - - X - -
9 - X - - - - - - X -
10 - - - - - - - - - -

Generation: 1
1 2 3 4 5 6 7 8 9 10
1 - - - - - - - - - -
2 - - - - - - - - X -
3 - - X - - - - X - -
4 - - - X - - X - - -
5 - - - - - - - - - -
6 - - - - - - - - - -
7 - - - X - - X - - -
8 - - X - - - - X - -
9 - X - - - - - - - -
10 - - - - - - - - - -

Generation: 2
1 2 3 4 5 6 7 8 9 10
1 - - - - - - - - - -
2 - - - - - - - - X -
3 - - - - - - - X - -
4 - - - - - - X - - -
5 - - - - - - - - - -
6 - - - - - - - - - -
7 - - - X - - - - - -
8 - - X - - - - - - -
9 - X - - - - - - - -
10 - - - - - - - - - -

Generation: 3
1 2 3 4 5 6 7 8 9 10
1 - - - - - - - - - -
2 - - - - - - - - X -
3 - - - - - - - X - -
4 - - - - - - X - - -
5 - - - - - - - - - -
6 - - - - - - - - - -
7 - - - X - - - - - -
8 - - X - - - - - - -
9 - X - - - - - - - -
10 - - - - - - - - - -
Try this:

 ``12345678910111213`` `````` const int Size = 10; // global const int HI = Size - 1; int n = (i > 0 & l > 0 & lastArr[i - 1][l - 1]) + (i > 0 & lastArr[i - 1][l ]) + (i > 0 & l < HI & lastArr[i - 1][l + 1]) + ( l > 0 & lastArr[i ][l - 1]) + ( l < HI & lastArr[i ][l + 1]) + (i < HI & l > 0 & lastArr[i + 1][l - 1]) + (i < HI & lastArr[i + 1][l ]) + (i < HI & l < HI & lastArr[i + 1][l + 1]); lifeArr[i][l] = lastArr[i][l] ? (n == 2 || n == 3) : (n == 3);``````

Last edited on
So did that work?

***EDIT - Just to update so everyone knows this is solved. I ended up rewriting the program without a game object. Most of the code is the same, and it worked once I split the copying of the arrays into their own function. No clue why but yeah

Thank you for helping dutch! Sorry for the delay, I needed a break.

Your code doesn't work exactly as needed, I'll put my output below

In your code, the block that increments the n value, is there anything really different about it besides it being much cleaner?

Also, the operation at line 13 does the entire work of my bottom if statements, yeah?

For the output, copying it over messes it up a bit so I hope it makes sense. If you look at the top right section of the starting board pattern, the very top right x dies as it should. However, life is created in the positions right next to and right below that corner. This shouldn't be the case, because those cells had only two neighbors. I've been messing around trying to find out why

1 2 3 4 5 6 7 8 9 10
1 - - - - - - - - - -
2 - X - - - - - - X -
3 - - X - - - - X - -
4 - - - X - - X - - -
5 - - - - X X - - - -
6 - - - - X X - - - -
7 - - - X - - X - - -
8 - - X - - - - X - -
9 - X - - - - - - X -
10 - - - - - - - - - -

Generation: 1
1 2 3 4 5 6 7 8 9 10
1 - - - - - - - - - -
2 - - X - - - - X - -
3 - X X X - - X X X -
4 - - X X X X X X - -
5 - - - X - - X - - -
6 - - - X - - X - - -
7 - - X X X X X X - -
8 - X X X - - X X X -
9 - - X - - - - X - -
10 - - - - - - - - - -

Generation: 2
1 2 3 4 5 6 7 8 9 10
1 - - - - - - - - - -
2 - X X X - - X X X -
3 - X - - - - - - X -
4 - X - - - - - - X -
5 - - - - - - - - - -
6 - - - - - - - - - -
7 - X - - - - - - X -
8 - X - - - - - - X -
9 - X X X - - X X X -
10 - - - - - - - - - -

Generation: 3
1 2 3 4 5 6 7 8 9 10
1 - - X - - - - X - -
2 - X X - - - - X X -
3 X X - - - - - - X X
4 - - - - - - - - - -
5 - - - - - - - - - -
6 - - - - - - - - - -
7 - - - - - - - - - -
8 X X - - - - - - X X
9 - X X - - - - X X -
10 - - X - - - - X - -

Last edited on
Topic archived. No new replies allowed.