【问题标题】:2d array, print maze, C二维数组,打印迷宫,C
【发布时间】:2021-05-25 22:55:07
【问题描述】:

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

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

我必须编写主函数来生成迷宫并打印它。到目前为止,我有这个:

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) 如何修复我的代码以便它打印迷宫?

【问题讨论】:

  • 你称迷宫为test,所以你应该使用test.a
  • 请在问题中包含您的所有代码。目前有几个未声明的函数:maze_dimension_to_matrix、init_stack,这意味着我们无法重现您的错误。

标签: arrays c 2d maze


【解决方案1】:

谢谢我整理一下:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-14
    • 2019-03-18
    • 2018-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多