简体   繁体   English

如何在C中初始化3D数组-指针数组的数组

[英]How to initialize a 3d array in C - Array of arrays of pointers

I am programming a game which generates the next possible moves. 我正在编写一个可以产生下一个可能动作的游戏。 I need to generate the next moves in order to perform the search. 我需要生成下一步动作才能执行搜索。 However I have no idea about how to do it in C. 但是我不知道如何在C中做到这一点。

The code to generate the board is: 生成板的代码为:

#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;

}

it prints: 它打印:

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

I need now to make an array of arrays, to generate all the next possible boards, for example, when the player 0 moves from b[0][0] to b[0][1], this is one leaf of the branch. 我现在需要制作一个数组数组,以生成所有下一个可能的面板,例如,当玩家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

How should I allocate this array? 我应该如何分配这个数组? I need an array of branches that will have all the other board_status and after that I will perform my search. 我需要一个将具有所有其他board_status的分支数组,然后执行搜索。 I am not sure about the type of the array, how to declare it? 我不确定数组的类型,如何声明? It will be an array of BOARD? 它将是一组BOARD吗?

I tried to use this approach, that I found here but seems like there's something wrong. 我尝试使用在这里找到的这种方法,但似乎出了点问题。 It's giving me an error: 这给了我一个错误:

incompatible types when assigning to type 'branches' from type 'int' array[i] = b[i][j]; 从'int'类型分配给'branches'类型时不兼容的类型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);


}

Can anyone help me, please? 有人可以帮我吗? Thank you. 谢谢。

you should not have a function in a function, move InitBranches out of generatePossibleMoves . 您不应在函数中具有函数,请将InitBranches移出generatePossibleMoves Also the typedef struct should be outside of the function. typedef struct应该在函数之外。

Your declaration of array and the malloc do not match, you should remove a * in the declaration or add one in the sizeof . 您的array声明和malloc不匹配,您应该在声明中删除*或在sizeof添加一个。

just a guess what you want to do: 只是您想做什么:

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
}

this will create 4 branches of your board array[0] till array[3] . 这将创建您的板array[0]array[3] 4个分支。

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

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