我有一项任务要求我生成一个10乘10的网格,每个网格都是空的或用星号填充。启动代码包括它应该生成的模式,但我只是不确定如何处理它。
在帮助文件中,声明了此typedef
#define BOARD_WIDTH 10
#define BOARD_HEIGHT 10
typedef enum cell{
EMPTY,
BLOCKED,
PLAYER
} Cell;
#define EMPTY_OUTPUT " "
#define BLOCKED_OUTPUT "*"
Cell BOARD_1[BOARD_HEIGHT][BOARD_WIDTH];
Cell BOARD_2[BOARD_HEIGHT][BOARD_WIDTH];
这是c文件中的代码
Cell BOARD_1[BOARD_HEIGHT][BOARD_WIDTH] =
{
{ BLOCKED, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, BLOCKED, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY },
{ EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY, BLOCKED, EMPTY, BLOCKED, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY },
{ EMPTY, EMPTY, BLOCKED, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, EMPTY, BLOCKED }
};
我写了一些代码来生成一个空白网格
void initialiseBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH])
{
int x;
int y;
printf("| |");
for(x=0; x<=9; x++){
printf("%i|", x);
}
printf("\n");
for (y=0; y<=9; y++){
printf("|%i|", y);
for(x=0; x<=9; x++){
printf(" |");
}
printf("\n");
}
}
void loadBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH],
Cell boardToLoad[BOARD_HEIGHT][BOARD_WIDTH])
{
/* TODO */
}
但我不确定如何使用BOARD_1
单元格用星号填充网格。
这个函数initialiseBoard()
也是启动代码的一部分,我还想解释为什么在生成空白网格时需要参数。
答案 0 :(得分:2)
“生成”并不意味着“打印到标准输出”,它意味着“创建内存状态”,换句话说,您的initialiseBoard()
函数应该更改内容它通过的板阵列,使它成为一个初始化的板(而不是一个随机的非初始化板)。
循环中的printf()
应该是board
的单元格的分配:
void initialiseBoard(Cell board[BOARD_HEIGHT][BOARD_WIDTH])
{
for (size_t y = 0; y < BOARD_HEIGHT; ++y)
{
for (size_t x = 0; x < BOARD_WIDTH; ++x)
board[y][x] = EMPTY;
}
}
这会将所有单元格设置为EMPTY
。