Game of Life - Style gone bad


1 reply [Last post]
LesMac88
LesMac88's picture
Offline
Joined: 2012-05-03
Points: 0

Howdie,

First of all id like to say that I have been loving the vidieos and am ashamed that it took me until I had a problem to sign up! 

I have been following through the C course and have come a little stuck on a issue which is baffling me somewhat.

When I compiled and run towards the end of the tutorials, the "little men" (\f) were not disapearing during each generation and athough the min went down till it stabalized I ended up with a ("Zombie filled world") with the dead members refusing to leave.

I tried tracking it down and found some other errors that would of caused some problems including one that made the entire grid full but still couldnt find out the issue.

I then signed up and downloaded your source code to compare, after about 30 mins, I was well and truly stumped so I sectioned up the wibit code and systematically replaced mine with it until I found what was causing the problem. 

From one of the earlier videos I remember hearing that you can always include a functions{} or leave it out if it is a single statement (I also liked notepad++ position highlight so have been adding them where I can, which infact caused this problem).

 

On line 108 of the Wibit Code lives this if statment:

 

if(tempGrid[i][j] == 1) population++;

g[i][j] = tempGrid[i][j];

 

 

Now when following through I put:

if(tempGrid[i][j] == 1) 

{

population++;

g[i][j] = tempGrid[i][j];

}

 

Which is causing my zombie apocolipse, removing the {} fixes this problem but I am unsure of why?

I am thinking along the lines of once included in the block statement it scopes it so it is not available where needed. 

Another small confusion is why this effects the persistance of the "zombie" characters, unless it in itself is breaking something that returns the array to 0. 

Please forgive me if this is a stupid question, completely new to programming but you never know unless you ask, and I prefer to be the fool that asks instead of the one sitting in silence.

Cheers,

Les

 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nowaddhero
nowaddhero's picture
Offline
Joined: 2012-01-03
Points: 0

Les, I'm not sure on this in C but it is so in Java, if you do not supply curly braces after an if statement it reads the very next statement and only the very next statement. So for an example of the code you provided.

if(tempGrid[i][j] == 1) population++;
g[i][j] = tempGrid[i][j];

Is the same as writing:

if(tempGrid[i][j] == 1)
{
population++;
}
g[i][j] = tempGrid[i][j];