Game of life

This is a discussion on Game of life within the C++ Programming forums, part of the General Programming Boards category; I have written a erronous game of life. the game of life is as follows: neighboring cells are defined as: ...

  1. #1
    *this
    Join Date
    Mar 2005
    Posts
    498

    Game of life

    I have written a erronous game of life.
    the game of life is as follows:

    neighboring cells are defined as:
    * = cell
    # = possible neighbor

    ###
    #*#
    ###

    every empty cell with three living neighbors will come to life next generation

    any cell with two or three neighbors will live into the next generation, cells with less or more will die

    births and deaths occur simultaneouslya

    program must be run for 5 generations
    _________________________________

    i tested my program and some bacteria seem to work and others appear to be random, please help me get an idea of what im doing wrong.

    My program of Life:

    life.cpp
    thanks,
    Josh
    Last edited by JoshR; 04-01-2005 at 04:08 AM.

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    82
    Interesting. What does the input file look like?

  3. #3
    *this
    Join Date
    Mar 2005
    Posts
    498
    first integer is total number of bacteria, followed by coordinates.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    yep quite interesting.
    Instead of this
    Code:
    void fill (char table[ MAX ][ MAX ])
    //fill the table with blank spaces
    {
       for (int row = 0; row < MAX; row++)
          for (int col = 0; col < MAX; col++)
             table[ row ][ col ] = ' ';
    }
    You could just do this
    Code:
    void fill(char *table)
    {
    
       memset(table, ' ', sizeof(table));
    }
    Be carefull in your alive check routine, it looks like you check all ajacent cells even if you're at the edge of the grid.
    Last edited by Quantum1024; 04-01-2005 at 04:16 AM.

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    i see where your going, but i havent quite learned that stuff yet lol thats why im doing that obscene looking fill. hehe. thanks for the advice though

  6. #6
    *this
    Join Date
    Mar 2005
    Posts
    498
    Be carefull in your alive check routine, it looks like you check all ajacent cells even if you're at the edge of the grid.
    i see what you mean, but when i enlarge the grid to ensure that cells arent on the edge, i still get the same output.

  7. #7
    *this
    Join Date
    Mar 2005
    Posts
    498
    please does anyone have any suggestions, im stumped

  8. #8
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Wait I don't understand, is this program actually a game?

  9. #9
    *this
    Join Date
    Mar 2005
    Posts
    498
    lol its called the game of life, the computer plays the game basically. its a computer simulation of the life and death of a population of organisms. It determines the life and death of organisms from one generation to the next.

  10. #10
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Quote Originally Posted by homeyg
    Wait I don't understand, is this program actually a game?
    It's a branch of cellular automata. Not strictly a game, but that's what it was called.

  11. #11
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Like Quantum1024 said, check your alive logic. Your overstepping your array bounds in your alive function. Remember that x-1 and x+1 aren't necessarily inside your array. That's a bug.

    Also, take a look at this:
    Code:
    void cycle (char table[ MAX ][ MAX ], int &total)
    //calculate each generation and assign new locations
    {
       char temp[ MAX ][ MAX ];
       bool life;
       
       fill (temp);
       for (int generation = 0; generation < 5; generation++)
       {
          for (int row = 0; row < MAX; row++)
             for (int col = 0; col < MAX; col++)
             {   
                life = alive(table, row, col);
                if (life)
                   temp[ row ][ col ] = '*';
             }
          table = temp;
       }
       print (table);
    }
    You can't assign arrays like that. You either need to do a member-wise copy, or just use memcpy.
    Code:
    //member-wise copy
       for (int row = 0; row < MAX; row++)
          for (int col = 0; col < MAX; col++)
             table[ row ][ col ] = temp[ row ][ col ];
    //memcpy
       memcpy(table,temp,sizeof(char)*100*100);
    It would help if you told us exactly what kind of errors you were getting.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  12. #12
    *this
    Join Date
    Mar 2005
    Posts
    498
    well the only error is that its not giving me the answer it should be, instead of the correct output:

    Code:
           12345678901234567890
    
     1           **            
     2          * *            
     3           *        **   
     4                  **  *  
     5                  ** **  
     6                 * * **  
     7               * **      
     8               *         
     9            ***  **      
    10          ** ** ** **    
    11          ******         
    12           ** **** **    
    13            *  **  ***   
    14           *  * *** **   
    15            **     ** *  
    16           **      **  * 
    17          * *    **    * 
    18          * *   ***    * 
    19          *    *  * **   
    20
    i get this:
    Code:
          12345678901234567890
    
     1
     2           *
     3
     4
     5                   *
     6
     7
     8                  **
     9                  **
    10                  **
    11
    12              *
    13
    14     *
    15            *
    16                     *
    17       *
    18       *
    19                ***
    20          *     ***
    Last edited by JoshR; 04-01-2005 at 04:12 PM.

  13. #13
    *this
    Join Date
    Mar 2005
    Posts
    498
    eh forget posting that, lol it took away my spaces

  14. #14
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Yup, use code tags instead.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  15. #15
    *this
    Join Date
    Mar 2005
    Posts
    498
    This is what it starts off as:

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

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Open-source Game Project
    By Glorfindel in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 03-24-2009, 02:12 AM
  2. 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, 01:48 AM
  3. C Programming 2d Array Question
    By jeev2005 in forum C Programming
    Replies: 3
    Last Post: 04-26-2006, 04:18 PM
  4. Game Of Life
    By din1983 in forum C Programming
    Replies: 20
    Last Post: 10-11-2005, 11:36 PM
  5. Please help with some coding..
    By stehigs321 in forum C Programming
    Replies: 2
    Last Post: 10-27-2003, 06:44 PM

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