Thread: [C++] Conway Game of Life - Help!

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    1

    Unhappy [C++] Conway Game of Life - Help!

    I've spent an embarrassing number of hours trying to figure this one part out, but am still stuck and desperate for some help.
    The professor has us starting us out with making a "U" shape w/ 1s while the grid is 0s, and updating that according to the rules.
    The problem is, is that when it updates to the next generation, the 1's that are supposed to update do update, but now in this second generation there are the old 1s and the new 1s, and there is overpopulating going on, but no cells are dying (Ex. One cell has 5 1's around it, and according to the rules and my code it should be dead, but it doesn't die and remains a 1, even in the 3rd, 4th 5th generation and so on).
    I have a feeling this has to do with rule 6: "All births and deaths take place at exactly the same time, so that dying cells can help to give birth to another, but cannot prevent the death of others by reducing overcrowding, nor can cells being born either preserve or kill cells in the previous generation." or this response I found online to another question. I don't quite understand the response. I do have a temporary Array that I am transferring to and from the main Array.
    I hope someone can point me in the right direction. Thank you so much.

    Code:
    
    
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <windows.h>
    #include <conio.h>
    
    
    using namespace std;
    
    
    /////////////GLOBAL VARIABLES/////////////
    int row, col;
    char PorQ;
    const int MAX_COL = 60;
    const int MAX_ROW = 30;
    
    
    int currentArray[MAX_ROW][MAX_COL];
    int tempArray[MAX_ROW][MAX_COL];
    int Array[MAX_ROW][MAX_COL];
    
    
    /////////////FUNCTION DECLARATIONS/////////////
    void displayMenu(void);
    
    
    void setZeroArray (int tempArray[MAX_ROW][MAX_COL], int currentArray[MAX_ROW][MAX_COL]);
    
    
    void setInitialPatternArray (void);
    
    
    void copyArray(int tempArray[MAX_ROW][MAX_COL], int currentArray[MAX_ROW][MAX_COL]);
    
    
    void displayArray (int Array[MAX_ROW][MAX_COL]);
    
    
    void setNextGenArray (int currentArray[MAX_ROW][MAX_COL], int tempArray[MAX_ROW][MAX_COL]);
    
    
    /////////////MAIN FUNCTION/////////////
    int main()
    {
        displayMenu();
        setZeroArray(currentArray, tempArray);
        setInitialPatternArray();
        copyArray(tempArray, currentArray);
        displayArray (currentArray);
    
    
        cout << endl << ">> ";
        cin >> PorQ;
    //quitting first time
        if ((PorQ == 'Q') || (PorQ == 'q'))
        {
            exit(0);
        }
        while ((PorQ != 'Q') && (PorQ != 'q') && (PorQ != 'P') && (PorQ != 'p'))
        {
            cout << "Invalid Entry. Press [P]lay or [Q]uit." << endl;
            cin >> PorQ;
        }
    
    
        if ((PorQ == 'P') || (PorQ == 'p'))
        {
            char ans;
            bool t;
            do
            {
                while (1)
                {
                    if (t = kbhit())//checks for user keyboard input
                    {
                        break;
                    }
                    else
                    {
                        system("cls");
    
    
                        setNextGenArray (currentArray, tempArray);
                        copyArray(tempArray, currentArray);
                        displayArray (currentArray);
    
    
    
    
                        Sleep(1000);
                    }
                }
    
    
                ans = getch();//if user keyboard input is q, quit
    
    
            }while (ans != 'q');
        }
        return 0;
    }
    
    
    
    
    
    
    /////////////FUNCTION DEFINITIONS/////////////
    void displayMenu(void)
    {
        cout << "[P]lay > Press 'P' to play." << endl;
        cout << "[Q]uit > Press 'Q' to exit." << endl;
    }
    
    
    void setZeroArray (int array1[MAX_ROW][MAX_COL], int array2[MAX_ROW][MAX_COL])
    {
        for (int row = 0; row < MAX_ROW; row++)
        {
            for (int col = 0; col < MAX_COL; col++)
            {
                array1[row][col] = 0;
            }
        }
    
    
        for (int row = 0; row < MAX_ROW; row++)
        {
            for (int col = 0; col < MAX_COL; col++)
            {
                array2[row][col] = 0;
            }
        }
    }
    
    
    void setInitialPatternArray (void)
    {
        srand(time(NULL));
        int randomrow;
        for (int i = 0; i <100; i++)
        {
            randomrow = rand() % (MAX_ROW - 6);
        }
    
    
        srand(time(NULL));
        int randomcol;
        for (int i = 0; i <100; i++)
        {
            randomcol = rand() % (MAX_COL - 6);
        }
    
    
        for (row = 0 + randomrow; row < MAX_ROW; row++)
        {
            for(col = 0 + randomcol; col < MAX_COL; col++)
            {
                if (row <= 4 + randomrow && col == 0 + randomcol)
                {
                    tempArray[row][col] = 1;
                }
    
    
                if (row == 5 + randomrow && col <= 6 + randomcol)
                {
                    tempArray[row][col] = 1;
                }
    
    
                if (row <= 5 + randomrow && col == 6 + randomcol)
                {
                    tempArray[row][col] = 1;
                }
            }
        }
    }
    
    
    void copyArray(int array1[MAX_ROW][MAX_COL], int array2[MAX_ROW][MAX_COL])
    {
        for (int row = 0; row < MAX_ROW; row++)
        {
            for (int col = 0; col < MAX_COL; col++)
            {
               array2[row][col] = array1[row][col];
            }
        }
    }
    void displayArray (int Array[MAX_ROW][MAX_COL])
    {
        for (row = 0; row < MAX_ROW; row++)
            {
                for(col = 0; col < MAX_COL; col++)
                {
                    cout << Array[row][col] << " ";
                }
            }
    }
    
    
    
    
    void setNextGenArray (int currentArray[MAX_ROW][MAX_COL], int tempArray[MAX_ROW][MAX_COL])
    {
    //copies currentArray into tempArray so that GoL modifications can take place on tempArray
        for (int row = 0; row < MAX_ROW; row++)
        {
            for (int col = 0; col < MAX_COL; col++)
            {
               tempArray[row][col] = currentArray[row][col];
            }
        }
    
    
        for (int row = 0; row < MAX_ROW; row++)
        {
            for (int col = 0; col < MAX_COL; col++)
            {
               tempArray[row][col] = 0;
            }
        }
    
    
    
    
    //tempArray now reflects current state of currentArray that is displayed. will modify below v
    //MIDDLE CELLS - check surrounding 8
        for (int row = 0; row < MAX_ROW; row++)
        {
            for (int col = 0; col < MAX_COL; col++)
            {
                if (((row > 1)  && (row < MAX_ROW)) && ((col > 1) && (col < MAX_COL)))
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1) //if there is a 1 in the box
                    {
                        if (tempArray[row-1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col+1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col+1] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col+1] == 1)
                            counter++;
    
    
    
    
                        if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter = 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                        //The cell ded.
                    }
                    else if (tempArray[row][col] == 0)//if cell is 0, check around to see if surrounded by 3
                    {
                        if (tempArray[row-1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col+1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col+1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col+1] == 1)
                            dedtoliv++;
    
    
                        if (dedtoliv == 3) //if the box that has 0 in it is surrounded by 3, the cell will become a 1
                            tempArray[row][col] = 1;
                        //ded cell to liv
                    }
            }
    
    
            }
    
    
        }
    }
    
    
    /*FOCUS ON MIDDLE CELLS FIRST TO FIGURE OUT ISSUE 
    //CORNER CELLS - check surrounding 3
        //upper left corner
        for (int row = 0; row < 1; row++)
            {
                for (int col = 0; col < 1; col++)
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1)
                    {
                        if (tempArray[row+1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col+1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col+1] == 1)
                            counter++;
    
    
    
    
                        if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter == 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                        //The cell ded.
                    }
                    else
                    {
                        if (tempArray[row+1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col+1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col+1] == 1)
                            dedtoliv++;
    
    
    
    
                        if (dedtoliv == 3) //if the box that has 0 in it is surrounded by 3, the cell will become a 1
                            tempArray[row][col] = 1;
                        //ded cell to liv
    
    
    
    
                    }
                }
            }
    
    
    
    
        //lower left corner
        for (int row = 30; row < 31; row++)
            {
                for (int col = 0; col < 1; col++)
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1)
                    {
                        if (tempArray[row-1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row][col+1] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col+1] == 1)
                            counter++;
    
    
    
    
                         if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter == 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                    //The cell ded.
                    }
                    else
                    {
                        if (tempArray[row-1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col+1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col+1] == 1)
                            dedtoliv++;
    
    
    
    
                        if (dedtoliv == 3)
                            tempArray[row][col] = 1;
                        //The ded cell to liv cell
                    }
                }
            }
    
    
        //upper right corner
        for (int row = 0; row < 1; row++)
            {
                for (int col = 60; col < 61; col++)
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1)
                    {
                        if (tempArray[row][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col] == 1)
                            counter++;
    
    
    
    
                        //cell ded or liv
                        if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter == 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                        //The cell ded.
                    }
                    else
                    {
                        if (tempArray[row][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col] == 1)
                            dedtoliv++;
    
    
    
    
                        if (dedtoliv == 3)
                            tempArray[row][col] = 1;
                    }
                }
            }
    
    
        //lower right corner
        for (int row = 30; row < 31; row++)
            {
                for (int col = 60; col < 61; col++)
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1)
                    {
                        if (tempArray[row-1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col-1] == 1)
                            counter++;
    
    
    
    
                        //cell ded or liv
                        if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter == 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                    }
                    else
                    {
                        if (tempArray[row-1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col-1] == 1)
                            dedtoliv++;
    
    
    
    
                        if (dedtoliv == 3)
                            tempArray[row][col] = 1;
                        //The ded cell to liv cell
                    }
                }
            }
    
    
    
    
    //SIDES - check surrounding 5
        //left side
         for (int row = 1; row < 30; row++)
            {
                for (int col = 0; col < 1; col++)
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1)
                    {
                        if (tempArray[row-1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col+1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col+1] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col+1] == 1)
                            counter++;
    
    
                        //cell ded or liv
                        if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter == 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                        //The cell ded.
                    }
                    else
                    {
                        if (tempArray[row-1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col+1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col+1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col+1] == 1)
                            dedtoliv++;
    
    
    
    
                        if (dedtoliv == 3)
                        tempArray[row][col] = 1;
                    //The ded cell to liv cell
                    }
                }
            }
    
    
        //bottom
         for (int row = 30; row < 31; row++)
            {
                for (int col = 1; col < 60; col++)
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1)
                    {
                        if (tempArray[row-1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col+1] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col+1] == 1)
                            counter++;
    
    
    
    
                        //cell ded or liv
                        if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter == 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                        //The cell ded.
                    }
                    else
                    {
                        if (tempArray[row-1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col+1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col+1] ==1)
                            dedtoliv++;
    
    
    
    
                         if (dedtoliv == 3)
                            tempArray[row][col] = 1;
                        //The ded cell to liv cell
                    }
                }
            }
    
    
        //right side
        for (int row = 1; row < 30; row++)
            {
                for (int col = 60; col < 61; col++)
                {
                    int counter = 0;
                    int dedtoliv = 0;
    
    
                    if (tempArray[row][col] == 1)
                    {
                        if (tempArray[row-1][col] == 1)
                            counter++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col-1] == 1)
                            counter++;
    
    
                        if (tempArray[row+1][col] == 1)
                            counter++;
    
    
                        //cell ded or liv
                        if(counter <= 1)
                            tempArray[row][col] = 0;
                        //The cell ded.
                        if((counter == 3) || (counter == 2))
                            tempArray[row][col] = 1;
                        //The cell liv.
                        if(counter >= 4)
                            tempArray[row][col] = 0;
                        //The cell ded.
                    }
                    else
                    {
                        if (tempArray[row-1][col] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row-1][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col-1] == 1)
                            dedtoliv++;
    
    
                        if (tempArray[row+1][col] == 1)
                            dedtoliv++;
    
    
    
    
                        if (dedtoliv == 3)
                        tempArray[row][col] = 1;
                        //The ded cell to liv cell
                    }
                }
            }
    
    
        //top
        for (int row = 0; row < 1; row++)
        {
            for (int col = 1; col < 60; col++)
            {
                int counter = 0;
                int dedtoliv = 0;
    
    
                if (tempArray[row][col] == 1)
                {
                    if (tempArray[row][col-1] == 1)
                        counter++;
    
    
                    if (tempArray[row+1][col-1] == 1)
                        counter++;
    
    
                    if (tempArray[row+1][col] == 1)
                        counter++;
    
    
                    if (tempArray[row+1][col+1] == 1)
                        counter++;
    
    
                    if (tempArray[row][col+1] == 1)
                        counter++;
    
    
    
    
                    //cell ded or liv
                    if(counter <= 1)
                        tempArray[row][col] = 0;
                    //The cell ded.
                    if((counter == 3) || (counter == 2))
                        tempArray[row][col] = 1;
                    //The cell liv.
                    if(counter >= 4)
                        tempArray[row][col] = 0;
                    //The cell ded.
                }
                else
                {
                    if (tempArray[row][col-1] == 1)
                        dedtoliv++;
    
    
                    if (tempArray[row+1][col-1] == 1)
                        dedtoliv++;
    
    
                    if (tempArray[row+1][col] == 1)
                        dedtoliv++;
    
    
                    if (tempArray[row+1][col+1] == 1)
                        dedtoliv++;
    
    
                    if (tempArray[row][col+1] == 1)
                        dedtoliv++;
    
    
    
    
                    if (dedtoliv == 3)
                    tempArray[row][col] = 1;
                    //The ded cell to liv cell
                }
            }
        }
    }
    */



  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    What's the point of posting 550 commented-out lines? Yikes!

    What's with all the global variables? And if you really want them to be global why are you "passing" them to functions as well?

    Don't call srand more than once. It's best called early in main. (Actually, in C++ it's best to use the C++ random number facilities.)

    What do you think this is doing (and the next similar piece of code)? It's setting randomrow 100 times ... for no apparent purpose.
    Code:
        int randomrow;
        for (int i = 0; i < 100; i++)
            randomrow = rand() % (MAX_ROW - 6);
    And look at this:
    Code:
        for (int row = 0; row < MAX_ROW; row++)
            for (int col = 0; col < MAX_COL; col++)
               tempArray[row][col] = currentArray[row][col];
     
        for (int row = 0; row < MAX_ROW; row++)
            for (int col = 0; col < MAX_COL; col++)
               tempArray[row][col] = 0;
    What in the world!?

    And:
    Code:
        for (int row = 0; row < MAX_ROW; row++)
        {
            for (int col = 0; col < MAX_COL; col++)
            {
                if (row > 1 && row < MAX_ROW && col > 1 && col < MAX_COL)
                {
    If you don't want to process col == 0 and row == 0, why bother starting at zero in the loops? And row and col will always be < their limits, as given in the for loops. I assume you're trying to not go out-of-bounds, but this isn't the way to do it. You need the checks for each if statement in the neighbour counting code. And row needs to be < MAX_ROW - 1 (similarly for col).

    And you don't need separate counting code for a live or dead cell. Just count once. You're over-complicating things!

    Anyway, your main problem is that you are copying the whole array to a temp array and then processing the temp array. That's totally missing the point of having two arrays. The point is that you can't modify the array that you are scanning, otherwise the changes you make as you scan will affect the scan! So don't copy the array first. Instead, scan the main array and set the elements of the temp array based on that scan. Then copy the temp back to the main array. (If you were trying to be efficient you could toggle back and forth between the two arrays, but that's not important.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conway's Game of Life
    By trdains in forum C++ Programming
    Replies: 1
    Last Post: 01-25-2014, 02:50 PM
  2. Conway's game of life in parallel
    By std10093 in forum C Programming
    Replies: 15
    Last Post: 07-25-2013, 01:01 PM
  3. Conway's Game of Life using pointers!
    By john_member in forum C Programming
    Replies: 2
    Last Post: 03-10-2013, 11:21 PM
  4. Conway's Game of Life
    By Anhur in forum C Programming
    Replies: 37
    Last Post: 09-27-2012, 10:54 PM
  5. John Conway's Game Of Life
    By O Mighty Nips in forum C Programming
    Replies: 3
    Last Post: 05-14-2011, 11:30 PM

Tags for this Thread