简体   繁体   English

康威的C生活游戏

[英]Conway's Game Of Life In C

I have made a version of conways game of life in C, using a 2d array which should wrap around the sides. 我用2D数组制作了一个用C语言制作的Conways人生游戏版本,该数组应该环绕侧面。 Unfortunately all that happens is the numbers flick back and forth between 1 and 0 with no clear pattern. 不幸的是,所有发生的事情都是数字在1到0之间来回滑动,没有清晰的图案。 Here is the code: #include 这是代码:#include

int main(){
    int const WIDTH = 100;
    int const HEIGHT = 100;
    int const CYCLES = 1000;
    int grid[HEIGHT][WIDTH];
    int temp[HEIGHT][WIDTH];

    int row;
    int col;
    for(row = 0; row < HEIGHT; row++){
        for(col = 0; col < WIDTH; col++){
            grid[row][col] = 0;
        }
    }

    int i;
    int x;
    int y;
    int neighbours;
    for(i = 0; i < CYCLES; i++){
        for(row = 0; row < HEIGHT; row++){
            for(col = 0; col < WIDTH; col++){
                temp[row][col] = 0;
            }
        }
        for(row = 0; row < HEIGHT; row++){
            for(col = 0; col < WIDTH; col++){
                neighbours = 0;
                for(y = -1; y < 2; y++){
                    for(x = -1; x < 2; x++){
                        if(x != 0 && y != 0 && grid[(row + y) % HEIGHT][(col + x) % WIDTH] == 1){
                            neighbours++;
                        }
                    }
                }
                if(grid[row][col] == 1){
                    if(neighbours < 2 || neighbours > 3){
                        temp[row][col] = 0;
                    }else{
                        temp[row][col] = 1;
                    }
                }else if(grid[row][col] == 0){
                    if(neighbours == 3){
                        temp[row][col] = 0;
                    }else{
                        temp[row][col] = 1;
                    }
                }
            }
        }
        for(row = 0; row < HEIGHT; row++){
            for(col = 0; col < WIDTH; col++){
                grid[row][col] = temp[row][col];
                printf("%d", grid[row][col]);
            }
            printf("\n");
        }
        printf("\n");
    }
}

I do notice one problem. 我确实注意到一个问题。

The 4th rule states that a dead cell should become alive again if it has exactly 3 neighbors. 第四条规则规定,如果一个死细胞正好有3个邻居,则该死细胞应再次存活。 Currently, your code does the opposite 目前,您的代码执行相反的操作

else if(grid[row][col] == 0){
    if(neighbours == 3){
        temp[row][col] = 0;
    }else{
        temp[row][col] = 1;
    }
}

This will leave the cell dead if there are exactly 3 and make it alive when that is not the case. 如果恰好有3个,将使该单元死亡,如果不是这种情况,则将其激活。 Switch the 1 and the 0 and it should work. 切换1和0,它应该可以工作。

The way you count your neighbors is false (what about -1%HEIGHT for example???). 您计算邻居的方式是错误的(例如, -1%HEIGHT的水平???)。 I suppose that you want to use a torus (leftmost column connected to rightmost column and the same for lines), so you need to make special cases for borders. 我想您想使用圆环面(最左边的列连接到最右边的列,线也一样),因此您需要对边框进行特殊处理。 A trick is to use modulus like the following. 一个技巧是使用如下模数。

Suppose you have a line of length N , then for each x from 0 to N-1 , compute mid=x+N , get neighbors as left=mid-1 and right=mid+1 , then count neighbors with grid(left%N) , grid(mid%N) , grid(right%N) (add second dimension the same way of course). 假设您有一条长度为N的线,然后对于从0N-1每个x ,计算mid=x+N ,将邻居设为left=mid-1right=mid+1 ,然后用grid(left%N)grid(mid%N)grid(right%N) (以同样的方式添加第二维)。 So you will catch the torus property without any special cases... 因此,您将在没有任何特殊情况的情况下捕获torus属性。

If you want to be sure that it works as expected, I can suggested you to initialize the grid to a well-known GOL pattern (a simple glider for example). 如果您想确保它能按预期运行,建议您将网格初始化为众所周知的GOL模式(例如,一个简单的滑翔机)。

Also verify that the GOL rules are the right ones. 还要验证GOL规则是否正确。

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

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