gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:25:31 24 4
gpt4 key购买 nike

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 表并确定字段是否无效、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;
}

关于C 枚举数组(需要帮助理解),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27832481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com