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

Dont worry, I cant fool my lecturer and he knows my way of working too well for me to even think of copying! LOL! :) otherwise, i would never have come here for HELP!!!! AND THEN, everywhere ive looked, they have done it in a language i dont understand...

Im meant to do it on my own, and its been decided even if we cant do it, we will give out the little we know is correct! and then, im learning cos its interesting, thanks to you all!!!

But i appreciate it, thanks again Booo!

So, ive been working with arrays, with loops, with if statements, and using my knowlegde in mathematics im trying to use switch!

A B C

D E F

G H I

thats my input in my array of 15 by 15!! i enter the state for each cell (either 1 or 0), so if

neighbours of A = B + D + E

neighbours of A = 0, A becomes 0

neighbours of A = 1, A becones 0

neighbours of A = 2, A stays A

neighbours of A = 3, A becomes 1

¦

¦

continues for A = 4 till A =8!!!

i do this for B, C, D, E, F, G ,H...

somebody tell me this is good!!! plz!

and how do I do it for the next generation???
 
Exactly.

First determine how many neighbours the element has
A=B+D+E that is three neighbours

but
B=A+D+E+F+C that is 5 neighbours similarly E has 8

I will give you a hint. If the element is located at the boundary i.e. either row=1 or column=1 or row=3 or column=3 then it will have either 3 or 5 neighbours

if it is a corner element i.e row and column are both 1 or row=1 and column=3 or row=3 column=1 or row and column are both 3
then it will have 3 neighbours

if the element is a boundary element but not a corner element it will have 5 neighbours

everything else will have 8 neighbours. I think you should first print the neighbours of each element so that you get the hold of it.
 
OMG! THANK YOU!!!
For at least teeling me this is good!
yeah, i figured that out...was scrubbling on a piece of paper...now im working on how to program the next generation..im using this loop
for (g=0;g>0;g++)
you are really guiding me through this! :)
 
okay, do one thing. modularize your code. that will help you solve the problem easily. divide and conquer.

1. write functions like bool IsCornerCell(cell), int CalculateNighbours(cell), void TakeInput() etc...
2. After writing these functions writing main function would become easy and you wont get confused.
 
hammerhead said:
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++)

Oops, sorry. Apologies. Forgot the '=' sign.
 
Zayni said:
OMG! THANK YOU!!!
For at least teeling me this is good!
yeah, i figured that out...was scrubbling on a piece of paper...now im working on how to program the next generation..im using this loop

you are really guiding me through this! :)

the loop
PHP:
for (g=0; g>0 ;g++ )

will run infinitely ....
the reason : you are telling the computer to start from 0 and keep on increasing by 1 until its greater than 0 , in this case which is always true.

correct form

PHP:
for (g=0; g <= n-1;g++ )

Btw if you are using > operator for comparing the condition ( ex. g> 0 , then it should be accompanied with g-- ) , if you are using < operator for comparing the condition ( ex. g<15, then it should be ++ )

Note : this above explanation is for very basic for loops, this rules don't remain same when you mix additional conditions.
 
Thanks for reminding me...im still a noob in that and that was to show how bad i am at it!! tomorrow is the day...lol!
so im going to work on it again...using arrays, some loops, the neighbours, count, ifs...thanks for the help!!!! :hap2:
 
hmmm ... seems like i over looked a loop

the loop g> 0 wont run coz it doesnt meet the primary condition ( since g=0)

however if it was g>=0 then it would have been in infinite loop :)
 
Urm, i was thinking...im working on it right now...can i post it and you tell me if there are things which are wrong in it???
 
#include <stdio.h>
int main (void)
{
int Before[16][16];
int After[16][16];
int i,j,x,y;
char n;
/* initialsing gameboard */

for(i=0;i<16;i++){
for(j=0;j<16;j++){
Before[j]=0;
After[j]=0;
}
}
do
{
printf("Please enter coordinates for alive cells:");
scanf("%d,%d", &x,&y);
Before[x+1][y+1]=1; /*value 1 for alive cells*/
printf("Do you want to continue adding live cells? y/n");
scanf("%c",&n);
}
while (n=='y'||n=='Y');


i think there's a mistake somewhere...
 
That should be before[x-1][y-1]=1;

Consider the case when user enters 1,1 as the upper left corner of the board. In that case the before[0][0] should be initialized to 1.
 
#include <stdio.h>
int main (void)
{
int Before[16][16];
int After[16][16];
int i,j,x,y;
int count;
char n;
/* initialising gameboard */

for(i=0;i<16;i++){
for(j=0;j<16;j++){
Before[j]=0;
After[j]=0;
}
}
do
{
printf("Please enter coordinates for alive cells:");
scanf("%d,%d", &x,&y);
Before[x-1][y-1]=1; /*value 1 for alive cells*/
printf("Do you want to continue adding live cells? y/n");
scanf("%c",&n);
}
while (n=='y'||n=='Y');
for(i=1;i<16;i++){
count=0;
for(j=1;j<16;j++){
if
(Before[i-1][j-1]=1)
{
count++;
}
if
(Before[i-1][j]=1)
{
count++;
}
if
(Before[i-1][j+1]=1)
{
count++;
}
if
(Before[j-1]=1)
{
count++;
}
if
(Before[j+1]=1)
{
count++;
}
if
(Before[i+1][j-1]=1)
{
count++;
}
if
(Before[i+1][j]=1)
{
count++;
}
if
(Before[i+1][j+1]=1)
{
count++;
}
}
}
printf("Number of neighbours=%d",count);
/*gameboard for next generation*/
if
(count==3)
After[j]=1;
if
(count==2)
After[j]=Before[j];
if
(count==0)
After[j]=0;
return 0;
}


Now its fine until it ask me do you want to continue...then it says press any key to continue!! :S
 
Zayni said:
Now its fine until it ask me do you want to continue...then it says press any key to continue!! :S

In your code
Code:
if
(Before[i-1][j]=1)
{
count++;
}

you have used the assignment operator = in if statement. Change it to == as

Code:
if
(Before[i-1][j]==1)
{
count++;
}
 
for(i=0;i<16;i++){
for(j=0;j<16;j++){
Before[j]=0;
After[j]=0;


This initialisation is leading to nowhere...According to me, it should be entering 0 for each cells, put nothing appears on the screen!
Only
Please enter coordinates for alive cells:
Do you want to continue adding live cells? (y/n)Number of neighbours=

are appearing on the screen! :huh:
 
you need to use printf() for displaying the result of that initialization

Code:
for(i=0;i<16;i++){
for(j=0;j<16;j++){
Before[i][j]=0;
After[i][j]=0;
printf ("%d",Before[i][j]);
}
printf("\n");
}
 
Back
Top