如何在 C 中初始化 3d 数组 - 指针数组数组
How to initialize a 3d array in C - Array of arrays of pointers
我正在编写一个生成下一个可能动作的游戏。我需要生成下一步以执行搜索。但是我不知道如何在 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
不匹配,你应该在声明中删除一个 *
或在 sizeof
中添加一个。
猜猜你想做什么:
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 中做到这一点。
生成板的代码是:
#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
不匹配,你应该在声明中删除一个 *
或在 sizeof
中添加一个。
猜猜你想做什么:
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]
。