Thread: The Game of Life problem

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    81

    The Game of Life problem

    Hey, I have an assignment called the Game of Life. If you haven't heard of it, feel free to look it up. Anyway, the problem I am having is the algorithm I have to compute if the living cell has any neighbors is not producing anything. I know that the code is a little redundant but if you have any suggestions, please help.

    This code basically goes through the array and adds up all the neighbors and if they are less than 2 or more than 3, then the current node is dead(i.e. 0)
    Here is the code:

    Code:
    void liveon(int g1[][22],int g2[][22])
    {
    for(int r=0;r<22;r++)
        for(int c=0;c<22;c++)
        while(g1[r][c]=1)
        {
         if(g1[r-1][c-1]+g1[r-1][c]+g1[r-1][c+1]//top of block
           + g1[r][c-1]+g1[r][c+1]               //middle of block
           + g1[r][c]+g1[r][c]+g1[r][c]<2        //bottom of block
            ||
            g1[r-1][c-1]+g1[r-1][c]+g1[r-1][c+1] //top of block
            + g1[r][c-1]+g1[r][c+1]                //middle of block
            + g1[r+1][c-1]+g1[r+1][c]+g1[r+1][c+1]>3) // bottom
         g1[r][c]=0;
         }
    //copy to second array
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well how about a nice and simple
    Code:
    for(int r=0;r<22;r++) {
        for(int c=0;c<22;c++) {
            neighbours = count_neighbours( g1, r, c );
            if ( neighbours == 2 || neighbours == 3 ) {
               // we're alive!
               g2[r][c] = 1;
            } else {
               // alas, poor Yorik
               g2[r][c] = 0;
            }
        }
    }
    > g1[r-1][c-1]
    At r=0, c=0, this is way off the ends of the array
    You need to allow for this in your count_neighbours() function

    > while(g1[r][c]=1)
    You probably meant while(g1[r][c]==1)
    But I'm not sure that fixes anything.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Also, I'm not sure what you are trying to accomplish with the while function. You should either kill the cell or keep it alive, but either way you don't need the while loop. With the algorithm that you have, if the cell is to be kept alive then it will just run into an infinite loop.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    81
    Yeah, I noticed, I changed it to an if statement and that seemed to work.. thanks for the help

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    hmm.. had done this beofre.. this might help you

    http://cboard.cprogramming.com/showt...highlight=life

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    so have I... http://www.johnshaoonline.com/frame.htm (it's called 'Life', Ironically ) I don't think I have my code there, but those are all open-source, so just post here or PM me if you need/want my code...

    P.S. - In my code, there's a problem writing back to the array so it kinda wraps around one or two columns... I just haven't gotten around to finding the problem...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

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, 10:49 AM
  2. Game Of Life 3D
    By blackslither in forum C Programming
    Replies: 8
    Last Post: 11-02-2008, 03: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, 01:32 PM