生命游戏 1d 阵列更新板时出错



嗨,我在尝试让这个函数工作时遇到了很多麻烦,我试图在函数内创建一个数组,该数组接受主指针数组的值并在更新指针数组之前编辑值通过将临时数组输入回指针。

试图让数组正确传输的尝试没有奏效,因为当板子应该只有 1 或 0 时,我得到了非常奇怪的大或小的值。

放置 print 语句是为了检查数组是否已正确输入。我相信我的其余代码应该是正确的。

void updateBoard(int* board, int boardRowSize, int boardColSize) {
    int i,j,sum,k;
    k = boardRowSize * boardColSize;
    int array[k];
    for(i = 0; i < boardRowSize; i++){
        for(j = 0; j < boardColSize; j++){
            array[(i * boardColSize) + j] = board[(i * boardColSize) + j];
            printf("%d",array[i * boardColSize + j]);
    }
    printf("n");
}
    for(i = 0; i < boardRowSize; i++){
            for(j = 0; j < boardColSize; j++){ 
                sum = 0;
                sum = countLiveNeighbor(board,boardRowSize,boardColSize,i,j);
                if(board[i * boardColSize + j] = 1){
                     if(sum == 3 || sum == 2){
                        array[(i * boardColSize) + j ] = 1;
                    }else{
                        array[(i * boardColSize) + j ] = 0;                 
                    }
                }else if (board[(i * boardColSize) + j] = 0){
                    if(sum == 3){
                        array[(i * boardColSize) + j] = 1;
                    }else{
                        array[(i * boardColSize) + j] = 0;                  
                    }           
                }           
            }
    }
    for(i = 0; i < boardRowSize; i++){
        for(j = 0; j < boardColSize; j++){
            board[i * boardColSize + j] = array[i * boardColSize + j];
            printf("%d",board[i * boardColSize + j]);
    }
}
}

应该假设函数 countLiveNeighbor() 按预期工作,计算正确数量的活动邻居。

这是数组的样子

00000000000000000000000000000000000
00000000000000000000000000000000000
00000000000000000000000000000000000
00000000000000000000001000000000000
00000000000000000000010100000000000
00000000000000000000111110000000000
00000000000000000000000000000000000
00000000000000000000100010000000000
00000000000000000001100011000000000
00000000000000000010100010100000000
00000000000000000001100011000000000
00000000000000000000100010000000000
00000000000000000000000000000000000
00000000000000000000111110000000000
00000000000000000000010100000000000
00000000000000000000001000000000000
00000000000000000000000000000000000
00000000000000000000000000000000000

CountLiveNeighbor 函数

int countLiveNeighbor(int* board, int boardRowSize, int boardColSize, int row, int col){
    int count = 0;  
    if((row - 1) >= 0 && board[ (row -1) * boardColSize + (col)] == 1){
        count++;    
    }if((row + 1) < boardRowSize  && board[ (row + 1) * boardColSize + (col)] == 1){
        count++;    
    }if((col - 1) >= 0 && board[ (row) * boardColSize + (col - 1)] == 1){
        count++;    
    }if((col - 1) < boardColSize && board[ (row) * boardColSize + (col + 1)] == 1){
        count++;    
    }if((row - 1) >= 0  && (col - 1) >= 0 && board[  (row -1) * boardColSize + (col - 1)] == 1){
        count++;    
    }if((row + 1) < boardRowSize  && (col - 1) >= 0 &&  board[(row + 1) * boardColSize + (col - 1)] == 1){
        count++;    
    }if((row + 1) < boardRowSize  && (col + 1) < boardColSize &&  board[(row + 1) * boardColSize + (col + 1)] == 1){
        count++;    
    }if((row - 1) >= 0  && (col + 1) < boardColSize &&  board[(row - 1) * boardColSize + (col + 1)] == 1){
        count++;    
    }
    return count;

}

提供的是用于测试此程序的函数:

#include <stdlib.h>
#include <stdio.h>
#include "updateBoard.h"
#include "updateBoardGold.h"
int main(){
    int row,col,step;
    step = 0;
    row = 18;
    col = 35;
    int i,j;
    int game_board_stu[18*35] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0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,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; 
    int stuCount,goldCount;
    for(i=0;i<row;i++){
        for(j=0;j<col;j++){
            stuCount = countLiveNeighbor(game_board_stu, row, col, i, j);
            goldCount = countLiveNeighbor_gold(game_board_gold,row,col,i,j);
            if(stuCount != goldCount){
                printf("countLiveNeighbor test failedn");
                return 0;
            }
        }
    }
    printf("countLiveNeighbor test passedn");
    step = 0;
    while(!aliveStable_gold(game_board_gold, row, col)){
        if(step > 10000){
            printf("updateBoard test passedn");
            printf("aliveStable test not finished due to too many steps takenn");
            return 0;
        }
        if(aliveStable(game_board_stu,row,col)){
            printf("aliveStable test failedn");
            return 0;
        }
        updateBoard_gold(game_board_gold,row,col);
        updateBoard(game_board_stu,row,col);
        for(i=0;i<row;i++){
            for(j=0;j<col;j++){
                if(game_board_stu[i*col+j]!= game_board_gold[i*col+j]){
                    printf("updateBoard test failedn");
                    return 0;
                }
            }
        }
    }
    if(!aliveStable(game_board_stu,row,col)){
        printf("aliveStable test failedn");
        return 0;
    }
    printf("updateBoard test passedn");
    printf("aliveStable test passedn");
    return 0;
}

我认为您需要初始化数组变量并将"=="替换为"=",因为它已经提到过。这是我尝试编辑的代码。您可以根据需要进一步编辑它。

void updateBoard(int* board, int boardRowSize, int boardColSize) {
int i, j, sum, k;
k = boardRowSize * boardColSize;
int array[20];
memset(array, 0, k*sizeof(int));
for (i = 0; i < boardRowSize; i++) {
    for (j = 0; j < boardColSize; j++) {
        array[(i * boardColSize) + j] == board[(i * boardColSize) + j];
        printf("%d", array[i * boardColSize + j]);
    }
    printf("n");
}
for (i = 0; i < boardRowSize; i++) {
    for (j = 0; j < boardColSize; j++) {
        sum = 0;
        sum = countLiveNeighbor(board, boardRowSize, boardColSize, i, j);
        if (board[i * boardColSize + j] == 1) {
            if (sum == 3 || sum == 2) {
                array[(i * boardColSize) + j] = 1;
            }
            else {
                array[(i * boardColSize) + j] = 0;
            }
        }
        else if (board[(i * boardColSize) + j] == 0) {
            if (sum == 3) {
                array[(i * boardColSize) + j] = 1;
            }
            else {
                array[(i * boardColSize) + j] = 0;
            }
        }
    }
}
for (i = 0; i < boardRowSize; i++) {
    for (j = 0; j < boardColSize; j++) {
        board[i * boardColSize + j] = array[i * boardColSize + j];
        printf("%d", board[i * boardColSize + j]);
    }
}

相关内容

  • 没有找到相关文章

最新更新