Hi I've got most of the code finished but I can't for the life of me figure out how to correctly count neighbors for the edges and corners of the array.
These are the rules.

If the cell is currently empty:
If the cell has exactly three living neighbors, it will come to life in the next generation.
If the cell has any other number of living neighbors, it will remain empty.
If the cell is currently living:
If the cell has one or zero living neighbors, it will die of loneliness in the next generation.
If the cell has four or more living neighbors, it will die of overcrowding in the next generation.
If the cell has two or three neighbors, it will remain living.
All births and deaths occur simultaneously.

Any help would be greatly appreciated. I believe my problems are in the counting neighbors functions and maybe the next generation one as well.no

``````#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int SIZE = 20;

void initGrid(bool life[][SIZE], bool life2[][SIZE]);
void printGrid(bool life[][SIZE], bool life2[][SIZE]);
int  LiveNeighbors(bool life[][SIZE], bool life2[][SIZE],int,int);
void NextGenerations(bool life[][SIZE], bool life2[][SIZE]);

int main()
{
bool life[SIZE][SIZE];
bool life2[SIZE][SIZE];
printGrid(life,life2);
for (int count = 0; count < 5; count++)
{
NextGenerations(life,life2);
}

printGrid(life,life2);
system("PAUSE");
return 0;
}

{
ifstream infile("bacteria.txt");

int numBacteria, row, col;

initGrid(life,life2);

infile >> row >> col;
while (infile){
life[row][col] = true;
infile >> row >> col;
}
infile.close();
}

void initGrid(bool life[][SIZE], bool life2[][SIZE])
{
for (int row = 0; row < SIZE; row++){
for (int col = 0; col < SIZE; col++){
life[row][col] = false;
}
}
for(int row=0; row<SIZE;row++){
for(int col=0; col<SIZE; col++){
life2[row][col]=false;
}
}
}

void printGrid(bool life[][SIZE], bool life2[][SIZE])
{
cout << "  01234567890123456789" << endl;
for (int row = 0; row < SIZE; row++){
cout << setw(2) << row;
for (int col = 0; col < SIZE; col++){
if (life[row][col]){
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
}

int LiveNeighbors(bool life[SIZE][SIZE], int row, int col)
{
int neighbors=0;
if(life[row][col]==true && life[row][col-1]==true && col-1>=0)
{
neighbors++;
}
if(life[row][col]==true && life[row+1][col-1]==true && row+1<SIZE && col-1>=0)
{
neighbors++;
}

if(life[row][col]==true && life[row+1][col]==true && row+1<SIZE)
{
neighbors++;
}
if(life[row][col]==true && life[row][col+1]==true && col+1<SIZE)
{
neighbors++;
}
if(life[row][col]==true && life[row-1][col-1]==true && row-1>=0 && col-1>=0)
{
neighbors++;
}
if(life[row][col]==true && life[row+1][col+1]==true && row+1<3 && col+1<SIZE)
{
neighbors++;
}
if(life[row][col]==true && life[row-1][col]==true && row-1>=0)
{
neighbors++;
}
if(life[row][col]==true && life[row-1][col+1]==true && row-1>=0 && col+1<SIZE)
{
neighbors++;
}

return neighbors;
}

{
int neighbors2=0;
if(life[row][col]==true && life[row][col-1]==true && col-1>=0)
{
neighbors2++;
}
if(life[row][col]==true && life[row+1][col-1]==true && row+1<SIZE && col-1>=0)
{
neighbors2++;
}

if(life[row][col]==true && life[row+1][col]==true && row+1<SIZE)
{
neighbors2++;
}
if(life[row][col]==true && life[row][col+1]==true && col+1<SIZE)
{
neighbors2++;
}
if(life[row][col]==true && life[row-1][col-1]==true && row-1>=0 && col-1>=0)
{
neighbors2++;
}
if(life[row][col]==true && life[row+1][col+1]==true && row+1<3 && col+1<SIZE)
{
neighbors2++;
}
if(life[row][col]==true && life[row-1][col]==true && row-1>=0)
{
neighbors2++;
}
if(life[row][col]==true && life[row-1][col+1]==true && row-1>=0 && col+1<SIZE)
{
neighbors2++;
}

return neighbors2;

}

void NextGenerations(bool life[][SIZE], bool life2[][SIZE])
{
int neighbors=0;
int neighbors2=0;

for (int row=0;row<SIZE;row++)
{
for (int col=0;col<SIZE;col++)
{
neighbors=LiveNeighbors(life,row,col);

if (neighbors<=1)
life2[row][col]=false;
if (neighbors>=4)
life2[row][col]=false;
if (neighbors==2)
life2[row][col]=true;
if (neighbors==3)
life2[row][col]=true;

}
}

for (int row=0;row<SIZE;row++)
{
for (int col=0;col<SIZE;col++)
{
if (neighbors2==3)
life2[row][col]=true;
}
}
for(int row=0;row<SIZE;row++)
{
for(int col=0; col<SIZE;col++)
life[row][col]=life2[row][col];
}

}
``````

## All 10 Replies

Do you have the .txt file?

0 0
0 3
0 5
0 9
0 19
1 6
1 14
1 16
2 4
2 7
2 11
2 14
2 15
2 16
2 18
3 2
3 5
3 9
3 10
3 17
3 19
4 0
4 3
4 5
4 9
5 2
5 3
5 5
5 15
5 16
5 17
6 5
6 6
6 10
6 11
6 13
6 14
6 17
6 18
7 6
7 8
7 14
7 16
8 1
8 5
8 8
8 10
8 17
9 3
9 4
9 6
9 7
10 0
10 9
10 19
11 1
11 2
11 3
11 5
11 7
11 8
11 14
11 17
11 19
12 4
12 5
12 6
12 8
13 0
13 1
13 7
13 10
13 11
13 14
13 15
13 16
13 17
13 18
14 2
14 11
14 13
14 15
14 17
14 19
15 11
15 13
15 18
16 1
16 2
16 5
16 8
16 16
17 5
17 6
18 6
18 16
18 18
19 0
19 3
19 4
19 7

These are the values in the .txt file.

couln't you just specify which part of the row and column you are trying count?

``````ex: area[0][0]
[0][9], etc ?
``````

since you already know which parts you're trying to calculate?

I just have no idea what kinds of changes I have to make in order to count the corners since it only has three potential neighbors. Do I just add another if statement to the counting neighbors function?

it doesn't appear to count anything just display, if you edit you last for statement to include count.

I think something like:

``````for(int row=0;row<SIZE;row++)
{
for(int col=0; col<SIZE;col++)
life[row][col]=life2[row][col];

int total;

total += life[row][col] //specify here what area to count?

cout << total;
}
``````

not sure, just got into c++ a few months ago

That function is to display once the counting is done in the deadneighbor and alive neighbor functions. I'm thinking that I need to add something to those.

First off, DeadNeighbors seems useless and redundant. Unless I'm missing something at a glance, it's exactly the same as LiveNeighbors. So, get rid of it.

--

Now, let's slow down and consider lines 85 to 89:

``````if (life[row][col] == true && life[row][col-1] == true && col-1 >= 0) {
neighbors++;
}
``````

Why is `life[row][col]==true` in there? If you wanted to check for it, create one check at the beginning. Don't put it in every single if statement! Let me push it further though. Consider what it means: "If this cell is dead, it has no neighbours." This means that once a cell is dead, it has no chance of being alive again! That's not what you want! So get rid of all of your `life[row][col]==true`.

--

`col-1 >= 0` This should work for checking the edges. What is the issue you're having exactly with this?

--

What do lines 188 to 196 do?

``````for (int row=0;row<SIZE;row++) {
for (int col=0;col<SIZE;col++) {
if (neighbors2==3)
life2[row][col]=true;
}
}
``````

You have already decided who lives and dies just before that. And the loop afterwards, you apply those changes. This loop is completly useless. Remove it.

I mispoke when I said "You have already decided who lives and dies just before that." You need to make a small change.

``````if (neighbors<=1)
life2[row][col]=false;
if (neighbors>=4)
life2[row][col]=false;
if (neighbors==2)
life2[row][col]=true;      // You only do this if there are 2 negihbours and it's already alive.
if (neighbors==3)
life2[row][col]=true;
``````

Also, using else if is faster. (and sometimes using it makes logic simpler as well).

Alright I did `wile(life[row][col]==true)` so that it isn't in every if statement and seems to work fine. I'm just confused as to how to do edges and corner neighbor counting. Do I need an if statement for each corner and edge?

Alright I did wile(life[row][col]==true) so that it isn't in every if statement

If I'm understanding your code correctly, you should remove it completly. Let me phrase it like this: LiveNeighbors tells you how many neighbours a cell has, wether it's dead or alive. So why are you checking if it's alive? Now dead cells always have 0 neighbours (incorrect), thus eare never born.

For example, given:

``````0 0
0 2
2 2
``````

the result after one iteration should be:

``````1 1
``````

I'm just confused as to how to do edges and corner neighbor counting. Do I need an if statement for each corner and edge?

In theory, Conways Game of Life has no edges. Of course, that is impossible for a program to do. So, you need to make a decision about how you want to handle it. The simplest way is to assume that all cells off the grid are dead. That is the same as saying that LiveNeighbours does not count them.

Oh! Look at that!

``````if (life[row][col-1]==true && col-1>=0)
neighbors++;
``````

You code is already not counting neighbours outside of the grid. Thus, your program should already work as expected for the edges! Unless you are having a problem?

If you are having a problem, then show us! Create a small grid (say, 5x5) and show us what the output is, and what it should be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.