C语言基础练习-扫雷

C语言基础练习-扫雷_第1张图片
扫雷效果图-mac os终端.png

#include 

#include 

void boom(void)

{

    int num;

    printf("请输入雷的数量\n");

    scanf("%d",&num);

    int nums[5][5] = {};

    int records[5][5] = {};

    int x;

    int y;

    for(int i = 0;i < num;i++){

          x = arc4random() % 5;

          y = arc4random() % 5;

          if(nums[x][y] != '@' - '0'){

              nums[x][y] = '@' - '0';

              for(int h = x - 1;h <= x + 1;h++){

                    for(int n = y - 1;n <= y + 1;n++){

                        if(h >= 0 && h < 5 && n >= 0 && n < 5 && nums[h][n] != '@' - '0'){

                              nums[h][n]++;

                        }

                    }

              }

          }else{

              i--;

          }

    }

    int win = 0;
    int count = 0;

    while (1) {
          system("clear");

          for(int x = 0;x < 5; x++){

              for(int y = 0;y < 5;y++){

                    if(records[x][y] != 0 || win == -1){

                        printf("%c(%d,%d)\t",nums[x][y] + '0',x,y);

                    }else{

                        printf("?(%d,%d)\t",x,y);

                    }

              }

              printf("\n");

          }

          if(win == -1){

              printf("loss\n");

              break;

          }

          printf("请输入坐标\n");

          scanf("%d%d",&x,&y);

          if(nums[x][y] == '@' - '0'){

              win = -1;

              continue;

          }else{

              count ++;

          }

          if(count == 25 - num){

              printf("win\t");

              break;

          }

          records[x][y] = 1;

    }

}

int main(void)

{

    boom();

    return 0;

}

坐标范围0~4 不然数组会越界

你可能感兴趣的:(C语言基础练习-扫雷)