【问题标题】:Random seeding in Game of Life cannot working well生命游戏中的随机播种无法正常工作
【发布时间】:2017-11-26 03:42:32
【问题描述】:

我正在为我的任务开发人生游戏。我尝试使用随机数进行播种,但下一代无法正常工作。如何使用随机播种来检查下一代。当我使用手动播种时,代码工作正常。但是当我使用随机播种时不起作用。

手动播种

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

随机播种

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;
}
}

主程序

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;
        }
    }
}
}

提前致谢。

【问题讨论】:

  • “不工作”是没有意义的,请解释它是什么以及如何“不工作”。您的手动播种根本没有播种,也没有调用rand()

标签: c++ random seeding


【解决方案1】:
for (col = 0; col < gridSize + 1; col++)
{
    gridOne[gridSize + 1][gridSize + 1] = (int)(rand() % 2);
    cout << gridOne[gridSize + 1][gridSize + 1] << ' ';
}

检查 gridOne 数组上的索引,您总是在内存中编辑超出数组末尾的相同位置。应该是

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多