Thread: Game of Life Problems

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    8

    Game of Life Problems

    I am having problems coding the game of life for an assignment. I can generate my initial matrix and then subsequently add a border of 0's around it to aid in the summation of live/dead cells.

    the criteria is:
    dead -- 3 live neighbours -- alive
    dead -- not 3 live neighbours -- dead
    alive-- 2/3 live neighbours -- alive
    alive-- not 2/3 live neighbours--dead

    i have code outside of main() to calculate the number of dead/live cells

    Code:
    #define A 1000
    #define B 1000
    
    /*calculates the state of the cell after each generation*/
    int state(int v[A][B],int i,int j)
    
    {   
        int k,q,amount=0;
    /*Counts how many live cells in the neighbourhood*/
        for(k=i-1;k<=i+1;k++){
            for(q=j-1;q<=j+1;q++){
                amount=amount+v[k][q];
    }
    
    }
    
    /*if alive, then die if not 2,3. if dead then remain dead if not 3*/
    /*returns a 0 if cell is now dead*/
        if((v[i][j]=1 && (amount<2 || amount>3)) || (v[i][j]=0 && (amount<3 || amount>4))) return 0;
    /*if alive, then remain alive if 2 or 3. if dead then become alive if 3*/
    /*returns a 1 if cell is now alive*/
        else if((v[i][j]=1 && (amount==2 || amount==3)) || (v[i][j]=0 && amount==3) ) return 1;
       
    
    }
    and in main() I have a do loop, which incorporates my initial matrix with the border of 0's

    Code:
    do 
        {
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=p;j++)
                { 
                    d=state(v,i,j);
                    if(d==0)
                        v[i][j]=0;
                    else if(d==1)
                        v[i][j]=1;
                }
            }
    /*Counts the number of generations down each time one has been programmed*/
            generations--;
    /*Stops the program when the new generations number gets to 0*/
        }while(generations!=0);
    for some (probably obvious reason) I don't seem to be getting the correct output in my final matrix.

    any help would be much appreciated

    thanks Emma

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What about out-of bounds access?

    do you check somewhere that your indexes not go out of range [0,A-1] when you do i-1, i+1 access?

    you have

    if(condition1) return 0;
    else if(condition2) return 1;

    what will be returned if both conditions are false?

    if it is not possible why do you need the second if?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your neighbor count counts the cell itself, so is one too high if the cell is alive.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    it shouldn't go out of bounds, i have removed the second else if and changed it to

    Code:
    else return 1;
    this still doesn't rectify my problem though. i am new to c programming and finding it hard to understand what is going on to be honest.

    thanks for your comments
    Last edited by emj83; 02-25-2009 at 12:49 PM.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Quote Originally Posted by tabstop View Post
    Your neighbor count counts the cell itself, so is one too high if the cell is alive.
    Code:
        if((v[i][j]=1 && (amount<3 || amount>4)) || (v[i][j]=0 && (amount<3 || amount>4))) return 0;
    /*if alive, then remain alive if 2 or 3. if dead then become alive if 3*/
    /*returns a 1 if cell is now alive*/
        else  return 1;
    so if i change it to this?

    it is still not giving me the final board that I expect

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    BTW - you should have 2 matrixes - current and next step

    in your case you start replacing nodes in the current matrix which disturbes calculation of neighbours for the nodes in the next row
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    8
    Quote Originally Posted by vart View Post
    BTW - you should have 2 matrixes - current and next step

    in your case you start replacing nodes in the current matrix which disturbes calculation of neighbours for the nodes in the next row
    ok this probably is where I am going wrong. do you think you could give me a hint on how to get going on this please? would be very much appreciated

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I would build array of two matrix

    and use index = 0,1 and switch it in each iteration
    index = 1-index

    and when I will count nighbours for a[index]
    and update the new values in the matrix a[1-index]
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game of Life
    By puk284 in forum C# Programming
    Replies: 2
    Last Post: 11-17-2008, 03:53 AM
  2. Game of Life
    By CornedBee in forum Contests Board
    Replies: 74
    Last Post: 05-20-2008, 01:50 AM
  3. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM
  4. Game of Life pros - help me
    By bnmwad in forum C Programming
    Replies: 1
    Last Post: 04-06-2005, 02:35 AM
  5. Memory Problems w/My DX game
    By Stan100 in forum Game Programming
    Replies: 2
    Last Post: 10-03-2003, 01:52 PM