samedi 25 novembre 2017

Random seeding in Game of Life cannot working well

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.




Aucun commentaire:

Enregistrer un commentaire