为什么我的 C++生命游戏无法正常运行?

发布于 2024-09-29 10:59:27 字数 1492 浏览 5 评论 0原文

编译并运行正常,但结果与应有的完全不同。

我剪掉了不相关的代码:

bool grid[1280][1024]; // hardcoded to my screen res for now

for (int x = 0; x<1280; x++)     //init grid to false
{
    for (int y = 0; y<1024; y++)
    {
        grid[x][y] = false;
    }
}

grid[320][120] = true; // add a simple pattern
grid[320][121] = true;
grid[320][122] = true;
grid[320][123] = true;
grid[320][124] = true;
grid[320][125] = true;
grid[320][126] = true;
grid[320][127] = true;
grid[320][128] = true;

// [snipped]

for (int x = 1; x< (1280 - 1); x++)
{
    for (int y = 1; y< (1024 - 1); y++)
    {
        int n = 0; // neighbours
        if (grid[x-1][y-1]) n++; // top left
        if (grid[x][y-1])   n++; // top middle
        if (grid[x+1][y-1]) n++; // top right

        if (grid[x-1][y])   n++; // left
        if (grid[x+1][y])   n++; // right

        if (grid[x-1][y+1]) n++; // bottom left
        if (grid[x][y+1])   n++; // bottom middle
        if (grid[x+1][y+1]) n++; // bottom right


        if (grid[x][y]) // current cell is on
        {
            SetPixel(screen, x, y, on); // drawing function

            if (n < 2) grid[x][y] = false; // die. :(
            if (n > 3) grid[x][y] = false; // die. :(
            // otherwise (2 or 3), survive. :)

        }
        else // current cell is off
        {
            SetPixel(screen, x, y, off); // drawing function

            if (n == 3) grid[x][y] = true; // IT'S ALIVE!!!
        }



    }
}

This compiles and runs okay, but the results are totally different from what they should be.

I've snipped irrelavent code:

bool grid[1280][1024]; // hardcoded to my screen res for now

for (int x = 0; x<1280; x++)     //init grid to false
{
    for (int y = 0; y<1024; y++)
    {
        grid[x][y] = false;
    }
}

grid[320][120] = true; // add a simple pattern
grid[320][121] = true;
grid[320][122] = true;
grid[320][123] = true;
grid[320][124] = true;
grid[320][125] = true;
grid[320][126] = true;
grid[320][127] = true;
grid[320][128] = true;

// [snipped]

for (int x = 1; x< (1280 - 1); x++)
{
    for (int y = 1; y< (1024 - 1); y++)
    {
        int n = 0; // neighbours
        if (grid[x-1][y-1]) n++; // top left
        if (grid[x][y-1])   n++; // top middle
        if (grid[x+1][y-1]) n++; // top right

        if (grid[x-1][y])   n++; // left
        if (grid[x+1][y])   n++; // right

        if (grid[x-1][y+1]) n++; // bottom left
        if (grid[x][y+1])   n++; // bottom middle
        if (grid[x+1][y+1]) n++; // bottom right


        if (grid[x][y]) // current cell is on
        {
            SetPixel(screen, x, y, on); // drawing function

            if (n < 2) grid[x][y] = false; // die. :(
            if (n > 3) grid[x][y] = false; // die. :(
            // otherwise (2 or 3), survive. :)

        }
        else // current cell is off
        {
            SetPixel(screen, x, y, off); // drawing function

            if (n == 3) grid[x][y] = true; // IT'S ALIVE!!!
        }



    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

话少情深 2024-10-06 10:59:27

首先,它不起作用,因为你没有分离每个单元格的结果,即,如果 grid[0][0] 死了,那么这将立即反映在 grid[1][0] 的生或死上,这这不是生命游戏的运作方式。其次,它不起作用,因为您似乎没有运行游戏超过一次迭代。

Firstly, it doesn't work because you haven't separated each cell's results, i.e., if grid[0][0] dies, then this will be immediately reflected on grid[1][0]'s life or death, which is not how Game of Life works. Secondly, it doesn't work because you don't appear to have run the game more than one iteration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文