简体   繁体   English

康威的生命游戏问题更新板

[英]Conway's Game of Life issue updating board

So I'm working on an assignmmnet for school which involves writing this update function in assembly to work with C code given to us. 所以我正在为学校开发一个assignmmnet,它涉及在程序集中编写这个更新函数以使用给我们的C代码。 It's just a simple version of Conway's Game of Life which generates a random board configuration and then updates it with new values based on the rules. 它只是Conway生命游戏的一个简单版本,可生成随机电路板配置,然后根据规则使用新值进行更新。 I figured I would write the function in C first so I knew what to do in assembly. 我想我会先用C编写函数,所以我知道在汇编时要做什么。 I'm running into some difficulty which I think stems from not fully understanding how double pointers work in C. Some where in the code given to us the board for the Game of Life is initialized using malloc calls like this: 我遇到了一些困难,我认为这些困难源于没有完全理解双指针在C中是如何工作的。有些地方在给我们的代码中,生命游戏的主板使用如下的malloc调用初始化:

int **board;
board = malloc(sizeof(int *) * height);
for(i = 0; i < height; i++){
    board[i] = malloc(sizeof(int) * width);
}

Each cell is either given a 1 if alive or a 0 if it's dead. 每个单元格如果存活则给定1或者如果它已经死亡则为0。

Now our update function we're supposed to write takes **board as a parameter. 现在我们应该写的更新函数将** board作为参数。 I've only worked with a regular 2 dimensional array but if I'm understanding it correctly than this double pointer should behave similarly to a 2 dimensional array. 我只使用常规二维数组,但如果我正确理解它,那么这个双指针的行为应该类似于二维数组。 Anyway here's the c code I have written: 无论如何这里是我写的c代码:

void update(int **board, int width, int height)
{
int neighborCount = 0; //keeps count of live neighbors
int row = 0;
int col;

int tempBoard[height][width]; //holds updated board temporarily

for(; row < height; row++)
{
    col = 0;

    for(; col < width ; col++)
    {
        //If tests to check if out of bounds.  If not than adds value of cell
        //to neighbor count.  Continues for each possible neighbor
        if ((row - 1) >= 0 && (col - 1) >= 0)
        {
            neighborCount = neighborCount + board[row - 1][col - 1];
        }
        if ((row - 1) >= 0)
        {
            neighborCount = neighborCount + board[row - 1][col];
        }
        if ((row - 1) >= 0 && (col + 1) < width)
        {
            neighborCount = neighborCount + board[row - 1][col + 1];
        }
        if ((col - 1) >= 0)
        {
            neighborCount = neighborCount + board[row][col - 1];
        }
        if ((col + 1) < width)
        {
            neighborCount = neighborCount + board[row][col + 1];
        }
        if ((row + 1) < height && (col - 1) >= 0)
        {
            neighborCount = neighborCount + board[row + 1][col - 1];
        }
        if ((row + 1) < height)
        {
            neighborCount = neighborCount + board[row + 1][col];
        }
        if ((row + 1) < height && (col + 1) < width)
        {
            neighborCount = neighborCount + board[row + 1][col + 1];
        }

        //save new value in temp board based on neighbor count
        if (board[row][col] == 1)
        {
            if (neighborCount == 2 || neighborCount == 3)
            {
                tempBoard[row][col] = 1;
            }
            else
            {
                tempBoard[row][col] = 0;
            }
        }
        else
        {
            if (neighborCount == 3)
            {
                tempBoard[row][col] = 1;
            }
            else
            {
                tempBoard[row][col] = 0;
            }
        }

        neighborCount = 0;
    }
}

//update board with new values from temp board
for(; row < height; row++)
{
    for(; col < width ; col++)
    {
        board[row][col] = tempBoard[row][col];
    }
}

}

My issue is towards the end of the function where I'm trying to update the values in **board with the new values I saved in tempBoard. 我的问题是在函数结束时,我试图用我在tempBoard中保存的新值更新**板中的值。 I'm sure it's my lack of understanding of double pointers but I can't figure out why when the board reprints itself after returning from my function it's identical to it's original state. 我确定这是我对双指针的理解不足,但是我无法弄清楚为什么当我从函数返回后重新打印自己时,它与原始状态相同。 I'm guessing I'm only be updating the local copy of **board and I'm losing my changes when I exit my function but I'm completely lost on how I should go about making sure my changes are seen outside of this function. 我猜我只是更新**板的本地副本,当我退出我的功能时我失去了我的更改,但我完全迷失了我应该如何确保我的更改在此之外被看到功能。

Any help would be greatly appreciated, I've been trying my best to figure it out on my own but I'm just stuck right now on what to do. 我会非常感激任何帮助,我一直在尽我所能来解决这个问题,但我现在只是坚持做什么。 If you need anymore info from me just let me know. 如果您需要我的信息,请告诉我。

row and col are being used in two different loops, and their values aren't being reset in between loops. rowcol正在两个不同的循环中使用,并且它们的值不会在循环之间重置。 So when you go to copy the temp board to the main board, row already equals height and the loop breaks immediately. 因此,当您将临时板复制到主板时, row已经等于height并且循环立即中断。

Just do this: 这样做:

//update board with new values from temp board
for(row = 0; row < height; row++)
{
    for(col = 0; col < width ; col++)
    {
        board[row][col] = tempBoard[row][col];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM