# Thread: A problem with the game of life

1. ## A problem with the game of life

Code:
```#include <stdio.h>
typedef int mat[10][10];

void input(mat matr)
{
int x,y;
scanf("%d %d",&x,&y);
while(x!=-1&&y!=-1)
{
matr[x][y]==1;
scanf("%d %d",&x,&y);
}
}
void print(mat matr)
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
printf("%d",matr[i][j]);
}

void zer(mat matr)
{
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(!matr[i][j])
matr[i][j]=0;
}

void killcell(mat matr)
{

int countneib=0;
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(matr[i][j]==1)
{
if(matr[i-1][j]==1)
countneib++;
if(matr[i+1][j]==1)
countneib++;
if(matr[i][j+1]==1)
countneib++;
if(matr[i][j-1]==1)
countneib++;
if(matr[i+1][j+1]==1)
countneib++;
if(matr[i+1][j-1]==1)
countneib++;
if(matr[i-1][j-1]==1)
countneib++;
if(matr[i-1][j+1]==1)
countneib++;
}
if(countneib>3)
matr[i][j]=0;
}

void createcell(mat matr)
{
int countneib=0;
int i,j;
for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(matr[i][j]==0)
{
if(matr[i-1][j]==1)
countneib++;
if(matr[i+1][j]==1)
countneib++;
if(matr[i][j+1]==1)
countneib++;
if(matr[i][j-1]==1)
countneib++;
if(matr[i+1][j+1]==1)
countneib++;
if(matr[i+1][j-1]==1)
countneib++;
if(matr[i-1][j-1]==1)
countneib++;
if(matr[i-1][j+1]==1)
countneib++;
}
if(countneib==3)
matr[i][j]=1;
}

int main ()
{
char choice;
int i,j;
mat matr;
input(matr);
zer(matr);
print(matr);
scanf("%c",&choice);
while(choice!='n')
{
killcell(matr);
createcell(matr);
print(matr);
scanf("%c",&choice);
}
return 0;
}```
The problem is that, there are weird numbers in the matrix.

Thanks

2. after a very quick look, i notice in your while loop of your input function:
matr[x][y]==1;
i assume you mean to use '=' instead of comparison operator '=='. im sure this will be a part of the problem, if it doesnt solve it ill take a deeper look.

3. Originally Posted by nadroj
after a very quick look, i notice in your while loop of your input function:
matr[x][y]==1;
i assume you mean to use '=' instead of comparison operator '=='. im sure this will be a part of the problem, if it doesnt solve it ill take a deeper look.
I also noticed that, changed it, and it crashes.

 Nevermind about the crash. I think it crashed because, being a genius, I was giving it incorrect input. Incidentally, the following code inside the zer() function seems incorrect.

Code:
```if(!matr[i][j])
matr[i][j]=0;```
Basically can be translated into:

Code:
```if(matr[i][j] == 0)
{
matr[i][j] = 0;
}```
Redundant.

[/edit]

BTW, is it me or does this seem like a wrong way to use typedef? I don't know. Maybe it's just me.

4. are you typing in input 100 times in the input function? if not then when you print it it will print the int at that index (in the for loop), which hasnt been initialized. which is where the large random numbers come from.

5. Code:
```for(i=0;i<10;i++)
for(j=0;j<10;j++)
if(matr[i][j]==1)
{
if(matr[i-1][j]==1)```
Wouldn't this go out of bounds? One solution would be to add unused "edges" to the array.

I'm not so sure if you can kill and create cells as you check them. I think you should first scan the array to establish which cells need to be killed and created first, without changing the array immediately and only then can you change the states of all cells at one go. In other words, you need to separate decision-making and actually modifying the array, because the decisions depend on the state of the game as a whole.

Popular pages Recent additions