繁体   English   中英

康威的生命游戏问题从 python 到 C

[英]Conway's Game of Life problem from python to C

我正在尝试将 python 中的代码转换为 C。但我遇到了一些问题,而且由于我需要将它用于多代,我需要使用类型 int function 并将板返回为一个二维数组。 但我不确定这样做的最佳方式是什么。 下面是 python 和 C 中的代码。此外,我被嵌套循环困住了,但无法弄清楚为什么会出现段错误。

void game_of_life(int board[5][5]) {
    int m = 5;
    int n = 5;
    int moves[8][2] = {{-1, 1},
                       {-1, 0},
                       {-1, -1},
                       {0,  -1},
                       {0,  1},
                       {1,  0},
                       {1,  -1},
                       {1,  1}};
    int dm = 8;
    int grid[m][n];
    memcpy(board, grid, sizeof(grid));
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; i++) {
            int total = 0;
            for (int k = 0; k < dm; k++) {
                int cr = i + moves[k][0];
                int cc = j + moves[k][1];

                if (cr >= 0 && cr < m && cc >= 0 && cc < n) {
                    total += board[cr][cc];
                    printf("%d", total);
                }
            }
            if (total < 2 || total > 3) {
                board[i][j] = 0;
            } else if (total == 3) {
                board[i][j] = 1;
            } else {
                board[i][j] = grid[i][j];
            }
        }
    }

    for(int k =0; k < 5; k++)
    {
        for(int l=0; l<5; l++)
        {
            printf("%d", board[k][l]);


        }
        printf("\n");

    }
}

    m = len(board)
    n = len(board[0])
    moves = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, 0), (1, -1), (1, 1)] # eight possible neighbors
    grid = [row[:] for row in board] # make a copy of the grid
    for i in range(m):
        for j in range(n): # for each cell, check how many neighbors alive
            total = 0 
            
            for di, dj in moves: # check every neighbor
                
                cr = i + di # finding the xy-index of neighbor 
                cc = j + dj
                
                if 0 <= cr < m and 0 <= cc < n: # check if it falls in valid range
                    total += grid[cr][cc] # increment the total neighbor alive for current cell
                
            
            if total < 2 or total > 3: # if the total is less than 2 or greater than three, set the value to 0
                board[i][j] = 0
                
            elif total == 3: # if the total is 3, set the value to 1
                board[i][j] = 1
                print(i,j, board[i][j])
            
            else: # else, the value remains unchanged, copy it from the original grid
                board[i][j] = grid[i][j]
    
    
    return board 
                

使用 gdb,我发现它会崩溃

            if (total < 2 || total > 3) {
                board[i][j] = 0;
            } else if (total == 3) {

因为i是548

看看你的循环。

    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; i++) {
            int total = 0;
            for (int k = 0; k < dm; k++) {
                int cr = i + moves[k][0];
                int cc = j + moves[k][1];

在第二个中,您再次执行i++ 改成j++

如果您要复制+粘贴嵌套循环,请务必注意此类细节,以免出现令人困惑的简单错误。

暂无
暂无

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

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