-1

I'm developing game of life for my assignment. I try to use random number for seeding but the next generation is not working. How can I use the random seeding to check the next generation. When I use the manual seeding the code working fine. But won't work when I use random seeding.

Manual Seeding

for (int i = 0;i<n;i++)
{
    cout << "Enter the coordinates of cell " << i + 1 << " : ";
    cin >> x >> y;
    gridOne[x][y] = true;
    printGrid(gridOne);
}

Random Seeding

void generation(int gridOne[gridSize + 1][gridSize + 1])
{
int row, col;
srand(time(0));
for (row = 0; row < gridSize + 1; row++)
{
    for (col = 0; col < gridSize + 1; col++)
    {
        gridOne[gridSize + 1][gridSize + 1] = (int)(rand() % 2);
        cout << gridOne[gridSize + 1][gridSize + 1] << ' ';
    }
    cout << endl;
}
}

Main program

int main()
{
clock_t begin, finish;
bool gridOne[gridSize + 1][gridSize + 1] = {};

//seeding code

cout << "Please enter number of iteration : " << endl;
cin >> iteration;

for (int a = 0; a<iteration; a++)
{
    begin = clock();

    printGrid(gridOne);
    determineState(gridOne);
    cout << endl;
}
finish = clock();
cout << "The elapsed time : " << (double)(finish - begin) / CLOCKS_PER_SEC;
system("pause");
return 0;
}


void printGrid(bool gridOne[gridSize + 1][gridSize + 1]) {
for (int a = 1; a < gridSize; a++)
{
    for (int b = 1; b < gridSize; b++)
    {
        if (gridOne[a][b] == true)
        {
            cout << " \xDB ";
        }
        else
        {
            cout << " . ";
        }
        if (b == gridSize - 1)
        {
            cout << endl;
        }
    }
    }
    }


     void compareGrid(bool gridOne[gridSize + 1][gridSize + 1], bool gridTwo[gridSize + 1][gridSize + 1]) {

for (int a = 0; a < gridSize; a++)
{
    for (int b = 0; b < gridSize; b++)
    {
        gridTwo[a][b] = gridOne[a][b];
    }
}
}

void determineState(bool gridOne[gridSize + 1][gridSize + 1]) {
bool gridTwo[gridSize + 1][gridSize + 1] = {};
compareGrid(gridOne, gridTwo);

for (int a = 1; a < gridSize; a++)
{
    for (int b = 1; b < gridSize; b++)
    {
        int alive = 0;
        for (int c = -1; c < 2; c++)
        {
            for (int d = -1; d < 2; d++)
            {
                if (!(c == 0 && d == 0))
                {
                    if (gridTwo[a + c][b + d])
                    {
                        ++alive;
                    }
                }
            }
        }
        if (alive < 2)
        {
            gridOne[a][b] = false;
        }
        else if (alive == 3)
        {
            gridOne[a][b] = true;
        }
        else if (alive > 3)
        {
            gridOne[a][b] = false;
        }
    }
}
}

Thanks in advance.

  • 2
    "not working" is meaningless, please explain what and how it's "not working". Your manual seeding has no seeding at all and no calls to `rand()`. – President James K. Polk Nov 26 '17 at 03:45
  • 1
    Possibly relevant https://stackoverflow.com/questions/27209282/how-does-calling-srand-more-than-once-affect-the-quality-of-randomness – Galik Nov 26 '17 at 03:51

1 Answers1

2
for (col = 0; col < gridSize + 1; col++)
{
    gridOne[gridSize + 1][gridSize + 1] = (int)(rand() % 2);
    cout << gridOne[gridSize + 1][gridSize + 1] << ' ';
}

Check the indices on the gridOne array, you are always editing the same place in memory that is past the end of the array. Should be

for (col = 0; col < gridSize + 1; col++)
{
    gridOne[row][col] = (int)(rand() % 2);
    cout << gridOne[row][col] << ' ';
}