0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. I was confused about the pointers and the typedef. Do you know if I haven't the correct numbers of leafs and want to limit the depth of the branch by the execution time, how can I initialize an array of Boards without having its size? Thank you so much!
you can realloc array every time you want to add a branch (it's better to double the size if it is too small). if you want to add a 5th branch in the example you can do array = realloc(5* sizeof *array); array[4] = set_game();.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.