C 枚举数组(需要帮助理解)

C enum array (need help understanding)

enum cell_contents {
    INVALID, HOLE, PEG
};

static const enum cell_contents master_board[BOARD_HEIGHT][BOARD_WIDTH] = {
    { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID },
    { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID },
    { PEG, PEG, PEG, PEG, PEG, PEG, PEG },
    { PEG, PEG, PEG, HOLE, PEG, PEG, PEG },
    { PEG, PEG, PEG, PEG, PEG, PEG, PEG },
    { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID },
    { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID }
};

我需要有关如何遍历 master_board table 并确定该字段是 INVALID、PEG 还是 HOLE 的帮助。

我试过使用这个方法,但没有用:

char *characters[] = {"\t", 'o', '.'};

/* display the game board to the screen */
void display_board(enum cell_contents board[][BOARD_HEIGHT])
{
    /* delete this comment and write your own comments and code here */
    for(int i = 0; i < BOARD_HEIGHT; i++) {
        printf("%d.", i+1);

        int length = sizeof(board[i]) / sizeof(board[i][0]);
        printf("Length: %d", length);

        for(int e = 0; e < length; e++){
            if(board[i][e] == INVALID)
                printf("\t%c", characters[0]);
            else if(board[i][e] == PEG)
                printf("\t%c", characters[1]);
            else if(board[i][e] == HOLE)
                printf("\t%c", characters[2]);
        }

        printf("\n");
    }
}

我正在使用另一个函数来调用 display_board:

void play_game(void)
{
    enum cell_contents board[BOARD_WIDTH][BOARD_HEIGHT];
    init_board(board);
    /* delete this comment and write your own comments and code here */

    //while(!is_game_over(board)) {
        display_board(board);
    //}
}

BOARD_WIDTH & BOARD_HEIGHT 在 7 处相同。

你定义了BOARD_WIDTH和BOARD_HEIGHT吗? 此代码对我有用:

#define BOARD_HEIGHT 7
#define BOARD_WIDTH 7
enum cell_contents {
  INVALID, HOLE, PEG
};

static const enum cell_contents master_board[BOARD_HEIGHT][BOARD_WIDTH] = {
  { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID },
  { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID },
  { PEG, PEG, PEG, PEG, PEG, PEG, PEG },
  { PEG, PEG, PEG, HOLE, PEG, PEG, PEG },
  { PEG, PEG, PEG, PEG, PEG, PEG, PEG },
  { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID },
  { INVALID, INVALID, PEG, PEG, PEG, INVALID, INVALID }
};

char *characters[] = {"\t", 'o', '.'};


void display_board(enum cell_contents board[][BOARD_HEIGHT])
{
    /* delete this comment and write your own comments and code here */
   for(int i = 0; i < BOARD_HEIGHT; i++) {
      printf("%d.", i+1);

      int length = sizeof(board[i]) / sizeof(board[i][0]);
      printf("Length: %d", length);

      for(int e = 0; e < length; e++){
          if(board[i][e] == INVALID)
              printf("\t%c", characters[0]);
          else if(board[i][e] == PEG)
              printf("\t%c", characters[1]);
          else if(board[i][e] == HOLE)
              printf("\t%c", characters[2]);
      }

       printf("\n");
  }
}

int main(int argc, char const *argv[])
{
 display_board(master_board);
 return 0;
}