首页 > 解决方案 > 开发扫雷游戏 - 我的主要功能的问题

问题描述

我目前正在尝试为 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 的循环并提示我输入坐标,然而,它只是显示猜测的数量,然后什么也不做

如果我在代码中有大量问题,我不会感到惊讶,任何帮助将不胜感激,但主要是解释为什么它永远不会到达循环的任何帮助都会有所帮助!

标签: c

解决方案


你有

if (status = STILL_ALIVE) 

which 设置statusSTILL_ALIVEwhich 为零,因此它永远不会进入循环。尝试

if (status == STILL_ALIVE) 

反而。


推荐阅读