Jaqueline Passos
Jaqueline Passos

Reputation: 1389

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.

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.

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. I am not sure about the type of the array, how to declare it? It will be an array of 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];

//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.

Upvotes: 0

Views: 137

Answers (1)

mch
mch

Reputation: 9804

you should not have a function in a function, move InitBranches out of generatePossibleMoves. Also the typedef struct should be outside of the function.

Your declaration of array and the malloc do not match, you should remove a * in the declaration or add one in the 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].

Upvotes: 2

Related Questions