Hi all!

You've probably all heard of some rendition of the game of life and now it's my turn to try and code it.:-/

Anyway, everything is fine up until my Life function(this is the function where I put all the "rules" of life). My output is now completely blank. I checked halfway through making all my if statements and half of the 1st row and half of the 2nd row was completely filled with *'s. After I finished writing my if statements, everything was blank. I knew something was wrong halfway through but I can't find a mistake in my logic. So, I need a fresh pair of eyes. lol! What am I doing wrong???
Below is my Life function code:

``````void Life(bool bacteria[][COL])
{
bool nextGenbacteria[ROW][COL];
int neighbors=0;

for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{
if(bacteria[row][col]==true&&bacteria[row][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==false&&bacteria[row][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col+1]==true)
{
}

if(neighbors<2||neighbors>3)
{
nextGenbacteria[row][col]=false;
}
if(neighbors==2||neighbors==3)
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=false;
}
}
}

for(int x=0;x<ROW;x++)
{
for(int y=0;y<COL;y++)
{
bacteria[x][y]=nextGenbacteria[x][y];
}
}
}``````

And attached is a .pdf file of my assignment.(I just noticed the nifty manage attachments button):\$

## All 17 Replies

Me again... :) ROW is not in scope in your function (I checked your other post). You'll want to either put it through a debugger (or put in a temporary int variable to count your loops or print out something etc) to make sure it goes through enough loops. It could be that it's skipping the if statements completely.

Also, you don't need to use == with something that's boolean, it already resolves to true or false.

Hi jonsca! I checked what you said by putting in a counter and the first for loop goes for 20 like it's supposed to but the second for loop went 400 times!!! :-O I don't even understand that. I gave both ROW and COL global scope too. I'll post my entire code and the input file. As always, thanks for your help!

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

const int ROW=20;
const int COL=20;
void initLife(bool bacteria[][COL]);
void printLife(bool bacteria[][COL]);
void Life(bool bacteria[][COL]);

int main()
{
bool bacteria[ROW][COL];

Life(bacteria);
printLife(bacteria);

system("pause");
return 0;

}

//*************************************************************************
{
ifstream infile;    //declare a file variable
int row;
int col;

infile.open("input.dat");     //open a file
assert(infile);               //make sure file opened

initLife(bacteria);           //initialize 2D array to false

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

//***************************************************************************
void initLife(bool bacteria[][COL])
{
for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{
bacteria[row][col]=false;
}
}
}

//****************************************************************************
void printLife(bool bacteria[][COL])
{
cout << "  01234567890123456789" << endl;
for (int row=0;row<ROW;row++)
{
cout <<setw(2)<< row;
for (int col=0;col<COL;col++)
{
if (bacteria[row][col]==true)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
cout<<'\n';
}

}
//***************************************************************************
void Life(bool bacteria[][COL])
{
bool nextGenbacteria[ROW][COL];
int neighbors=0;

for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{
if(bacteria[row][col]==true&&bacteria[row][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==false&&bacteria[row][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col+1]==true)
{
}

if(neighbors<2||neighbors>3)
{
nextGenbacteria[row][col]=false;
}
if(neighbors==2||neighbors==3)
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=false;
}
}
}

for(int x=0;x<ROW;x++)
{
for(int y=0;y<COL;y++)
{
bacteria[x][y]=nextGenbacteria[x][y];
}
}
}``````

This is in my input file:
0 0
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 17
17 5
17 6
18 6
18 16
18 18
19 0
19 3
19 4
19 7

Me again... :) ROW is not in scope in your function (I checked your other post). You'll want to either put it through a debugger (or put in a temporary int variable to count your loops or print out something etc) to make sure it goes through enough loops. It could be that it's skipping the if statements completely.

The reason you're not getting any output is because by the time you've got to the end of the Life function, all of your original generation of bacteria are utterly annihilated, dead, gone, pushing up the daisies, wormfood! heh heh! :)

The problem is this section of code in your life function:

``````...
void Life(bool bacteria[][COL])
{
bool nextGenbacteria[ROW][COL];
int neighbors=0;

for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{
if(bacteria[row][col]==true&&bacteria[row][col-1]==true)
{
...``````

The two counter variables 'neighbours' and 'neighboursDead' have been declared before the two nested for loops.

As a result of this, the counters are global to both for loops and the counters are incrementing upwards during each iteration of the loops. So before the first iteration, the value of neighboursDead is 0, but by the final iteration the value of neighboursdead has counted up to something like 49.

Basically the counters are storing the total number of neighbours and dead neighbours for all iterations so far (So it incrementally stores the numbers for every single row and column).
Because of this, in accordance with your rules of life, all of your bacteria are being annihilated.

What you really want to be doing is counting the neighbours and dead neigbours for each column of the current row and then reset the counters before counting those in the next column.
So you need to ensure that both counters are reset each time your second loop reiterates.
If you move their declarations down a few lines into the 2nd for loop your counters will be reset each time the loop reiterates and your counters will actually contain values that will allow life to be sustained! ;)

So move the declarations of the counters down a few lines so your code looks like this:

``````...
void Life(bool bacteria[][COL])
{
bool nextGenbacteria[ROW][COL];

for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{
int neighbors=0;
if(bacteria[row][col]==true&&bacteria[row][col-1]==true)
{
...``````

And your program will actually be able to sustain life! Heh heh! :)

Cheers for now,
Jas.

Thanks JasonHippy! I finally have life happening. lol! And I understand why my program was doing that. Thanks again!

...now I have to modify my "rules of life" because, although I am getting "life" I am not getting the output I know I should.

Okay, I'm almost there! Thanks again for everyone's help. Now, I've changed a couple of things and narrowed down one last bug. It has something to do with the edges of the array. For example, the output on row 12 has an * at column 19 when it should be in column 0. I suppose I need to add another if statement to address those cases, but I'm not sure what the conditions would be. So, when

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

sometimes(like on row 12) the * is placed at 19 when it should be at 0.
Here is my entire Life function(revised):

``````void Life(bool bacteria[][COL])
{
bool nextGenbacteria[ROW][COL];

for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{
int neighbors=0;
if(bacteria[row][col]==true&&bacteria[row][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==false&&bacteria[row][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col+1]==true)
{
}

if(neighbors<2||neighbors>3&&bacteria[row][col]==true)
{
nextGenbacteria[row][col]=false;
}
if(neighbors==2||neighbors==3&&bacteria[row][col]==true)
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=false;
}
}
}

for(int x=0;x<ROW;x++)
{
for(int y=0;y<COL;y++)
{
bacteria[x][y]=nextGenbacteria[x][y];
}
}
}``````

Ideas? Thanks!

Oh, and here are the rules again:

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.

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

becomes

``````if(bacteria[row][col]&&bacteria[row-1][col])
{
neighbors++;
}``````

(and anything X == false becomes !X)
Back to your question, are you saying that your board should wrap around, that is, cols 0 and 19 should affect each other? I think you'd have to take care of a special case rather than modifying all your existing if statements (by putting a %20 to your indicies for example and correcting your -1 index, there may be a clever way to do that which is beyond me, but you see the potential for a mess). I would just run your actual for loop from 1 to 18 and have another for loop over the rows of cols 0 and 19.

I did what you said regarding the == and I got more wrong cells...not sure why so I put em all back for now.

To answer your question, columns 0 and 19 shouldn't affect each other. I believe this is the problem. When the loop gets to this at column 19:

``````if(bacteria[row][col]==true&&bacteria[row][col+1]==true)
{
neighbors++;
}``````

I think it's reading column 0 to find out if there is a neighbor but I don't want it to. I'm going to attempt to do what you said as far as running the loop from 1-18 and then doing a different one for 0 and 19. Thanks for the pointers again! I'll be back. lol!

Okay, so I changed the original for loop from:

``````for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{ [I]rest of code blah blah[/I]``````

to this:

``````for(int row=1;row<ROW-1;row++)
{
for(int col=1;col<COL-1;col++)
{ [I]the rest of code here[/I]``````

I imagine this processes everything from row 1, col 1 to row 18, col 18-leaving a border around the entire array unprocessed.

But how in the world would I write the loop to address just the border??
I tried this:

``````for(int row_2=0;row_2<20;row_2++)
{
for(int col_2=0;col_2<1;col_2++)
{ [I]different rules for here...blah blah[/I]``````

I'm trying to cover the entire right border for now(row 0-19 but just col 0).
But something just isn't clicking for me...

I did what you said regarding the == and I got more wrong cells...not sure why so I put em all back for now.

I was afraid it might goof you up... well, for next time when you don't have a zillion if /else in there.

I believe this is the problem. When the loop gets to this at column 19:

``````if(bacteria[row][col]==true&&bacteria[row][col+1]==true)
{
neighbors++;
}``````

I think it's reading column 0 to find out if there is a neighbor but I don't want it to.

It shouldn't be. If it steps over the end of your array your program should crash. I can't remember if there's anything in your rules about boundary conditions...I haven't tested your posting of your entire code I'll run it if I have a chance.

It shouldn't be. If it steps over the end of your array your program should crash. I can't remember if there's anything in your rules about boundary conditions...I haven't tested your posting of your entire code I'll run it if I have a chance.

Oh yeah! Duh!! So that can't be the problem.:-/
There's nothing in my rules about boundary conditions. And I'll just post my entire code here for you along with the input and I'm going to attach a .doc file of what the output should look like.

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

const int ROW=20;
const int COL=20;
void initLife(bool bacteria[][COL]);
void printLife(bool bacteria[][COL]);
void Life(bool bacteria[][COL]);
//bool bacteria[ROW][COL];

int main()
{
bool bacteria[ROW][COL];
//bool nextGenbacteria[ROW][COL];

Life(bacteria);
printLife(bacteria);

system("pause");
return 0;

}

//*************************************************************************
{
ifstream infile;    //declare a file variable
int row;
int col;

infile.open("input.dat");     //open a file
assert(infile);               //make sure file opened

initLife(bacteria);           //initialize 2D array to false

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

//***************************************************************************
void initLife(bool bacteria[][COL])
{
for(int row=0;row<ROW;row++)
{
for(int col=0;col<COL;col++)
{
bacteria[row][col]=false;
}
}
}

//****************************************************************************
void printLife(bool bacteria[][COL])
{
cout << "  01234567890123456789" << endl;
for (int row=0;row<ROW;row++)
{
cout <<setw(2)<< row;
for (int col=0;col<COL;col++)
{
if (bacteria[row][col]==true)
{
cout<<"*";
}
else
{
cout<<" ";
}
}
cout<<'\n';
}

}
//***************************************************************************
void Life(bool bacteria[][COL])
{
bool nextGenbacteria[ROW][COL];

for(int row=1;row<ROW-1;row++)
{
for(int col=1;col<COL-1;col++)
{
int neighbors=0;
if(bacteria[row][col]==true&&bacteria[row][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row+1][col-1]==true)
{
neighbors++;
}
if(bacteria[row][col]==true&&bacteria[row-1][col+1]==true)
{
neighbors++;
}
if(bacteria[row][col]==false&&bacteria[row][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col+1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row+1][col-1]==true)
{
}
if(bacteria[row][col]==false&&bacteria[row-1][col+1]==true)
{
}
//*************************************************************
//to process only the border
for(int row_2=0;row_2<20;row_2++)
{
for(int col_2=0;col_2<1;col_2++)
{
if(bacteria[row_2][col_2]==true&&bacteria[row_2][col_2+1]==true)
{
neighbors++;
}
if(bacteria[row_2][col_2]==true&&bacteria[row_2+1][col_2]==true)
{
neighbors++;
}
if(bacteria[row_2][col_2]==true&&bacteria[row_2-1][col_2]==true)
{
neighbors++;
}
if(bacteria[row_2][col_2]==true&&bacteria[row_2+1][col_2+1]==true)
{
neighbors++;
}
if(bacteria[row_2][col_2]==true&&bacteria[row_2-1][col_2+1]==true)
{
neighbors++;
}
}
}
//*******************************************************************

if(neighbors<2||neighbors>3&&bacteria[row][col]==true)
{
nextGenbacteria[row][col]=false;
}
if(neighbors==2||neighbors==3&&bacteria[row][col]==true)
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=true;
}
{
nextGenbacteria[row][col]=false;
}
}
}

for(int x=0;x<ROW;x++)
{
for(int y=0;y<COL;y++)
{
bacteria[x][y]=nextGenbacteria[x][y];
}
}
}``````

Input file:
0 0
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 17
17 5
17 6
18 6
18 16
18 18
19 0
19 3
19 4
19 7

for (int row = 0;row<ROW;row++)
col = 0;
test for this column only (don't test any col-1 points)

for (int row = 0;row<ROW;row++)
col = 19
test for this column only (don't test any col+1 points)

What I'm trying to figure out now is how [row][col-1] for col = 0 is not crashing in your program as it is.

I think we're leapfrogging posts lol.

In fact, now that I think about it further, you need to have specialized sections for row = 0 and row = 19 also. I was testing some points that should be out of bounds on your array and many of them are initialized to 0 which is probably the biggest factor in messing up your counts. So in summary for your "only row" for loops take out the statements with col-1 for col 0 and those with col+1 for col 19, and for those in row = 0 take out the terms with row-1 and those in row 19 take out the terms with row+1.

Your logic makes sense to me...lets see what happens. Be back in a few. Thanks!

Sooo, I did all of that and now my output is even farther off...I'm utterly lost now

I got this, it's a bit closer. I need to reread the rules as I haven't actually checked all of your if statements versus them. Go through the if statements line-by-line to see if they match. Small changes in these rules can cause large changes in the game.

...going through my code again...i'll be back. lol!

AAAAAAAAAAAAAAHHHHHHHHHHHHHHHHHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

Okay, I'm finally finished. I got it! You won't believe what I had to do(or maybe you would). It was so much. And logically it all makes sense now. lol! It's just shy of midnight and now I can concentrate on studying for orgo instead of pounding my head over this program.

Thanks again jonsca and JasonHippy!!!

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.