我目前正在尝试为C中的项目编写扫雷游戏; 我需要执行非常具体的功能并要满足条件 我对c还是很陌生,很难确定为什么我认为应该起作用,而不是;
尝试调用启动游戏的函数时发生我的问题; 它会打印初始的printf,但永远不会到达游戏的循环;
我需要在我的functions.c文件中实现这些功能:
void display(int grid[][DIM], int size);
void update_status(int row, int col, int scores[][4], int known[][4]);
void check_found(int row, int col, struct locations bombs[], int size, bool* found);
char get_user_char();
void update_known_info(int row, int col, int bomb_info[][DIM], int known[][DIM]);
void start_game(struct locations * bombs, int bomb_location_info[][DIM], int size_of_grid, int players_info[][DIM], int no_of_bombs){}
我还需要使用此结构和其他定义:
#define DIM 4
#define UNKNOWN -1
#define BOMB -2
struct locations {
int x;
int y;
bool found;
};
就目前而言;我的主像是这样:
int _of_bombs = 2;
struct locations * bombs = (struct locations *) malloc(sizeof(struct locations) * 2);
int bomb_location_info[4][4] = { 1,1,1,0,
1,0,2,1,
1,1,2,0,
0,0,1,1 };
int known_location_info[4][4] = { UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN,
UNKNOWN,UNKNOWN,UNKNOWN,UNKNOWN };
bombs[0].x = 1;
bombs[0].y = 1;
bombs[0].found = false;
bombs[1].x = 2;
bombs[1].y = 3;
bombs[1].found = false;
int size = 4;
start_game(bombs, bomb_location_info, size, known_location_info, _of_bombs);
return 0;
free(bombs);
除了我尝试实现但未能实现的start_game之外,所有这些内容都是为了符合规范而编写的。
按现状显示,这是我对Functions.c的代码:
void display(int known_location_info[][DIM], int size) {
int f = 0;
int g = 0;
for (f = 0; f < size; f++) {
g = 0;
for (g = 0; g < size; g++) {
if (g < size) {
printf("%d", known_location_info[g][f] );
}
}
printf("\n");
}
}
void update_known_info(int row, int col, int bomb_info[][DIM], int known[][DIM]) {
known[row][col] = bomb_info[row][col];
}
void check_found(int row, int col, struct locations bombs[], int size, bool* found) {
for (int i = 0; i < 2; i++) {
if (bombs[i].x == row) {
if (bombs[i].y == row) {
*found = true;
}
}
}
}
void get_user_char(int* a ) {
scanf("%d", a);
}
void start_game(struct locations * bombs, int bomb_location_info[][DIM], int size_of_grid, int players_info[][DIM], int no_of_bombs) {
enum game_status { STILL_ALIVE, GAME_OVER };
enum game_status status = STILL_ALIVE;
printf("Number of moves = 5");
for (int i = 0; i < 6; i++) {
if (status = STILL_ALIVE) {
int chosenX = 0;
int chosenY = 0;
printf("Enter X coordinate:");
get_user_char(&chosenX);
printf("Enter Y coordinate:");
get_user_char(&chosenY);
bool found = false;
check_found(chosenX, chosenY, bombs , size_of_grid, found);
if (found = true) {
status = GAME_OVER;
}
else {
update_known_info(chosenX, chosenY, bomb_location_info, players_info);
}
}
}
}
我希望它进入game_start循环并提示我输入坐标,但是,它只是显示猜测数目,然后什么也不做
如果我在代码中有很多问题,我将不会感到惊讶,我们将不胜感激,但主要是任何有助于解释为什么它永远不会到达循环的帮助都会有用!
答案 0 :(得分:2)
你有
if (status = STILL_ALIVE)
将status
设置为STILL_ALIVE
,该值为零,因此它永远不会进入循环。
尝试
if (status == STILL_ALIVE)
相反。