2d数组,打印迷宫,c



我不知道如何在2d数组中打印生成的迷宫。有以下函数:

struct maze {
char **a;               // 2D array supporting maze
unsigned int w;         // width
unsigned int h;         // height
unsigned int cell_size; // number of chars per cell; walls are 1 char
};

和生成它的那个以return maze结尾;

struct maze generate_maze(unsigned int width, unsigned int height,
unsigned int cell_size, int rand_seed) {
int row, col, i;
struct stack stack;
struct cell cell;
struct cell neighbours[4]; // to hold neighbours of a cell
int num_neighbs;
struct maze maze;
maze.w = width;
maze.h = height;
maze.cell_size = cell_size;
maze.a =
(char **)malloc(sizeof(char *) * maze_dimension_to_matrix(&maze, height));
// Initialise RNG
srand(rand_seed);
// Initialise stack
init_stack(&stack, width * height);
return maze;
}

我必须写main函数接受输入来生成迷宫并打印它。到目前为止,我有这个:

int main() {
int height;
int width;
int cellSizer;
int randSeed;
printf("enter Height ");
scanf("%d", &height);
printf("enter Width ");
scanf("%d", &width);
printf("enter Seed ");
scanf("%d", &randSeed);
printf("enter cellSizer ");
scanf("%d", &cellSizer);
printf("%d %d %d %d n", height, width, cellSizer, randSeed);
struct maze test = generate_maze(width, height, cellSizer, randSeed);
int row, col;
for (row = 0; row < height; row++) {
for (col = 0; col < width; col++) {
maze.a[height][width];
}
}
printf("n");
for (row = 0; row < height; row++) {
for (col = 0; col < width; col++) {
printf("%c", maze.a[height][width]);
If(height = width) printf("/n");
}
}
}

我不知道如何进行,我知道要打印2d数组我必须调用一个数组,它是maze.a,但我得到的是error: 'maze' undeclared(first use in the function)我如何修改我的代码,使它能打印迷宫?

谢谢我整理好了:

struct maze maze = generate_maze(width, heigh, cellSizer, randomSeed);
for(row=0; row < maze.h; row++){
for(col=0; col < maze.w; col++){
printf("%c", maze.a[row][col]);
}
printf("n");
}

最新更新