Thread: my game of life, array help

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    2

    my game of life, array help

    My board is not displaying after the initial generation is shown. I have a feeling my loops are a bit funking but i cannot spot it.
    Code:
    #include <iostream>
    
    using namespace std;
    
    #include <conio.h>    // specialized keyboard functions like _kbhit
    #include <memory.h>
    #include <time.h>    // functions dealing with the clock
    
    void main()
    {
        const    int        MaxCols(60);
        const    int        MaxRows(60);
        const    time_t    WaitTime(3);
    
        bool    CurrBoard[MaxRows + 2][MaxCols + 2];
        bool    NextBoard[MaxRows + 2][MaxCols + 2];
        int        Col;
        time_t    CurrTime;
        int        Generation;
        int        i;
        int        j;
        int        NumNeighbors;
        int        Row;
    
        memset(CurrBoard, false, (MaxRows + 2) * (MaxCols + 2) * sizeof (bool));    // set all cells in an array to a value
        memset(NextBoard, false, (MaxRows + 2) * (MaxCols + 2) * sizeof (bool));
    
        //    Get which cells are alive from the user
        cout << "Enter the row and column number of each cell to be alive\nuse -1 and -1 to stop" << endl;
        cout << "Row values range from 1 to " << MaxRows << endl;
        cout << "Column values range from 1 to " << MaxCols << endl;
        do    {
            cout << "=> ";
            cin >> Row >> Col;
            // make sure value are inside the bounds of the array
            CurrBoard[Row][Col] = true;
        } while ((Row != -1) && (Col != -1));
    
        Generation = 0;
        cout << "\tGeneration " << Generation << endl;
        for (Row = 1; Row <= MaxRows; Row++)        // display the board
        {
            for (Col = 1; Col <= MaxCols; Col++)
                cout << (CurrBoard[Row][Col] ? '*' : ' ');
            cout << endl;
        }
    
        do    {        //    Loop through each generation
            Generation++;
            //        Loop through every cell on the board
            for (Row = 1; Row <= MaxRows; Row++)
            {
    
                for (Col = 1; Col <= MaxCols; Col++)
                {
    
                    //            count number of living neighbors
                    NumNeighbors = 0;    // one way to count neighbors
                    if (CurrBoard[Row - 1][Col - 1])
                        NumNeighbors++;
                    if (CurrBoard[Row - 1][Col])
                        NumNeighbors++;
                    if (CurrBoard[Row - 1][Col + 1])
                        NumNeighbors++;
                    if (CurrBoard[Row][Col - 1])
                        NumNeighbors++;
                    if (CurrBoard[Row][Col + 1])
                        NumNeighbors++;
                    if (CurrBoard[Row + 1][Col - 1])
                        NumNeighbors++;
                    if (CurrBoard[Row + 1][Col])
                        NumNeighbors++;
                    if (CurrBoard[Row + 1][Col + 1])
                        NumNeighbors++;
    
                    //            if Number of Neighbors >= 4
                    if (NumNeighbors >= 4)
    
                        NextBoard[Row][Col] == false;
    
                    //                    Cell is false            NextBoard [Row] [Col] = false;
                    //            if Number of neighbors <= 1
                    //                    Cell is false
                    if (NumNeighbors <= 1)
    
                        NextBoard[Row][Col] == false;
    
                    //            if Number of Neighbors == 3
                    //                    Cell is true
                    if (NumNeighbors == 3)
    
                        NextBoard[Row][Col] == true;
    
                    //            if Number of Neighbors == 2
                    //                    Cell does not change    NextBoard [Row] [Col] = CurrBoard [Row] [Col]
    
                    if (NumNeighbors == 2)
    
                        NextBoard[Row][Col] == CurrBoard[Row][Col];
                }
    
            }
    
    
    
            //            end loop
            memcpy(CurrBoard, NextBoard, (MaxRows + 2) * (MaxCols + 2) * sizeof (bool));    // copies NextBoard into CurrBoard
            cout << "\tGeneration " << Generation << endl;
            for (Row = 1; Row <= MaxRows; Row++)        // display the board
            {
                for (Col = 1; Col <= MaxCols; Col++)
                    cout << (CurrBoard[Row][Col] ? '*' : ' ');
                cout << endl;
            }
            CurrTime = time(0);    //    gets current time in seconds from Jan 1, 1970
            do    {
            } while ((time(0) - CurrTime) < WaitTime);
        } while (!_kbhit());    //    end loop if user hits key
    }

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>NextBoard[Row][Col] == false;
    This is comparison, not assignment; hence these statements do nothing.

    Other advice:
    - Do not use void main. It is not standard and never has been. main shall return int. This is required by the standard.
    - Do not use memset. Get into the habit of using std::set instead.
    - Do not use memcpy. Get into the habit of using std::copy instead.
    - Use std::array instead of C arrays.
    - Use C++ headers: time.h should be ctime and memory.h should be cmemory.
    - Do not use conio.h. It's not standard and is not available on all platforms and compilers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    2
    thank you for pointing that out. i have changed the comparison into assignment, but my board is not showing what i want it show. Thank you for the advice I will keep that in mind as i continue to learn.

    Code:
                    if (NumNeighbors >= 4)
    
                        NextBoard[Row][Col] == ' ';
    
                    //                    Cell is false            NextBoard [Row] [Col] = false;
                    //            if Number of neighbors <= 1
                    //                    Cell is false
                    if (NumNeighbors <= 1)
    
                        NextBoard[Row][Col] == ' ';
    
                    //            if Number of Neighbors == 3
                    //                    Cell is true
                    if (NumNeighbors == 3)
    
                        NextBoard[Row][Col] == '*';
    
                    //            if Number of Neighbors == 2
                    //                    Cell does not change    NextBoard [Row] [Col] = CurrBoard [Row] [Col]
    
                    if (NumNeighbors == 2)
    
                        NextBoard[Row][Col] == CurrBoard[Row][Col];

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>NextBoard[Row][Col] == ' ';
    >>NextBoard[Row][Col] == ' ';
    >>NextBoard[Row][Col] == '*';
    >>NextBoard[Row][Col] == CurrBoard[Row][Col];
    You are still using comparison (==) instead of assignment (=).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. the game of life
    By ben432 in forum C Programming
    Replies: 1
    Last Post: 06-13-2011, 10:23 AM
  2. Game of Life... Turned my life to HELL!
    By yigster in forum C Programming
    Replies: 1
    Last Post: 05-21-2009, 06:29 PM
  3. Game of Life
    By CornedBee in forum Contests Board
    Replies: 74
    Last Post: 05-20-2008, 01:50 AM
  4. Game Of Life
    By din1983 in forum C Programming
    Replies: 20
    Last Post: 10-11-2005, 10:36 PM
  5. Game of life
    By JoshR in forum C++ Programming
    Replies: 30
    Last Post: 04-03-2005, 02:17 PM