Game Of Life will not update the next generations in C++

Discussion in 'C++' started by zachlin, May 14, 2017.

Tags:
  1. zachlin

    zachlin New Member

    Joined:
    May 13, 2017
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    I made a C++ game of life program that displays generations based on the rules to Conrad's Game of Life but the generations are always the same. Keep in mind, I am a pretty new programmer and I really need help on this because it's for a class assignment! Please help me! Here is my code:

    Code:
    #include <iostream>
    
    #include <iomanip>
    
    #include <ctime>
    
    using namespace std;
    
    
    const int n = 10; //Size of two dimension array (10x10)
    
    
    int check(const char LifeArray[][n + 2], int row, int col) //function to determine number of neighbors
    
    {
    
        int num_neighbors;
    
     
    
        num_neighbors = 0;
    
     
    
        if ((LifeArray[row - 1][col - 1]) == 'X')
    
            num_neighbors++;
    
        if ((LifeArray[row - 1][col]) == 'X')
    
            num_neighbors++;
    
        if ((LifeArray[row - 1][col - 1]) == 'X')
    
            num_neighbors++;
    
        if ((LifeArray[row][col - 1]) == 'X')
    
            num_neighbors++;
    
        if ((LifeArray[row][col + 1]) == 'X')
    
            num_neighbors++;
    
        if ((LifeArray[row + 1][col-1]) == 'X')
    
            num_neighbors++;
    
        if ((LifeArray[row + 1][col]) == 'X')
    
            num_neighbors++;
    
        if ((LifeArray[row + 1][col + 1]) == 'X')
    
            num_neighbors++;
    
     
    
        return num_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);
    
              // srand ( time(NULL) );
    
    
                if((random_integer) == 0)
    
                    {
    
                    life_array[row][col] = '-';
    
                    }
    
                else
    
                    {
    
                    life_array[row][col] = 'X';
    
                    }
    
              
    
                }
    
            }
    
        for(int num_gen = 0;num_gen < 2;num_gen++)      //generation loop for 2 generations
    
            {
    
            cout << "Generation: " << num_gen + 1 << endl; //Header
    
          
    
          
    
            for(row = 1;row < n + 1;row++)        //Print Generation
    
                {
    
                cout << endl;
    
                for(col = 1;col < n + 1;col++)
    
                    {
    
                    cout << life_array[row][col] << " ";
    
                    }
    
                }
    
              
    
            for(row = 1;row < n + 1;row++)     //Loop to determine nieghbors
    
                {
    
                cout << endl;
    
                  
    
                for(col = 1;col < n + 1;col++)
    
                    {
    
                    if ((life_array[row][col]) == 'X') //If box has an X
    
                        {
    
                        num_neighbors = check(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 = check(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 + 1;row++)     //Copies new array to original array
    
                {
    
                for(col = 1;col < n + 2;col++)
    
                    {
    
                    new_array[row][col] = life_array[row][col];
    
                    }
    
                }
    
          
    
          
    
            }
    
        return 0;
    
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice