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

时间:2017-10-20 05:50:43

标签: php jquery d q

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

我尝试让数组正确传输的尝试并没有奏效,因为当电路板只有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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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,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 failed\n");
                return 0;
            }
        }
    }
    printf("countLiveNeighbor test passed\n");
    step = 0;
    while(!aliveStable_gold(game_board_gold, row, col)){
        if(step > 10000){
            printf("updateBoard test passed\n");
            printf("aliveStable test not finished due to too many steps taken\n");
            return 0;
        }
        if(aliveStable(game_board_stu,row,col)){
            printf("aliveStable test failed\n");
            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 failed\n");
                    return 0;
                }
            }
        }
    }
    if(!aliveStable(game_board_stu,row,col)){
        printf("aliveStable test failed\n");
        return 0;
    }
    printf("updateBoard test passed\n");
    printf("aliveStable test passed\n");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

我认为你需要初始化数组变量并替换&#34; ==&#34;到&#34; =&#34;正如已经提到的那样。 这是我尝试编辑的代码。您可以根据需要进一步编辑。

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]);

    }
}