相信小伙伴在学习c语言的时候想做一些小趣事,下面就是用c语言来实现一个扫雷小游戏,不过是简单的实现扫雷(只是通过数组的方式来实现),适合新手学习。
我用的是vs敲的这个代码,大家可以用vs运行(可能有些小地方不支持其他编译器,不过自己可以修改)。
1.创建一个sao.c的文件,用来放main()函数,也是程序的入口,sao.c文件中的代码如下:
#include<stdio.h>
 #include"sao.h"
 #include<stdlib.h>
 #include<time.h>
void menu()
 {
     printf("********************************\n");
     printf("******1.玩游戏  0.退出游戏******\n");
     printf("********************************\n");
 }
void game()
 {
     //1.布置好雷区
     char mine[ROWS][COLS] = { 0 };
    //2.排查雷的信息
     char show[ROWS][COLS] = { 0 };
    //初始化
     initboard(mine, ROWS, COLS, '0');
     initboard(show, ROWS, COLS, '*');
    //打印出棋盘
     //displayboard(mine, ROW, COL);
     displayboard(show, ROW, COL);
    //布置雷区
     setmine(mine, ROW, COL);
     //displayboard(mine,ROW,COL);
    //扫雷
     findmine(mine, show, ROW, COL);
 }
 void test()
 {
     printf("扫雷游戏\n");
     srand((unsigned int)time(NULL));
     int input = 0;
     do{
         menu();
         printf("请选择:");
         scanf_s("%d", &input);
         switch (input) {
         case 1:
             game();
             break;
         case 0:
             break;
         default:
             printf("输入错误!\n");
             break;
         }
     } while(input);
 }
int main()
 {
     test();
     return 0;
 }
2.第二个文件名为:sao2.c ,这个文件中主要是放一些函数的实现,sao.2c中的代码为:
#include"sao.h"
void initboard(char board[ROWS][COLS], int rows, int cols,char set)
 {
     int i=0;
     int j=0;
     for (i = 0; i < rows; i++)
     {
         for (j = 0; j < cols; j++)
         {
             board[i][j] = set;
         }
     }
 }
void displayboard(char board[ROWS][COLS], int row, int col)
 {
     int i = 0;
     int j = 0;
     printf("   ");
     for (i = 1; i <= col; i++)
     {
         printf(" %d ", i);
     }
     printf("\n");
     for (i = 1; i <=row; i++)
     {
         printf(" %d ", i);
         for (j = 1; j <= col; j++)
         {
             printf(" %c ", board[i][j]);
         }
         printf("\n");
     }
 }
void setmine(char board[ROWS][COLS], int row, int col)
 {
     int x = 0;
     int y = 0;
     int count = EASY_MINE;
     while(count >= 1)
     {
         x = rand() % row + 1;
         y = rand() % col + 1;
         if (board[x][y] == '0')
         {
             board[x][y] = '1';
             count--;
         }
     }
 }
int get_mine_count(char mine[ROWS][COLS], int x, int y)
 {
     return
         mine[x - 1][y] +
         mine[x + 1][y] +
         mine[x][y + 1] +
         mine[x][y - 1] +
         mine[x - 1][y - 1] +
         mine[x - 1][y + 1] +
         mine[x + 1][y - 1] +
         mine[x + 1][y + 1] - 8 * '0';
 }
void  findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
 {
     int x = 0;
     int y = 0;
     while (1)
     {
         printf("请输入你排查的坐标:");
         scanf_s("%d%d", &x, &y);
         if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
         {
             //坐标合法
             if (mine[x][y] == '1')
             {
                 printf("很遗憾!你被炸死了!\n");
                 displayboard(mine,row,col);
                 break;
             }
             else
             {
                 int count = get_mine_count(mine, x, y);
                 show[x][y] = count + '0';
                 displayboard(show, row, col);
             }
         }
         else {
             printf("输入错误!\n");
         }
     }
 }
3.第三个文件为:sao.h的头文件,里面主要是放一些函数的申明和宏,具体代码如下:
#define ROW 9
 #define COL 9
 #define ROWS ROW+2
 #define COLS COL+2
 #include<stdio.h>
 #define EASY_MINE 10
 #include<time.h>
//声明
 void initboard(char board[ROWS][COLS], int rows, int cols,char set);
 //声明
 void displayboard(char board[ROWS][COLS], int row, int col);
 //布置雷区函数的声明
 void setmine(char board[ROWS][COLS], int row, int col);
 //扫雷声明
 void  findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
4.上面代码对大神来说不值一提,对学习c阶段的小伙伴来说可以看看,没有什么难度。










