So I am working on the Game of life..I have most of it down but I am COMPLETELY stumped when it comes to applying the rules to the game.. I am trying to get all the standard rules as follow;
Living cell dies of overcrowding if next generation has >4 neighrbors
Living cell dies of loneliness in the next generation if it has <1 neighbors
An Empty cell becomes a birth cell if it has 3 living neighbors in the next generation
All other cells remain unchanged..
I don't even know where to begin..I have 90% of the program written but applying the rules is KILLING me! Can someone help point me in the right direction?
I grabbed this code off the webs and I've been trying to figure it out, but how can I make the the cells count neighbors? I am comfortable somewhat with the for and if/else loop but I'm completely lost on its implementation in this instance..but I can't put it down lol.
Not really. At least not without doing all the work for you.
I'll have to set "aliveneighbors" as a bool right?
It's an int (as illustrated in my code). Maybe "numberofaliveneighbors" would be more descriptive, but I figured that was too long.
The idea is, you count the number of alive neighbors by checking each neighbor and adding 1 to 'aliveneighbors' for each neighbor that is alive.
Once you check all neighbors, you can use your 'aliveneighbors' value to determine what action to take (either birth the cell if == 3, or kill the cell if > 4, or kill if < 1, or whatever the rules are)
Can you provide me a small example of how to intiliaze this?
err... I just did. =P
1 2 3 4 5 6 7 8 9 10 11 12
// our variable which counts the number of neighbors which are alive
int aliveneighbors = 0;
// check each neighbor to see if they're alive
if( cell_above_this_one_is_alive ) // check neighbor above us to see if it's alive
aliveneighbors += 1; // if it is alive, count it
if( cell_below_this_one_is_alive ) // check neighbor below us
aliveneighbors += 1; // if alive, count it
// repeat for all neighbors
Now, I would put that in a function so that it can be more easiliy used. Hopefully you already learned about functions:
That's ridiculous. Writing the game of life without functions is borderline insanity.
I would say I don't really believe you, but I've seen far worse in some programming courses, so you never know....
How can I "kill the cell"?
It depends entirely on how you're representing each cell.
Basically each cell would have a variable indicating whether it's alive or dead. To kill it, you just change it's status to dead.
Although the more and more I speak with you, the more I see how ill equipped you are to handle this problem. I don't think anyone here will be able to help you other than handing out solutions (and that isn't really help).
You really need to either go to your teacher/professor and tell him you don't understand the assignment, or get a private tutor who can walk you through all of this.
It is a once a week class we meet for 3 hours at a time, last week we did Arrays and we are supposed to get into functions this week(when this program is due).
I'll go to the PC lab before class and try to get help, but thanks anyways!
Also, you can try reading the documentation on this website. If someone like me can self-teach C++ with no formal classes, I'm sure you'll have it in no time!