0

Can you guys help me build conway's game of life? I don't need anyone to build it for me, but if you could help me work through it, I have no idea how to implement the rules correctly.

Here is the basic board which I have set up so far..

#include <iostream>

using namespace std;
#include <conio.h>
#include <memory.h>
#include <time.h>

void main ()

{
const   long    NumCols     (20);
const   long    NumRows     (20);
const   long    WaitTime    (2);

        bool    Board [NumRows] [NumCols];
        long    Col;
        long    Generation;
        long    i;
        long    NumCellsToOccupy;
        long    Row;
        time_t  StartTime;


memset (Board, 0, sizeof (Board));  
do  {
    cout << "How many cells do you want occupied? ";
    cin >> NumCellsToOccupy;
    } while ((NumCellsToOccupy <=0) || (NumCellsToOccupy > (NumRows * NumCols)));
for (i = 0; i < NumCellsToOccupy; i++)
    {
    do  {
        cout << "Enter the row and col for occupied cell #" << (i + 1) << ": ";
        cin >> Row >> Col;
        } while ((Row < 1) || (Row > NumRows) || (Col < 1) || (Col > NumCols));
    Board [Row - 1] [Col - 1] = true;
    }



Generation = 0;
do  {

    cout << "Generation: " << Generation << endl;
    for (Row = 0; Row < NumRows; Row++)
        {
        for (Col = 0; Col < NumCols; Col++)

            cout << (Board [Row] [Col] ? '*' : ' ');
        cout << endl;
        }


    StartTime = time (0);
    do  {
        } while ((time (0) - StartTime) < WaitTime);

    Generation ++;
    } while (!_kbhit ());   
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by kes166
0

I'm assuming you mean these rules.

I would use two 2d arrays instead of four 1d arrays. You only have 2 1d arrays. The reason I would have an extra 2d array is to preserve the current board without altering it while coming up with the next phase of life.
o = life cell, x = dead cell

Phase 1:
o x o x
x o x o
x x x x
o o o o

Phase 2:
x o o x
x o o x
o x x o
x o o x

Phase 3:
x x x x
o x x o
o x x o
x o o x

Phase 4: 

etc etc etc

You will need to preserve the 2d array until you have the new 2d array filled out.

Edit:

Basically, I would start like

int main(int argc, char* argv[]) {
    bool oldBoard [20][20];
    bool newBoard [20][20];
    ...
}

Edit #2: Wait, I missed your 2d array bool on the first pass through. your line 11 and 12 threw me off a litte. If you know the size of the board, you don't need const, just put the size directly in the variable declaration

Edited by kes166: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.