c - 如何在 C 中初始化 3d 数组 - 指针数组数组

标签 c arrays pointers malloc

我正在编写一个生成下一个可能 Action 的游戏。我需要生成下一步以执行搜索。但是我不知道如何在 C 中做到这一点。

生成板的代码是:

#include <stdio.h> //prints 
#include <stdbool.h> //bool
#include <stdlib.h>  //malloc 

static const int BOARD_SIZE = 6;
typedef int **BOARD;

void print_board(BOARD b){
    int i,j;
    printf("BOARD array is:\n");
    for (i=0; i<BOARD_SIZE; i++) {
        for (j=0; j<BOARD_SIZE; j++){
            printf("%d ",b[i][j]);
        }
        printf("\n");
    }
}

BOARD set_game(){
//set board
//all the squares starts with 2 
    int i, j;

    BOARD b = malloc(sizeof(int *) * BOARD_SIZE);
    for (i = 0; i < BOARD_SIZE; i++){
        b[i] = malloc(sizeof(int) * BOARD_SIZE);
    }

    for (i=0; i<BOARD_SIZE; i++) {
        for (j=0; j<BOARD_SIZE; j++){
            //position player 0 peons
            if(j == 0){
                b[i][j] = 0;
            }
            //position player 1 peons
            else if(j == BOARD_SIZE-1){
                b[i][j] = 1;
            }else{
                b[i][j] = 2;
            }
        }
    }

    print_board(b);
    return b;
}

// Game
int main(){
// a pointer to an int.
    BOARD p, board;
    p = set_game();

    board = board_status(p);
    return 0;

}

它打印:

BOARD array is:
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1

我现在需要制作一个数组的数组,以生成所有下一个可能的棋盘,例如,当玩家 0 从 b[0][0] 移动到 b[0][1] 时,这是一片叶子的分支机构。

BOARD array is:
2 0 2 2 2 1
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1 
0 2 2 2 2 1

我应该如何分配这个数组? 我需要一组包含所有其他 board_status 的分支,然后我将执行搜索。 我不确定数组的类型,如何声明它?会是一个BOARD的数组吗?

我尝试使用这种方法,我发现了 here但似乎有什么不对劲。它给我一个错误:

incompatible types when assigning to type ‘branches’ from type ‘int’ array[i] = b[i][j];

//generate ALL possible moves
void generatePossibleMoves(int player){
int i, j;
typedef struct
{
    int BOARD[BOARD_SIZE];
} branches;

branches** array = NULL;

void InitBranches( int num_elements )
{
    array = malloc( sizeof( branches ) * num_elements);

    if( !array )
    {
       printf( "error\n" );
       exit( -1 );
    }

    for(i = 0; i < num_elements; i++ )
    {
        for(j = 0; j < BOARD_SIZE; j++ )
        {
           BOARD b = set_game();
           array[i] = b[i][j];
           printf("%d", array[i]);
        }
        printf("\n");
    }
}

InitBranches(4);


}

有人能帮帮我吗?谢谢。

最佳答案

你不应该在函数中有函数,将 InitBranches 移出 generatePossibleMoves。此外,typedef struct 应该在函数之外。

你的array声明和malloc不匹配,你应该在声明中删除一个*或者在中添加一个>大小

猜猜你想做什么:

BOARD* InitBranches( int num_elements )
{
    int i;
    BOARD* array = malloc(num_elements * sizeof *array);

    if( !array )
    {
        printf( "error\n" );
        exit( -1 );
    }

    for(i = 0; i < num_elements; i++ )
    {
       array[i] = set_game();
    }
    return array;
}

void generatePossibleMoves(int player){

    BOARD* array = InitBranches(4);

    //do your moves here
}

这将为您的板创建 4 个分支 array[0] 直到 array[3]

关于c - 如何在 C 中初始化 3d 数组 - 指针数组数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30061814/

相关文章:

c++ - 如何重新定义c++指针函数?

c++ - 空悬挂指针 C++

c++ - 大型原始数字类型之间的算术 : can I save the overflow?

c - OpenCL : Querying max clock frequency of a mobile GPU always returns a lesser value

arrays - node.js,restify - 处理数组类型的参数

javascript - 加载后Timepicker JS更改选项值

pointers - Fortran 解除分配

c - 将硬编码值类型转换为 volatile UINT8 指针 - C 编程

c - 我如何在 C 中为 Windows 创建多线程?

python - python中数组的导数?