# Thread: Assistance with some homework, please?

1. ## Assistance with some homework, please?

Hello everyone!

I have here my last homework assignment for an introductory C++ programming class. My skill level in this class ended about 4 weeks ago, so I'm have a terrible time with this last assignment.

I've searched the board and found a few threads related to this exact same program, but due to my limited knowledge in this stuff, I couldn't apply any of it to my code to find a fix.

It's another "Game of Life" type program I'm sure many of you are familiar with and probably consider pretty simple...I wish I did!

Anyway, here is the assignment instructions and my code to follow:

Write a program to simulate life.

The world is a rectangular grid of cells, each of which may contain an

organism.

Each cell has eight neighbors, like so:

1 2 3

4 * 5 (the numbers are for illustration purposes

only)

6 7 8

The world is initially created by the user with some organisms at

various

cells. The user specifies the cells that are initially "alive".

Then successive generations are obtained by two rules:

1) an organism in a cell survives to the next generation if exactly

2 or 3 of its neighbors are living, otherwise it dies

2) an organism is born into an unoccupied cell if exactly 3 of its

neighbors are occupied.

Display the generations under user command until the user grows weary

of this world.

Method:

The world will be represented by a 2-dimensional array (which may not

be a global variable). 20 by 20 is a good size.

Each component of the array will represent a cell that has a living

organism in it or is dead.

A new generation is generated by examining each cell and creating its

next state (living or dead) simultaneously with all the other cells,

thus the next generation must be created into a copy of the world.

To simplify the code, the "perimeter" of the world can always be dead

cells.

And here's my code. Only problem is, I can't figure out the algorithm

for

Option 2:

2) an organism is born into an unoccupied cell if exactly 3 of its

neighbors are occupied.
And the code:

Code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cctype>

const int MAX_SIZE = 20;

void printGen(int[][MAX_SIZE]);
void createGen(int[][MAX_SIZE]);

using namespace std;

int main()
{
int lifeGen[MAX_SIZE][MAX_SIZE];
int row, col;
int x = 0;
int y = 0;
char cont;

//Initialize array by setting all blocks to 0
for(row = 0; row < MAX_SIZE; row++)
for(col = 0; col < MAX_SIZE; col++)
lifeGen[row][col] = 0;

//Initial instructions for inputting coordinates.
cout << "Enter the coordinates to plot cell locations.  Input " << endl;
cout << "results in the format of:" << endl << endl;
cout << "10 15" << endl << endl;
cout << "When finished, enter coordinates with at least one being negative." << endl;

//Loop for entering coordinates until user enters in a negative one.
do
{
cout << "Enter coordinates: ";
cin >> x;
cin >> y;
if(x > MAX_SIZE || y > MAX_SIZE)
{
cout << "Coordinates must be from 0 - 20!  Reenter coordinates: ";
cin >> x >> y;
}
else
lifeGen[x - 1][y - 1] = 1;
}
while(x > 0 && y > 0);

printGen(lifeGen);

//User-controlled loop for outputting new generations.
do
{
cout << endl << endl;
cout << "Press any key and 'Enter' to continue with next generation, " << endl;
cout << "or press 'Q'  and 'Enter' to quit: ";
cin >> cont;
if(toupper(cont) == 'q')
{
cout << "Thanks for trying my life simulator!  Bye!" << endl;
return 0;
system("PAUSE");
}
else
{
createGen(lifeGen);
printGen(lifeGen);
}
}
while(cont);

return EXIT_SUCCESS;
}

//Function to output life generation on screen.
void printGen(int lifeGen[][MAX_SIZE])
{
int rows;
int cols;

do
{
for(rows = 0; rows < MAX_SIZE; rows++)
for(cols = 0; cols < MAX_SIZE; cols++)
{
if(cols % 2 == 0)
cout << endl;
if(lifeGen[rows][cols] == 1)
cout << "*";
else if(lifeGen[rows][cols] != 1)
cout << " ";
}
}
while(cols < MAX_SIZE);
}

//Function to create new generations of cells.
void createGen(int lifeGen[][MAX_SIZE])
{
int r;
int c;
int totalrc = 0;

for(r = 0; r < MAX_SIZE; r++)
for(c = 0; c < MAX_SIZE; c++)
{
if(lifeGen[r][c] == 1)
totalrc = lifeGen[r - 1][c - 1] + lifeGen[r - 1][c] + lifeGen[r - 1][c + 1]
+ lifeGen[r][c - 1] + lifeGen[r][c + 1] + lifeGen[r + 1][c - 1]
+ lifeGen[r][c - 1] + lifeGen[r + 1][c] + lifeGen[r + 1][c + 1];
if(totalrc <= 1 || totalrc >= 4)
lifeGen[r][c] = 0;
else if(totalrc == 3)
lifeGen[r][c] = 1;
}
}
I hope it's not a total disaster. As is, this doesn't output anything and I don't know why. I found an example online that I followed which did work, somewhat, but for some reason mine doesn't at all.

Any help would be greatly appreciated!

2. Couple of things...

I see output, however it's incorrectly displayed.
Code:
if(cols % 2 == 0)
Should be if(cols % 20 == 0), or more accurately if(cols % MAX_SIZE == 0). As it is now, you're outputting the array into 2 columns. You should also consider the logic of the printGen() function, and determine when the newline should be printed.

Another output problem is
Code:
createGen(lifeGen);
printGen(lifeGen);
You should swap those statements. The first generation is never displayed to show the initial state.

Additionally, you should have a second array to copy the results of each generation into (As the project states you should anyway). As it is now, you're editing the array elements sequentially rather than simultaneously, which affects (usually detrimentally) the outcome of each generation. In effect, you're killing off all the organisms almost immediately.

And one last item, you should set the array size to be 2 elements greater than the range you want to represent the world. This will create a perimeter (which is always dead). If you look at and analyze the statement ...
Code:
totalrc = lifeGen[r - 1][c - 1] + lifeGen[r - 1][c] + lifeGen[r - 1][c + 1]
+ lifeGen[r][c - 1] + lifeGen[r][c + 1] + lifeGen[r + 1][c - 1]
+ lifeGen[r][c - 1] + lifeGen[r + 1][c] + lifeGen[r + 1][c + 1];
You'll eventually understand why.

3. Thanks, I think I just missed putting a '0' after the '2' for cols. I still get no output for this though...I don't know what the problem is.

As far as creating a copy of the original array, I see where that comes in now, except I have no clue where to even begin there. The arrays I understand a little, but function calls leave me almost totally in the dark.

As far as creating the boundary for dead cells, I get it...just don't know how to do it. As I said before, I'm way over my head in this assignment. I look at most of what I've done and I don't even understand where I got it from, lol.

I appreciate the help though.

4. as I understand your problem is that you can't realy access the arrays from the function?

You can solve this by making the arrays global by declaring them outside of the main function. Get my drift?

5. I know, but the instructor is only allowing for one global and that's the size of the array (MAX_SIZE).

I just hope he's good for partial credit because I don't understand this junk at all. I'm glad this is the only programming class I have to take.

6. try this:

Code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
void genLife();
void showLife();
int x;
int y;
int max;
char choice;
int cell[20][20];
int main(int argc, char *argv[])
{
max = 20;
for(int row=0;row < max;row++){

for(int colom=0;colom < max;colom++){
cell[row][colom] = 0;
}
}
cout << "Plot living organisms by entering coordinates in this format:\n\n15 10\n\nOnce you are done enter negative coordinates" << endl;

do {
cout << "Enter coordiantes:\n";
cin >> x >> y;
if(x > max || y > max){
cout << "Coordinates must be between 0-"<< max << "\nEnter coordinates\n";
cin >> x >> y;
}else{
cell[x][y] = 1;
}
}
while(x >= 0 && y >= 0);
showLife();
return 0;
}

void showLife(){
int colom = 0;
int row = 0;
do {
cout << "[" << cell[row][colom] << "]";
colom = colom + 1;
if (colom == max){
row = row + 1;
colom = 0;
cout << endl;
}
}
while(row < max && colom < max);

cout << "\n\nGo to next generation? (Y/N)" << endl;
cin >> choice;
if(choice == 'y' || choice == 'Y'){
genLife();
}else{
int life = 0;
for(int colom=0;colom < max;colom++){
if(cell[row][colom] == 1){
life = life +1;
}
}

cout << "Thank you for using my world simulator" << endl;
}
}

void genLife(){
int colom = 0;
int row = 0;
int c = 0;
do {
if(cell[row][colom + 1] == 1){
c = c +1;
}
if(cell[row][colom - 1] == 1){
c = c + 1;
}
if(cell[row +1][colom] == 1){
c = c + 1;
}
if(cell[row - 1][colom] == 1){
c = c + 1;
}
if(cell[row - 1][colom +1] == 1){
c = c + 1;
}
if(cell[row - 1][colom -1] == 1){
c = c + 1;
}
if(cell[row + 1][colom -1] == 1){
c = c + 1;
}
if(cell[row + 1][colom +1] == 1){
c = c + 1;
}
if (c == 3 || c == 2 && cell[row][colom] == 1){
cell[row][colom] = 1;
}else{
cell[row][colom] = 0;
}
if (c == 3 && cell[row][colom] == 0){
cell[row][colom] = 1;
}
c = 0;
colom = colom + 1;
if (colom == max){
row = row + 1;
colom = 0;
}
}
while(row < max && colom < max);
showLife();
}

7. try this:

Code:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
void genLife();
void showLife();
int x;
int y;
int max;
char choice;
int cell[20][20];
int main(int argc, char *argv[])
{
max = 20;
for(int row=0;row < max;row++){

for(int colom=0;colom < max;colom++){
cell[row][colom] = 0;
}
}
cout << "Plot living organisms by entering coordinates in this format:\n\n15 10\n\nOnce you are done enter negative coordinates" << endl;

do {
cout << "Enter coordiantes:\n";
cin >> x >> y;
if(x > max || y > max){
cout << "Coordinates must be between 0-"<< max << "\nEnter coordinates\n";
cin >> x >> y;
}else{
cell[x][y] = 1;
}
}
while(x >= 0 && y >= 0);
showLife();
return 0;
}

void showLife(){
int colom = 0;
int row = 0;
do {
cout << "[" << cell[row][colom] << "]";
colom = colom + 1;
if (colom == max){
row = row + 1;
colom = 0;
cout << endl;
}
}
while(row < max && colom < max);

cout << "\n\nGo to next generation? (Y/N)" << endl;
cin >> choice;
if(choice == 'y' || choice == 'Y'){
genLife();
}else{
int life = 0;
for(int colom=0;colom < max;colom++){
if(cell[row][colom] == 1){
life = life +1;
}
}

cout << "Thank you for using my world simulator" << endl;
}
}

void genLife(){
int colom = 0;
int row = 0;
int c = 0;
do {
if(cell[row][colom + 1] == 1){
c = c +1;
}
if(cell[row][colom - 1] == 1){
c = c + 1;
}
if(cell[row +1][colom] == 1){
c = c + 1;
}
if(cell[row - 1][colom] == 1){
c = c + 1;
}
if(cell[row - 1][colom +1] == 1){
c = c + 1;
}
if(cell[row - 1][colom -1] == 1){
c = c + 1;
}
if(cell[row + 1][colom -1] == 1){
c = c + 1;
}
if(cell[row + 1][colom +1] == 1){
c = c + 1;
}
if (c == 3 || c == 2 && cell[row][colom] == 1){
cell[row][colom] = 1;
}else{
cell[row][colom] = 0;
}
if (c == 3 && cell[row][colom] == 0){
cell[row][colom] = 1;
}
c = 0;
colom = colom + 1;
if (colom == max){
row = row + 1;
colom = 0;
}
}
while(row < max && colom < max);
showLife();
}

8. It doesn't compile for me...alot of errors, mostly dealing with 'max'.

I think I can follow what's going on though so maybe I can use some of it. I'll just have to figure out how to get rid of all the globals except for one constant and go from there.

I see what you're saying about making the arrays global, though. I'm just not allowed to make any of their values global, which sucks. All of those have to come from within main, or locally within the function itself. I guess he's trying to get us to learn how to pass values between functions without using the easy route (which is hard enough for me as it is!).

Thanks, you have no idea how much I appreciate this help.

9. Mr. Pink,

He's obviously not having any problems accessing the array since he's doing it already. The fundamental tools are already in place, since he's demonstrated he already knows how to pass an array to a function. It's all simply a series of logic problems he hasn't worked out for himself. Furthermore, I suggest you review the policies in doing Homework for other people. Providing all the code for the original poster does absolutely nothing toward helping him. Additionally, when you do attempt to provide help, look at the problem as it is outlined by his instructor. Your code did not follow the exercise at all.

jkeleslie,

As I mentioned above, you've already demonstrated the fundamental tools. I recommend you re-read the assignment and pseudocode the logic to get it straight. You've already shown you know how to pass an array to a function. All you need to do now is create a second array. Then instead of changing values in the original array, make all modifications to the backup array, then copy each and all elements to the original array. The reason is because when you modify the original array, you'll be altering elements of the array before they can be compared to adjacent elements in future checks within the loop. All changes should be made simutaneously after all logic comparisons have been made.

Oh, one more thing...
Code:
if(toupper(cont) == 'q')
will always evaluate to false.

10. > + lifeGen[r][c - 1] + lifeGen[r][c + 1] + lifeGen[r + 1][c - 1]
> + lifeGen[r][c - 1] + lifeGen[r + 1][c] + lifeGen[r + 1][c + 1];

Notice you've got lifeGen[r][c - 1] twice. It looks like you wanted the second one to be:
lifeGen[r+1][c - 1]

11. Thanks, I caught the 'Q' error later...simple typo, except it doesn't work anyway and won't let me exit out of that loop with either case. I'll re-evaluate that later as it's not overly important right now.

As far as creating another array to copy results into, I think I'm starting to see where I need to do it, I just don't know about implementing it. I'm guessing the copied array gets passed the results after checking the original array and after the copy is displayed, the original is made equal to the copy? That's the only logic I can make from it. I suppose either could be displayed depending on when I do the comparison within the code, so long as the check goes through before copying array2 back to array 1. Am I in the ballpark here?

I'm also going to increase MAX_SIZE to 22, as suggested, and just make sure the first and last row and column is always set to 0 so that I have that boundary of dead cells.

Thanks for catching the duplication there too, swoopy. Don't know how I missed that one...I even had the grid drawn out to go by.

12. OK, so I've tried reworking it according to the suggestions I've received so far and I don't know if I've gotten closer or made things worse.

As of now, the original results display just fine, which is good. However, after that nothing happens. After choosing to go to the next generation, it just repeats the original set over and over. I can sort of see why, but I've tried everything else I can think of (which isn't much).

I guess what I'm trying to get help with is how to update my array into a new array (I think I might have that part right...definately not for sure) and then get that array to print to screen. I figured out how to copy an array into another, but I'm lost after that. I've gone over this code so many times that I barely know what I'm looking at anymore.

Anyway, here it is...if you can offer any suggestions, I'd really appreciate it.

Code:
#include <iostream>
#include <cctype>
#include <cstdlib>

using namespace std;

const int MAX = 22;
void printLife(int[][MAX]);
void createLife(int[][MAX]);

int main()
{
int lifeGen[MAX][MAX];
int newGen[MAX][MAX];
int row, col;
int x = 0, y = 0;
char cont;

for(row = 0; row < MAX; row++)
for(col = 0; col < MAX; col++)
lifeGen[row][col] = 0;

cout << "Enter the coordinates to plot cell locations.  Input " << endl;
cout << "results in the format of:" << endl << endl;
cout << "10 15" << endl << endl;
cout << "When finished, enter coordinates with at least one being negative." << endl;

do
{
cout << "Enter coordinates: ";
cin >> x >> y;
if(x > MAX || y > MAX || x == 0 || y == 0)
{
cout << "Invalid input.  Reenter coordinates: ";
cin >> x >> y;
}
else if(x < 0 || y < 0)
break;
else
lifeGen[x][y] = 1;
}
while(x > 0 && y > 0);

printLife(lifeGen);

do
{
cout << endl;
cout << "Press any key and 'Enter' to display the next generation." << endl;
cout << "Press 'Q' and 'Enter' to quit: ";
cin >> cont;
if(toupper(cont) == 'Q')
{
cout << "Thank you for trying my life silmulator.  Bye!" << endl;
system("PAUSE");
return 0;
}
else
{
createLife(lifeGen);
printLife(lifeGen);

}
}
while(cin);

return EXIT_SUCCESS;
}

void printLife(int lifeGen[][MAX])
{
int x, y;

for(x = 0; x < MAX; x++)
{
for(y = 0; y < MAX; y++)
{
if(y > MAX)
cout << endl;
else if(lifeGen[x][y] == 1)
cout << "*";
else if(lifeGen[x][y] == 0)
cout << "-";
}
cout << endl;
}
}

void createLife(int lifeGen[][MAX])
{
int x, y, c;
int cell = 0;
int newGen[MAX][MAX];

for(x = 0; x < MAX; x++)
{
for(y = 0; y < MAX; y++)
newGen[x][y] = lifeGen[x][y];
}
for(x = 0; x < MAX; x++)
{
for(y = 0; y < MAX; y++)
{
if(lifeGen[x][y] == 1)
cell = lifeGen[x-1][y-1] + lifeGen[x-1][y] + lifeGen[x-1][y+1] +
lifeGen[x][y-1] + lifeGen[x][y+1] + lifeGen[x+1][y-1] +
lifeGen[x+1][y] + lifeGen[x+1][y+1];
if(cell <= 1)
newGen[x][y] = 0;
if(cell == 3)
newGen[x][y] = 1;
}
}

for(x = 0; x < MAX; x++)
{
for(x = 0; x < MAX; x++)
lifeGen[x][y] = newGen[x][y];
}
}
Sorry if I'm way off base with this...I really have no clue what I'm doing anymore.

13. You've put a perimeter around the array. However you're loops cycle the range of 0 - MAX. Since you want to leave the perimeter alone, they should cycle 1 - (MAX - 1). Thus leaving the elements 0 and 21 alone.

Also (and this is a quick peek at your code as I'm on my way out the door), although you are successfully copying lifeGen into newGen, what is happening in your program is you 1st copy lifeGen into newGen, then you analyze lifeGen and make the modifications to newGen. But here's the kicker... your printLife function prints out lifeGen, which is still the old information since the modifications have been made to newGen. Then you loop back around and copy lifeGen (still the old data) back into newGen.

What you should do is copy the information into newGen as you already do, compare the data in newGen then make the changes to lifeGen. Thus when you print lifeGen, you'll be printing the new data, and then when you copy lifeGen into newGen, the new data will copy over the old.

And one final note, your program is only modifying on cells where if(lifeGen[x][y] == 1). Which means you only check to see if that cell survives to the next generation. You need to check empty cells to see if new cells are born into life.

14. I see what you're saying about the perimeter, but shouldn't it be ok the way it is since I've limited to what the user can input for coordinates? If the user can only choose 1-20, 0 and 21 should always be empty, no? If that logic is way off, I apologize...if this course wasn't a requirement, I'd never have taken it.

I see where the lifeGen/newGen problems are coming from now. I've messed with this for so long I don't even know where I'm at in it anymore. I'll figure out the order of things after some sleep.

You mention comparing the data in newGen after copying it over...what do you mean, exactly? I just don't know enough about this level of programming to understand what help I'm given.

Anyway, let me run down what I basically think I know of what to do and see if I can get some pointers from there:

1) For the perimeter of dead cells, I'll stay with the logic I came up with above and if that's OK, I'll leave it be. If not, I know how to implement what you've suggested so I should have no problems there.

2) LifeGen is copied correctly into newGen and updated, but I need to compare (need more info there) what I got into newGen and then copy newGen back into lifeGen before printing it out.

3) For checking the dead cells for new life, I'm assuming I can use the same method as when lifeGen[x][y] == 1, except I can just do lifeGen[x][y] == 0...correct? I actually wondered about this myself and tried implementing a few solutions I thought of, but I don't know if I made any progress since everything else was jacked up.

Thanks for the help. I really do appreciate it. Anything I can get to point me in the right direction gets me closer. I didn't do all that great on my final so I really need to get this working.

15. I believe I got it finished. I had a little help from another forum aside from this one, but with the combined help, I was able to get it all done.

I've converted the user input portion as a function since the instructions mention it and I need all the points I can get and all the comments have been added for instructor purposes. I've got a solid 'A' as far as homework grades go, but I think I only got a 'C', at best, on my final...doing things on paper is just not the same, so I wanted to do as well as I could on this one.

I just wanted to post a big thanks for all the great help I got and give those who are interested a look at my final code for any pointers, suggestions, or to just try it out if you want to play a version of the classic "Game of Life".

Thanks again!

PS:

I have a question about #include <cstdlib> and the return EXIT_SUCCESS. I saw it in a few of the examples I found on the internet I used as guides and wondered if it was really necessary? Would a return 0 not suffice or are they basically the same?

Code:
//CMIS140
//20FEB05
//Homework Assignment 5
//This program simulates life.  The user inputs coordinates to plot the location
//of the first generation of cells.  Upon choosing to generate a new world, the
//cells have rules of the world applied which determines whether the cell lives,
//dies, or new cells are born.  If a live cell will live if exactly 2 or 3 of its
//neighbors are alive, else it dies.  A dead cell becomes alive if exactly 3 of
//its neighbors are alive.  The user may generate new worlds until they want to
//end it all!

#include <iostream>
#include <cctype>
#include <cstdlib>

//Constant for world size plus boundary, and functions for getting user
//coordinates, displaying the world, and for generating new worlds.
const int MAX = 22;
void getXY(int[][MAX]);
void printLife(int[][MAX]);
void createLife(int[][MAX]);

using namespace std;

int main()
{
int lifeGen[MAX][MAX];
int row, col;
int x = 0, y = 0;
char cont;

//Initialize original world array by setting all elements to 0.
for(row = 0; row < MAX; row++)
for(col = 0; col < MAX; col++)
lifeGen[row][col] = 0;

//Call to get the user's coordinates for cells to begin with.
getXY(lifeGen);

//Loop for generating and displaying new worlds.
do
{
printLife(lifeGen);
createLife(lifeGen);
cout << endl;
cout << "Press any key and 'Enter' to display the next generation." << endl;
cout << "Press 'Q' and 'Enter' to quit: ";
cin >> cont;
if(toupper(cont) == 'Q')
{
cout << "Thank you for trying my life silmulator.  Bye!" << endl;
system("PAUSE");
return 0;
}
}
while(cin);

return EXIT_SUCCESS;
}

//Funtion to get user's input for plotting live cells.
void getXY(int lifeGen[][MAX])
{
int x, y;

cout << "Enter the coordinates to plot cell locations.  Input " << endl;
cout << "results in the format of:" << endl << endl;
cout << "10 15" << endl << endl;
cout << "When finished, enter coordinates with at least one being negative." << endl;

//Loops until negative coordinate is input.  Also checks that input does not
//include boundary of dead cells.  Input coordinates set the specific element
//to 1, or 'alive'.
do
{
cout << "Enter coordinates: ";
cin >> x >> y;
if(x > MAX - 2 || y > MAX - 2 || x == 0 || y == 0)
{
cout << "Invalid input.  Reenter coordinates: ";
cin >> x >> y;
}
else if(x < 0 || y < 0)
break;
else
lifeGen[x][y] = 1;
}
while(x > 0 && y > 0);
}

//Function to display contents of current world.  Checks each element of the array
//and prints a '*' if cell is alive, or a '-' if cell is dead.
void printLife(int lifeGen[][MAX])
{
int x, y;

for(x = 0; x < MAX; x++)
{
for(y = 0; y < MAX; y++)
{
if(y > MAX)
cout << endl;
else if(lifeGen[x][y] == 1)
cout << "*";
else if(lifeGen[x][y] == 0)
cout << "-";
}
cout << endl;
}
}

//Funtion to generate new world.  A copy of the original world is created called
//newGen.  Each element of lifeGen is then checked according to the rules of the game.
//All updates, according to those rules, are made to newGen.  Finally, the updated
//world is copied back into the original for use by the print function.
void createLife(int lifeGen[][MAX])
{
int x, y;
int cell = 0;
int newGen[MAX][MAX];

//Current world is copied into another world for updates to occur.
for(x = 0; x < MAX; x++)
for(y = 0; y < MAX; y++)
newGen[x][y] = lifeGen[x][y];

//1 and MAX-1 are used in order to stay within the boundary of dead cells.
for(x = 1; x < (MAX - 1); x++)
{
for(y = 1; y < (MAX - 1); y++)
{
//Checks each cell for neighboring cells and sums them.
cell = lifeGen[x-1][y-1] + lifeGen[x-1][y] + lifeGen[x-1][y+1] +
lifeGen[x][y-1] + lifeGen[x][y+1] + lifeGen[x+1][y-1] +
lifeGen[x+1][y] + lifeGen[x+1][y+1];

//If current cell is dead, rules are applied according to neighbors.
if (lifeGen[x][y] == 0)
if (cell == 3)
newGen[x][y] = 1;
else
newGen[x][y] = 0;

//If current cell is alive, rules are applied according to neighbors.
else if (lifeGen[x][y] == 1)
if (!(cell == 3 || cell == 2))
newGen[x][y] = 0;
else
newGen[x][y] = 1;
}
}

//Copies updated world back into original world.
for(x = 0; x < MAX; x++)
for(y = 0; y < MAX; y++)
lifeGen[x][y] = newGen[x][y];
}

Popular pages Recent additions