繁体   English   中英

二维阵列,打印迷宫,C

[英]2d array, print maze, C

我不知道如何在二维数组中打印生成的迷宫。 有以下功能:

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

我必须编写主要的 function 输入以生成迷宫并打印它。 到目前为止,我有这个:

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");
    }
  }
}

我不知道如何进行,我知道要打印二维数组我必须调用一个数组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");
        }

暂无
暂无

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

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