C enum数组(需要帮助理解)

时间:2015-01-08 03:33:39

标签: c arrays loops enums

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表并确定该字段是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&amp; BOARD_HEIGHT与7相同。

1 个答案:

答案 0 :(得分:1)

你定义了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;
}