Please Help me with this Assignment - Game Of Life in C!

Zayni

Disciple
hello all,

Im in desperate need to do an assignment on Game of life in C language!

And despite doing programming for 1 week, i still dont know how to implement this or write program in a proper manner! I have been reading books, looking for info on it o the internet, but i cant seem to start it..as I said, Im really not an ace in computer programming and I have to return this assignment by friday and if anyone could help me with this, ill be really grateful!

The program consists of:

Assignment

The Problem: The Game of Life

The game of life is a simple cellular automaton where the world is a 2D grid of cells which have two states: alive or dead. After each iteration, the new state of a cell is determined by the state of its neighbors at the previous iteration. This includes both the nearest neighbors and the

diagonal neighbors. i.e. like a 9 point stencil without using the value of the cell itself as input. The rules for the evolution of the system are:

• If a cell has exactly two alive neighbors, it maintains state.

• If a cell has exactly three alive neighbors, it is alive.

• Otherwise, the cell is dead.

Your code will need to:

1. Initialize the gameboard

2. Start loop:

a) Print the gameboard (temporarily for correctness checking)

b) Calculate number of live neighbors

c) If (live neighbors = 3) then live

d) If (live neighbors < 2) or (live neighbors > 3) then die

3. End loop

4. Print final gameboard (again for correctness checking):huh: :huh:

Its supposed to be an array of [15][15]..with functions...:huh: oh well, i dont know..plz someone help!!! ive been designing it on a piece of paper, but cant seem to know where to start.. I want to do it on my own, but I really need an enlightment, i think!!! thanks in advance!

Please, help me with this...Im a chemical engineering student, never knew i had to do programming! :(
 
I doubt someone will come and make the program for you. I can point you in the right direction.
Start with creation of the board in main as
Code:
int board[15][15];

after that randomly assign the values of the board with either 0 meaning dead or 1 meaning alive. Yyou will need to use the srand() function to seed the random number generation after that call the rand() function as
Code:
for(int i=0;i<15;i++)
{
for(int j=0;j<15;j++)
{
board[i][j]=rand()%2;
}
}

That takes care of the initialization. After that you need to iterate the entire 2d array and check the neighbours of each element and reassign it to 0 or 1.

Except for the boundary elements, all other elements will have 8 neighbours. All the elements at the corner will have 3 neighbours and the remaining will have 5 neighbours.

Traverse each node using the loop above and check to which class the element belongs to at each iteration (either having 3 neighbours, 5 neighbours or 8 neighbours)

Code:
if(i>0&&j>0&&i<14&&j<14)
{
//eight neighbours that are (i+1,j+1),(i-1,j-1),(i+1,j),(i,j+1),(i-1,j),(i,j-1),(i+1,j-1),(i-1,j+1)

//check state of all the neighbours and reassign the present element i,j

}

Make the code for this much first.
 
Thanks...this is of great help, you cant imagine!

we have been using switch case thinking it was only an array of 3 by 3! And I dont think Ive thought about the 5 neighbours...will check it today! Thanks A LOT!
for(g=2;g>0;g++){

{ /*calculating for cell A*/
sA = B + D + E ;
switch(sA)
{
case 0:
A=0;
break;
case 1:
A=0;
break;
case 2:
A=A;
break;
case 3:
A=1;
break;
case 4:
A=0;
break;
case 5:
A=0;
break;
case 6:
A=0;
break;
case 7:
A=0;
break;
case 8:
A=0;
break;
}
}


{ /*calculating for cell B*/
sB = A + C + D + E + F ;
switch(sB)
{
case 0:
B=0;
break;
case 1:
B=0;
break;
case 2:
B=B;
break;
case 3:
B=1;
break;
case 4:
B=0;
break;
case 5:
B=0;
break;
case 6:
B=0;
break;
case 7:
B=0;
break;
case 8:
B=0;
break;
}
}

But yesterday, our lecturer asked us to do it on an 15 by 15...and we got to use functions..that is where im lost!
 
Zayni said:
Thanks...this is of great help, you cant imagine!

we have been using switch case thinking it was only an array of 3 by 3! And I dont think Ive thought about the 5 neighbours...will check it today! Thanks A LOT!


But yesterday, our lecturer asked us to do it on an 15 by 15...and we got to use functions..that is where im lost!

Create a function called 'neighbours' which takes as input the cell you are at and returns as output the number of live neighbours.
So you will run

while (i<15,j<15)
call neighbours(i,j)
if neighbours(i,j) is = 3 then cell(i,j) is live else dead
end while
 
Functions are not that difficult. Take a good book or google for some tutorial. The basic outline is
Code:
void neighbours(int board[][])

you may also want to consider a code for initialization
 
read a book called datastructures in c for which one of the authors is clovis L. Tondo
that book has nice explenation of the problem.

best of luck with the assignment.
 
Thanks all for your replies!
Im trying all these...
Booo, im really grateful..im downloading the book, thanks! i hope it can help!
:ashamed:
 
#include<stdio.h>
int main()
{
int A,B,C,D,E,F,G,H,I,sA,sB,sC,sD,sE,sF,sG,sH,sI;
int g;
char n;
/*gameboard*/
printf("\n\t\t ***WELCOME TO THE GAME OF LIFE***");
printf("\n\n\n\t\t\tGeneration:0");
printf("\n\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|\tBrought to you");
printf("\n\t\t\t| A\t| B\t| C\t|");
printf("\n\t\t\t|\t|\t|\t|\tby: *0714729*");
printf("\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|\t *0712629*");
printf("\n\t\t\t| D\t| E\t| F\t|");
printf("\n\t\t\t|\t|\t|\t|\t *0716408*");
printf("\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|");
printf("\n\t\t\t| G\t| H\t| I\t|\tDead:0");
printf("\n\t\t\t|\t|\t|\t|\tAlive:1");
printf("\n\t\t\t-------------------------\n");
/*entering status of cells*/
printf("\n^^^Enter 0 for a dead cell and 1 for an alive cell.^^^\n\n");
printf("Please enter state (0 or 1) of cell A:");
scanf("%d",&A);
printf("Please enter state (0 or 1) of cell B:");
scanf("%d",&B);
printf("Please enter state (0 or 1) of cell C:");
scanf("%d",&C);
printf("Please enter state (0 or 1) of cell D:");
scanf("%d",&D);
printf("Please enter state (0 or 1) of cell E:");
scanf("%d",&E);
printf("Please enter state (0 or 1) of cell F:");
scanf("%d",&F);
printf("Please enter state (0 or 1) of cell G:");
scanf("%d",&G);
printf("Please enter state (0 or 1) of cell H:");
scanf("%d",&H);
printf("Please enter state (0 or 1) of cell I:");
scanf("%d",&I);
printf("Press ENTER to start the game.");
scanf("%c", &n);
/*printing gameboard with states for generation 1*/
printf("\n\n\n\n\t\t ***WELCOME TO THE GAME OF LIFE***");
printf("\n\n\n\t\t\tGeneration:1");
printf("\n\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|\tBrought to you");
printf("\n\t\t\t| %d\t| %d\t| %d\t|", A,B,C);
printf("\n\t\t\t|\t|\t|\t|\tby: *0714729*");
printf("\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|\t *0712629*");
printf("\n\t\t\t| %d\t| %d\t| %d\t|", D,E,F);
printf("\n\t\t\t|\t|\t|\t|\t *0716408*");
printf("\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|");
printf("\n\t\t\t| %d\t| %d\t| %d\t|\tDead:0", G,H,I);
printf("\n\t\t\t|\t|\t|\t|\tAlive:1");
printf("\n\t\t\t-------------------------\n");
printf("\n\nPress ENTER for the next generation.");
scanf("%c", &n);
/*starting iteration for generation 2 onwards*/
for(g=2;g>0;g++){

{ /*calculating for cell A*/
sA = B + D + E ;
switch(sA)
{
case 0:
A=0;
break;
case 1:
A=0;
break;
case 2:
A=A;
break;
case 3:
A=1;
break;
case 4:
A=0;
break;
case 5:
A=0;
break;
case 6:
A=0;
break;
case 7:
A=0;
break;
case 8:
A=0;
break;
}
}


{ /*calculating for cell B*/
sB = A + C + D + E + F ;
switch(sB)
{
case 0:
B=0;
break;
case 1:
B=0;
break;
case 2:
B=B;
break;
case 3:
B=1;
break;
case 4:
B=0;
break;
case 5:
B=0;
break;
case 6:
B=0;
break;
case 7:
B=0;
break;
case 8:
B=0;
break;
}
}
{ /*calculating for cell C*/
sC = B + E + F ;
switch(sC)
{
case 0:
C=0;
break;
case 1:
C=0;
break;
case 2:
C=C;
break;
case 3:
C=1;
break;
case 4:
C=0;
break;
case 5:
C=0;
break;
case 6:
C=0;
break;
case 7:
C=0;
break;
case 8:
C=0;
break;
}
}


{ /*calculating for cell D*/
sD = A + B + E + G + H ;
switch(sD)
{
case 0:
D=0;
break;
case 1:
D=0;
break;
case 2:
D=D;
break;
case 3:
D=1;
break;
case 4:
D=0;
break;
case 5:
D=0;
break;
case 6:
D=0;
break;
case 7:
D=0;
break;
case 8:
D=0;
break;
}
}


{ /*calculating for cell E*/
sE = A + B + C + D + F + G + H + I ;
switch(sE)
{
case 0:
E=0;
break;
case 1:
E=0;
break;
case 2:
E=E;
break;
case 3:
E=1;
break;
case 4:
E=0;
break;
case 5:
E=0;
break;
case 6:
E=0;
break;
case 7:
E=0;
break;
case 8:
E=0;
break;
}
}
{ /*calculating for cell F*/
sF = B + C + E + H + I ;
switch(sF)
{
case 0:
F=0;
break;
case 1:
F=0;
break;
case 2:
F=F;
break;
case 3:
F=1;
break;
case 4:
F=0;
break;
case 5:
F=0;
break;
case 6:
F=0;
break;
case 7:
F=0;
break;
case 8:
F=0;
break;
}
}


{ /*calculating for cell G*/
sG = D + E + H ;
switch(sG)
{
case 0:
G=0;
break;
case 1:
G=0;
break;
case 2:
G=G;
break;
case 3:
G=1;
break;
case 4:
G=0;
break;
case 5:
G=0;
break;
case 6:
G=0;
break;
case 7:
G=0;
break;
case 8:
G=0;
break;
}
}
{ /*calculating for cell H*/
sH = G + I + D + E + F ;
switch(sH)
{
case 0:
H=0;
break;
case 1:
H=0;
break;
case 2:
H=H;
break;
case 3:
H=1;
break;
case 4:
H=0;
break;
case 5:
H=0;
break;
case 6:
H=0;
break;
case 7:
H=0;
break;
case 8:
H=0;
break;
}
}
{ /*calculating for cell I*/
sI = H + E + F ;
switch(sI)
{
case 0:
I=0;
break;
case 1:
I=0;
break;
case 2:
I=I;
break;
case 3:
I=1;
break;
case 4:
I=0;
break;
case 5:
I=0;
break;
case 6:
I=0;
break;
case 7:
I=0;
break;
case 8:
I=0;
break;
}
}

/*printing gameboard*/
printf("\n\n\n\n\t\t ***WELCOME TO THE GAME OF LIFE***");
printf("\n\n\n\t\t\tGeneration:%d",g);
printf("\n\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|\tBrought to you");
printf("\n\t\t\t| %d\t| %d\t| %d\t|", A,B,C);
printf("\n\t\t\t|\t|\t|\t|\tby: *0700000*");
printf("\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|\t *0711111*");
printf("\n\t\t\t| %d\t| %d\t| %d\t|", D,E,F);
printf("\n\t\t\t|\t|\t|\t|\t *0722222*");
printf("\n\t\t\t-------------------------");
printf("\n\t\t\t|\t|\t|\t|");
printf("\n\t\t\t| %d\t| %d\t| %d\t|\tDead:0", G,H,I);
printf("\n\t\t\t|\t|\t|\t|\tAlive:1");
printf("\n\t\t\t-------------------------\n");
printf("\n\nPress ENTER for the next generation.");
scanf("%c", &n);
}
return 0;
}
/*bye bye

Game Over*/

This is what ive done so far with friends...but the thing is im supposed to use an array of [15][15] and im confused about it! How do i put in my program now???
i look for the book, thanks, Booo!
 
Read my first post. What I am guessing from your code is that you are trying for 9 elements i.e A....I placed in this order

A B C
D E F
G H I

you will need to create a 2d array like
Code:
int board[3][3];

for 9 elements. This means that the board has three rows and three columns. The first row and column start from 0 and end at 2. Hence

board[0][0] corresponds to A
board[0][1] corresponds to B
board[1][1] corresponds to E
and so on

you will access each element in a loop as

Code:
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
//elements referred as board[i][j]
}
}
 
I have done this loop..but thats for an array of [3][3]..the board dimension of the gameboard must be 15 x 15 or 10 x 10!!!! the thing is i dont know how to put a 2d array of 9 elements into a 15 y 15 board! :S :S :S
 
Zayni said:
I have done this loop..but thats for an array of [3][3]..the board dimension of the gameboard must be 15 x 15 or 10 x 10!!!! the thing is i dont know how to put a 2d array of 9 elements into a 15 y 15 board! :S :S :S

What is so confusing?? You won't have nine elements for a larger board. You will have more.

So, like Hammer head mentioned, do

Code:
int board[n][n];

Your iteration will be

Code:
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
//elements referred as board[i][j]
}
}

where n is 3,10,15 or any number you want. You will always have n*n elements. For a board of 3 you had 3*3 = 9 elements!
 
whatsinaname said:
What is so confusing?? You won't have nine elements for a larger board. You will have more.

So, like Hammer head mentioned, do

Code:
int board[n][n];

Your iteration will be

Code:
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-1;j++)
{
//elements referred as board[i][j]
}
}

where n is 3,10,15 or any number you want. You will always have n*n elements. For a board of 3 you had 3*3 = 9 elements!
Isnt it supposed to be
Code:
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
//elements referred as board[i][j]
}
}
 
Zayni said:
Isnt it supposed to be

Code:
for(int i=0;i<n;i++)

{

for(int j=0;j<n;j++)

{

//elements referred as board[i][j]

}

}

0 to n-1 has n repetetions of the loop.

0 to n has n+1 repetetions of the loop.

So if your n = 15, either you run the loop from i=1 to i=15 or run the loop from i=0 to i=14.

In C++, the first element of an array is x[0]. So you iterate from i=0 to i=14 to get fifteen elements.
 
whatsinaname said:
0 to n-1 has n repetetions of the loop.
0 to n has n+1 repetetions of the loop.

So if your n = 15, either you run the loop from i=1 to i=15 or run the loop from i=0 to i=14.

In C++, the first element of an array is x[0]. So you iterate from i=0 to i=14 to get fifteen elements.

yeah...im using C language!!! and im starting to have a headache now!
 
#include<stdio.h>
int main ()
{
int array[3][3];
int g;
int i;
char n;
int liveneighbors[3][3];

printf("Welcome to the Game of Life\n");
printf("Generation 0\n");
printf("Please enter 1 for an Alive cell and 0 for a Dead cell\n");
printf("State for cell A\n");
scanf("%d", &array[0][0]);
printf("State for cell B\n");
scanf("%d", &array[0][1]);
printf("State for cell C\n");
scanf("%d", &array[0][2]);
printf("State for cell D\n");
scanf("%d", &array[1][0]);
printf("State for cell E\n");
scanf("%d", &array[1][1]);
printf("State for cell F\n");
scanf("%d", &array[1][2]);
printf("State for cell G\n");
scanf("%d", &array[2][0]);
printf("State for cell H\n");
scanf("%d", &array[2][1]);
printf("State for cell I\n");
scanf("%d", &array[2][2]);

printf("Game of Life\n");
printf("Generation 0\n");
printf("%d %d %d\n", array[0][0], array[0][1], array[0][2]);
printf("%d %d %d\n", array[1][0], array[1][1], array[1][2]);
printf("%d %d %d\n", array[2][0], array[2][1], array[2][2]);
printf("Press Enter to play.\n");
scanf("%c", &n);

printf("Please enter a generation:\n");
scanf("%d", &g);
//calculating number of live cells

//Number of neigbors of cell A

printf("Generation =%d\n", g);

for(i=0;i<g+1; i++)
{

liveneighbors[0][0] = (array[0][1] + array[1][0] + array[1][1]);
liveneighbors[0][1] = (array[0][0] + array[0][2] + array[1][0] + array[1][1] + array[1][2]);
liveneighbors[0][2] = (array[0][1] + array[1][1] + array[1][2]);
liveneighbors[1][0] = (array[0][0] + array[0][1] + array[1][1] + array[2][0] + array[2][1]);
liveneighbors[1][1] = (array[0][0] + array[0][1] + array[0][2] + array[1][0] + array[1][2] + array[2][0] + array [2][1] + array [2][2]);
liveneighbors[1][2] = (array[0][1] + array[1][1] + array[2][1]);
liveneighbors[2][0] = (array[1][0] + array[1][1] + array[2][1]);
liveneighbors[2][1] = (array[1][0] + array[1][1] + array[1][2] + array[2][0] + array[2][2]);
liveneighbors[2][2] = (array[1][1] + array[1][2] + array[2][1]);
if(liveneighbors[0][0] =3)
{

printf("1\t", array[0][0]);
}
else((liveneighbors[0][0]<2)||(liveneighbors[0][0]>3));
{
printf("0\t", liveneighbors[0][0]);
}
if(liveneighbors[0][1] = 3)
{

printf("1\t", liveneighbors[0][1]);
}
else((liveneighbors[0][1]<2)||(liveneighbors[0][1]>3));
{
printf("0\t", liveneighbors[0][1]);
}

if(liveneighbors[0][2] =3)
{

printf("1\n", liveneighbors[0][2]);
}
else((liveneighbors[0][2]<2)||(liveneighbors[0][2]>3));
{
printf("0\n",liveneighbors[0][2]);
}

if(liveneighbors[1][0] =3)
{

printf("1\t", liveneighbors[1][0]);
}
else((liveneighbors[1][0]<2)||(liveneighbors[1][0]>3));
{
printf("0\t", liveneighbors[1][0]);
}

if(liveneighbors[1][1] =3)
{

printf("1\t", liveneighbors[1][1]);
}
else((liveneighbors[1][1]<2)||(liveneighbors[1][1]>3));
{
printf("0\t", liveneighbors[1][1]);
}

if(liveneighbors[1][2] =3)
{

printf("1\n", liveneighbors[1][2]);
}
else((liveneighbors[1][2]<2)||(liveneighbors[1][2]>3));
{
printf("0\n", liveneighbors[1][2]);
}

if(liveneighbors[2][0] =3)
{

printf("1\t", liveneighbors[2][0]);
}
else((liveneighbors[2][0]<2)||(liveneighbors[2][0]>3));
{
printf("0\t", liveneighbors[2][0]);
}

if(liveneighbors[2][1] =3)
{

printf("1\t", liveneighbors[2][1]);
}
else((liveneighbors[2][1]<2)||(liveneighbors[2][1]>3));
{
printf("0\t", liveneighbors[2][1]);
}

if(liveneighbors[2][2] =3)
{

printf("1\n", liveneighbors[2][2]);
}
else((liveneighbors[2][2]<2)||(liveneighbors[2][2]>3));
{
printf("0\n", liveneighbors[2][2]);
}
}

return 0;
}
using if statement...but i think there's a mistake somewhere, the program says no error, but when you go for the next generation, it gets strange! could anyone check it, please?? thanks!
 
whatsinaname said:
0 to n-1 has n repetetions of the loop.
0 to n has n+1 repetetions of the loop.

So if your n = 15, either you run the loop from i=1 to i=15 or run the loop from i=0 to i=14.

In C++, the first element of an array is x[0]. So you iterate from i=0 to i=14 to get fifteen elements.

Actually Zayni is right. The loop that you have written is

for(i=0;i<n-1;i++)

the < operator ensures that the loop runs till n-1 in your case it will run till n-2

the alternative will be

for(i=0;i<=n-1;i++)
 
^ You shouldn' t. Giving the code away just means that the op will not learn anything and will most likely copy paste the code for the assignment.
 
Back
Top