A problem with the game of life

This is a discussion on A problem with the game of life within the C Programming forums, part of the General Programming Boards category; 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); } ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    Unhappy 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. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote 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.

    [Edit] 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.
    Last edited by MacGyver; 03-25-2007 at 03:05 PM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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 subscribe to a feed

Similar Threads

  1. platform game logic, problem
    By Akkernight in forum Game Programming
    Replies: 7
    Last Post: 02-23-2009, 09:49 AM
  2. Game Of Life 3D
    By blackslither in forum C Programming
    Replies: 8
    Last Post: 11-02-2008, 02:30 PM
  3. Game of Life
    By CornedBee in forum Contests Board
    Replies: 74
    Last Post: 05-20-2008, 01:50 AM
  4. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  5. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 12:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21