C++ Game of Life Program

This is a discussion on C++ Game of Life Program within the C++ Programming forums, part of the General Programming Boards category; Hey guys, have any of you written the code for or are familiar with the game of life, invented by ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    46

    C++ Game of Life Program

    Hey guys, have any of you written the code for or are familiar with the game of life, invented by John H. Conway? I have the layout of what is expected in the abstract, however, I have no idea how to begin.

    The game is played on a board consisting of n squares in horizontal and vertical directions whre n is alwasy between 10-25. Each square can be empty or contain an X indicating the presence of an organism. Every square (except the border squares) has eight neighbors.

    The program should read in number n. Then in a loop read initial configureation of organisms and print the orig. game array. It should calculate the next array into the original game aarry, and repeat for cycle of 8 generations. A function should be written to calculate the number of neighbors for each organism.

    this is the formula to calculate the next generation (the whole point of the game):
    1. If an empty square has exactly 2 neighbors an organism is born there
    2. If an organism has less than 3 neighbors then it dies of loneliness.
    3. If an organism has more then 3 neighbors then it dies of overcrowding

    also teacher gave hint of using an array of size n+2 thus forming a border around the actual array. Assume that these borders of the game array are infertile regions where organisms can neither survive nor be born; you will not have to process the border squares. Thus the array will be declared of size (27,27)


    ok thats the basic layout of the game, here is my thoughts so far. I dont need anyone to write the code, as I am really trying to learn this language and how it works. I would love someone to work with me through the problem and help me understand this.

    The board can be randomly generated or controlled by manually assigning the x's (ie. (row 1, col 3 = x)

    This pgm will need 2 arrays, an new one and an old one.

    Should my first step be how to assign the X's?; writing the function?; ? I always get stuck at the beginning of problems like this

    Anyone help describe this game/program in more easy to understand lingo to help me grip what the program really does, etc?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > A function should be written to calculate the number of neighbors for each organism.
    Seems like a good place to start to me

    To calculate the next generation, you call that function in a loop.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    I would start with two basic functions.

    The first randomly populates the array. Loop through the array and use a random number generator to calculate if the spot should have life, something like if a random number is less than 30 out of a hundred.

    The second function loops through the array, calculates how many neighbors each spot has (could be a third function if you want to do this) and either kills the lifeform at the spot, adds a lifeform to ths spot, or leaves it alone. Make sure you don't change the original array though, create a new array with the updated info.

    Once you have those two working properly, then worry about the small specifics as set out by your teacher.

    I'm can't tell if you know how the game of life works or not. Do a search online, theres lots of info on it. Heres a version I did in Windows two years ago. Use the mouse to populate the board, or press "x" to randomly populate it. Either can be done at any time. The press "Enter" to see the next generation or hold on to "Enter" to see many generations quickly. Be warned though, this was written when I first started Windows programming and I didn't fully understand how to repaint the screen when the window was mucked with. So if you cover up the window or resize the window, it'll dissappear! When I finally did understand how to fix this, I never went back and rewrote the code for this program
    Attached Files Attached Files

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Either create a new array or, as I have done with this particular problem, save previous state information in your arrary as well (an array of structs).

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    Hey guys, this program is really giving me problems. I have done code, cleared it, done something else, cleared it, etc...

    This is what I have so far, can anyone help direct me along the right path ,keeping with the instructions set?(using two dimensional array)

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int checkx(char life_array[][],int row,int col,int num_neighbors);
    const int n=12;
    
    int main()
    {
    
        int row;    
        int col;
        char life_array[n+2][n+2];        //original array
        char new_array[n+2][n+2];        //array for next generation
        int num_neighbors=0;                //Neighbors of each square in generation
    
        for(int num_gen=0;num_gen<8;num_gen++)        //generation loop
                                                    //for 8 generations
        {
            cout<<setw(10)<<"Generation:"<<num_gen+1<<endl<<endl; //Header
            
            for(row=1;row<n+2;row++)        //Print Generation
            {
                cout<<endl;
                for(col=1;col<n+2;col++)
                {
                    cout<<life_array[row][col]<<" ";
                }
            }
            for(row=1;row<n+2;row++)
            {
                cout<<endl;
                for(col=1;col<n+2;col++)
                {
                    if (life_array[row][col]='X')
                    {
                        num_neighbors=checkx(life_array[][],row,col,num_neighbors);
                        if (num_neighbors != 3)
                            new_array[row][col]=' ';
                        else
                            new_array[row][col]='X';
                    }
                    else
                    {
                        num_neighbors=checkx(life_array[][],row,col,num_neighbors);
                        if(num_neighbors != 2)
                            new_array[row][col]=' ';
                        else
                            new_array[row][col]='X';
                    }
                }
            }
    
    
            for(row=1;row<n+2;row++)
                for(col=1;col<n+2;col++)
                    new_array[row][col]=life_array[row][col];
    
            return 0;
        }
    }
    
    
        int checkx(char life_array[][n+2],int row,int col,int num_neighbors)
    
        {
    
            if life_array[row-1][col-1]='X';
                num_neighbors++;
            if life_array[row-1][col]='X';
                num_neighbors++;
            if life_array[row-1][col-1]='X';
                num_neighbors++;
            if life_array[row][col-1]='X';
                num_neighbors++;
            if life_array[row][col+1]='X';
                num_neighbors++;
            if life_array[row+1][col-1]='X';
                num_neighbors++;
            if life_array[row+1][col]='X';
                num_neighbors++;
            if life_array[row+1][col+1]='X';
                num_neighbors++;
    
            return num_neighbors;
    
        }

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Code:
    if life_array[row-1][col-1]='X';
                num_neighbors++;
    I believe you want =='X'; in this statement and all the go below it.

    oh, and same thing here:
    Code:
    if (life_array[row][col]=='X')
                    {
                        num_neighbors=checkx(life_array[][],row,col,num_neighbors);
                        if (num_neighbors != 3)
                            new_array[row][col]=' ';
                        else
                            new_array[row][col]='X';
                    }
    sorry, can't help you much more as I've enver played the game...but I would have a seperate function to populate you array.

    You're outputing elements here, yet they have never been set...
    Code:
    for(col=1;col<n+2;col++)
                {
                    cout<<life_array[row][col]<<" ";
                }
    ...and then you pass that same empty array to checkx.

    actually this looks like just some random code slapped around to evoke more help from the community. I'm pretty sure this has been done before on these boards, and PJY also attached something: do a search, work a little harder, and come back with new questions.

    and read about rand();

    edit:: I've double checked, and indeed there are a few threads about this: http://cboard.cprogramming.com/searc...searchid=71047
    Last edited by axon; 09-20-2004 at 11:49 AM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Quote Originally Posted by rayrayj52
    The game is played on a board consisting of n squares in horizontal and vertical directions whre n is alwasy between 10-25.
    Actually the plane of game of life is assumed to be infinite in every direction. For practical reasons, the board is usually a little bit smaller than that though.
    It could be played on a 25x25 board, but there exist patterns which requires boards with millions of squares.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    post your game when it's done cause I want to play
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    What class is the homework for?

    Kuphryn

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Axon pointed out a big error that probably gave you problems with you only using one equal sign. Your other big problem is for some reason you pass num_neighbors to your checkx function. Because of this, num_neigbors never gets resetted to zero which it needs to be with every new cell checked. If you insist of sending it, then add the line "num_neighbors=0" at the beginning of the function.

    Other than that, just create the random populate function and actually ask questions next time instead of just posting code and have us guess what you want.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    thanks for all the help guys, you are really helpfull!! I am still having problems with this.

    The code is listed below.

    My problem is I am getting an error saying, "checkx cannot convert paramater 1 from 'char' to 'char[][14]'

    I have no idea why I am getting this error, can you please help describe what would be causing this.

    Also, I added the random generator function to randomly populate the "x's", does this look right to you guys? Would this have anything to do with the problem?

    Thanks in advance

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    const int n=12; //Size of two dimension array (12x12)
    
    int checkx(char life_array[][n+2],int row,int col);
    //function to determine how many neighbors 
    
    
    int main()
    {
      
      int row;    
      int col;
      char life_array[n+2][n+2];      //original array
      char new_array[n+2][n+2];       //array for next generation
      int num_neighbors=0;                //Neighbors of each square in generation
    
      for(int num_gen=0;num_gen<8;num_gen++)      //generation loop
                                                  //for 8 generations
      {
          cout<<setw(10)<<"Generation:"<<num_gen+1<<endl<<endl; //Header
          
          for(row=1;row<n+2;row++)
              for(col=1;col<n+2;col++)
              {
                  int random_integer=(rand()%2);
                  if(random_integer=0)
                      life_array[row][col]=' ';
                  else
                      life_array[row][col]='X';
                  
              }
          for(row=1;row<n+2;row++)        //Print Generation
          {
              cout<<endl;
              for(col=1;col<n+2;col++)
              {
                  cout<<life_array[row][col]<<" ";
              }
          }
          for(row=1;row<n+2;row++)     //Loop to determine nieghbors
          {
              cout<<endl;
              for(col=1;col<n+2;col++)
              {
                  if ((life_array[row][col])=='X') //If box has an X
                  {
                      num_neighbors=checkx(life_array[row][col],row,col);
                      if (num_neighbors != 3)  //Conditions to determine if X lives or dies
                          new_array[row][col]=' ';
                      else
                          new_array[row][col]='X';
                  }
                  else         //If no 'X' in box
                  {
                      num_neighbors=checkx(life_array[row][col],row,col);
                      if(num_neighbors != 2)   //Condition to determine if X is born
                          new_array[row][col]=' ';
                      else
                          new_array[row][col]='X';
                  }
              }
          }
    
    
          for(row=1;row<n+2;row++)     //Copies new array to original array
              for(col=1;col<n+2;col++)
                  new_array[row][col]=life_array[row][col];
    
          return 0;
      }
    }
    
    
      int checkx(char life_array[][n+2],int row,int col)
       //function to determine number of neighbors
      {
    
          
       
          int num_neighbors;
    
          num_neighbors=0;
    
          if ((life_array[row-1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row-1][col])=='X')
              num_neighbors++;
          if ((life_array[row-1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row][col-1])=='X')
              num_neighbors++;
          if ((life_array[row][col+1])=='X')
              num_neighbors++;
          if ((life_array[row+1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row+1][col])=='X')
              num_neighbors++;
          if ((life_array[row+1][col+1])=='X')
              num_neighbors++;
    
          return num_neighbors;
    
      }

  12. #12
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Code:
                 if ((life_array[row][col])=='X') //If box has an X
                  {
                      num_neighbors=checkx(life_array[row][col],row,col);
                      if (num_neighbors != 3)  //Conditions to determine if X lives or dies
                          new_array[row][col]=' ';
                      else
                          new_array[row][col]='X';
                  }
                  else         //If no 'X' in box
                  {
                      num_neighbors=checkx(life_array[row][col],row,col);                  if(num_neighbors != 2)   //Condition to determine if X is born
                          new_array[row][col]=' ';
                      else
                          new_array[row][col]='X';
    the problem is in red. checkx() is expecting you to pass an array, but u have passed an array element. I didnt check what you are suppose to pass though

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    46
    ok, great, this solved my first problem, the code now executes, however, I dont think it is working quite yet. I believe it still needs tweaking because, it doesn't seem to work right, does anyone notice anything wrong in the source code? I get the 8 generations to populate and randomly as well, however, it doesnt seem to be following the rules I set, and I think I have small changes that need to be made, does anything stand out to anyone with the new code posted below?

    I think it could be the assignment of new array and life array

    Also the "X's" show up in 13X13 and I need to get them to show up in 12X12, any suggestions on how to alter the code to display in 12X12?

    Thanks!!!

    Code:
    #include <iostream>
    #include <iomanip>
    #include <cstdlib>
    
    using namespace std;
    
    const int n=12; //Size of two dimension array (12x12)
    
    int checkx(const char life_array[][n+2],int row,int col);
    //function to determine how many neighbors 
    
    
    int main()
    {
      
      int row;    
      int col;
      char life_array[n+2][n+2];      //original array
      char new_array[n+2][n+2];       //array for next generation
      int num_neighbors=0;                //Neighbors of each square in generation
    
           for(row=1;row<n+2;row++)
              for(col=1;col<n+2;col++)
              {
                  int random_integer=(rand()%2);
                  if((random_integer)==0)
                      life_array[row][col]=' ';
                  else
                      life_array[row][col]='X';
                  
              }
      for(int num_gen=0;num_gen<8;num_gen++)      //generation loop
                                                  //for 8 generations
      {
          cout<<setw(10)<<"Generation:"<<num_gen+1<<endl<<endl; //Header
          
        
          for(row=1;row<n+2;row++)        //Print Generation
          {
              cout<<endl;
              for(col=1;col<n+2;col++)
              {
                  cout<<life_array[row][col]<<" ";
              }
          }
          for(row=1;row<n+2;row++)     //Loop to determine nieghbors
          {
              cout<<endl;
              for(col=1;col<n+2;col++)
              {
                  if ((life_array[row][col])=='X') //If box has an X
                  {
                      num_neighbors=checkx(life_array,row,col);
                      if (num_neighbors != 3)  //Conditions to determine if X lives or dies
                          new_array[row][col]=' ';
                      else
                          new_array[row][col]='X';
                  }
                  else         //If no 'X' in box
                  {
                      num_neighbors=checkx(life_array,row,col);
                      if(num_neighbors != 2)   //Condition to determine if X is born
                          new_array[row][col]=' ';
                      else
                          new_array[row][col]='X';
                  }
              }
          }
    
    
          for(row=1;row<n+2;row++)     //Copies new array to original array
              for(col=1;col<n+2;col++)
                  new_array[row][col]=life_array[row][col];
    
          
      }
      return 0;
    }
    
    
      int checkx(const char life_array[][n+2],int row,int col)
       //function to determine number of neighbors
      {
    
          int num_neighbors;
    
          num_neighbors=0;
    
          if ((life_array[row-1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row-1][col])=='X')
              num_neighbors++;
          if ((life_array[row-1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row][col-1])=='X')
              num_neighbors++;
          if ((life_array[row][col+1])=='X')
              num_neighbors++;
          if ((life_array[row+1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row+1][col])=='X')
              num_neighbors++;
          if ((life_array[row+1][col+1])=='X')
              num_neighbors++;
    
          return num_neighbors;
    
      }

  14. #14
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Code:
    if ((life_array[row-1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row-1][col])=='X')
              num_neighbors++;
    
    //not right, i think   
       if ((life_array[row-1][col-1])=='X')
              num_neighbors++;
    
    
    
          if ((life_array[row][col-1])=='X')
              num_neighbors++;
          if ((life_array[row][col+1])=='X')
              num_neighbors++;
          if ((life_array[row+1][col-1])=='X')
              num_neighbors++;
          if ((life_array[row+1][col])=='X')
              num_neighbors++;
          if ((life_array[row+1][col+1])=='X')
              num_neighbors++;
    this is my conjecture cos i dont know how the game works

  15. #15
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, Raison pointed out your mistake, you have [row-1][col-1] twice in your check neighbor function. As for printing out 12x12, only go to n+1 in your loops.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 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, 12:48 AM
  3. interfacing a program with a game
    By dragonklown in forum C++ Programming
    Replies: 3
    Last Post: 02-16-2005, 11:59 PM
  4. viewport window for life program
    By rip1968 in forum Windows Programming
    Replies: 1
    Last Post: 04-01-2003, 10:45 AM

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