小奔最近学了C语言不少的东西,但是想用学到的东西来搞一个小游戏。
这次就来搞一个扫雷游戏,可以实现的功能就是可自定义 1*1 到 9*9 的扫雷范围,自定义雷的数量,可以自己识别胜利的条件,也可以标记雷的地方,现在我们就来演示一下这个游戏吧
(当前的范围是 9*9 的大小,雷的数量是 5 个,简单实现一下就好)
如果对你有帮助,那就给小奔点一个赞吧,谢谢啦

目录

运行的过程

        • 开始的界面
          • 输入0结束程序
          • 输入1开始游戏
        • 选择标记地雷或者选择踩坐标
          • 输入0标记地雷模式
            • 输入坐标
          • 输入1踩坐标模式
            • 输入坐标
        • 在输入坐标处输入0 0结束游戏
        • 踩到炸弹,出现炸弹位置
          • 输入0结束程序
          • 输入1重新开始游戏
        • 胜利
          • 输入0结束程序
          • 输入1重新开始游戏
        • 代码

开始的界面

跳转到目录


输入0结束程序

跳转到目录


输入1开始游戏

跳转到目录


选择标记地雷或者选择踩坐标


输入0标记地雷模式

跳转到目录


输入坐标

跳转到目录


输入1踩坐标模式

跳转到目录


输入坐标

跳转到目录


在输入坐标处输入0 0结束游戏

跳转到目录


踩到炸弹,出现炸弹位置

跳转到目录
(1表示炸弹的位置,0表示没有炸弹的位置)


输入0结束程序

跳转到目录


输入1重新开始游戏

跳转到目录


胜利

跳转到目录


输入0结束程序

跳转到目录


输入1重新开始游戏

跳转到目录


代码

跳转到目录
我创建了两个.c源文件,一个.h头文件


test.c
跳转到目录

#define _CRT_SECURE_NO_WARNINGS#include"game.h"int main(){int exi = 0;srand((unsigned int)time(NULL));board();printf("请输入是否开始游戏:>");scanf("%d", &exi);do{switch (exi){case 1:{game();printf("是否输入1重新开始游戏:>");scanf("%d", &exi);if (exi == 0){printf("游戏结束");}break;}case 0:{printf("游戏结束");break;}default:{printf("输入错误,请重新输入:>");scanf("%d", &exi);if (exi == 0){printf("游戏结束\n");}break;}}} while (exi);return 0;}

game.h
跳转到目录

#pragma once#include#include#include#define WID 9#define LON 9#define WIDS WID+2#define LONS LON+2#define RAN 5void board();//打印开始的面板void game();//游戏运行的起点void initialization(char mane[WIDS][LONS], char siz, int x, int y);//把数组内框初始化为sizvoid display(char mane[WIDS][LONS], int x, int y);//打印数组内框的字符void random(char mane[WIDS][LONS], int count);//在数组中随机赋予count个炸弹int look(char mane[WIDS][LONS], int x, int y);//计算mane数组x,y位置周围有多少炸弹void judge(char mane[WIDS][LONS], char show[WIDS][LONS],char include[WIDS][LONS]);//判断输入是否获得胜利void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS], int X, int Y);//判断周围没有雷,会向外继续推,直到出现雷void change(char show[WIDS][LONS], int x, int y, char siz);//改变数组show位置(x,y)为字符sizvoid jishu();//统计选择了几次的位置,包括类推的位置,实现一点出现一大片的功能

game扫雷.c
跳转到目录

#define _CRT_SECURE_NO_WARNINGS#include"game.h"static int a = 0;void board(){printf("****************************\n");printf("****************************\n");printf("********* 1.play**********\n");printf("********* 0.exit**********\n");printf("****************************\n");printf("****************************\n");}//数组初始化void initialization(char mane[WIDS][LONS], char siz, int x, int y){int i = 0;for (i = 0; i <= x+1; i++){int j = 0;for (j = 0; j <= y+1; j++){mane[i][j] = siz;}}}//打印第一个面板void display(char mane[WIDS][LONS], int x,int y){int i = 0;int j = 0;printf("-----------扫雷-----------\n");printf("0 | ");for (j = 1; j <= y; j++){printf("%d ",j);}printf("\n");printf("- - -");for (j = 1; j <= y; j++){printf(" -");}for (i = 1; i <= x; i++){printf("\n");printf("%d | ",i);for (j = 1; j <= y; j++){printf("%c ", mane[i][j]);}}printf("\n-----------扫雷-----------\n");}void random(char mane[WIDS][LONS],int count){int x = 0;int y = 0;while (count){x = rand() % WID + 1;y = rand() % LON + 1;if (mane[x][y] == '0'){mane[x][y] = '1';count--;}}}int look(char mane[WIDS][LONS],int x,int y){return mane[x][y + 1] +mane[x][y - 1] +mane[x - 1][y + 1] +mane[x - 1][y - 1] +mane[x + 1][y + 1] +mane[x + 1][y - 1] +mane[x - 1][y] +mane[x + 1][y]-8*'0';}void jishu(){a++;}void xunhuan(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS],int X,int Y){if (include[X][Y] != '1'){int count = 0;count = look(mane, X, Y);show[X][Y] = count + '0';include[X][Y] = '1';jishu();if (count == 0){xunhuan(mane, show, include, X + 1, Y + 1);xunhuan(mane, show, include, X - 1, Y - 1);xunhuan(mane, show, include, X + 1, Y);xunhuan(mane, show, include, X - 1, Y);xunhuan(mane, show, include, X, Y + 1);xunhuan(mane, show, include, X, Y - 1);xunhuan(mane, show, include, X + 1, Y - 1);xunhuan(mane, show, include, X - 1, Y + 1);}}}void change(char show[WIDS][LONS], int x, int y,char siz){show[x][y] = siz;}void judge(char mane[WIDS][LONS], char show[WIDS][LONS], char include[WIDS][LONS]){int X = 0;int Y = 0;display(show, WID, LON);do{int num = a;if (num == WID * LON - RAN){printf("恭喜你获得胜利!\n\n");display(mane, WID, LON);break;}printf("想要标记地雷就输入0,想要选择就输入1):>");int choose = 0;scanf("%d", &choose);printf("\n");if (choose==1){printf("输入0 0结束游戏\n");printf("请输入你选择的坐标:>");scanf("%d%d", &X, &Y);if (X == 0 && Y == 0){printf("\n结束此次游戏\n\n");break;}if (X >= 1 && X <= 9 && Y >= 1 && Y <= 9){if (mane[X][Y] == '1'){printf("\n你吃到炸弹啦,死翘翘了\n\n");display(mane, WID, LON);break;}else{xunhuan(mane, show, include, X, Y);display(show, WID, LON);//display(mane, WID, LON);}}else{printf("\n你输的超过范围啦,");}}else{printf("\n输入0 0结束游戏\n");printf("请输入你选择的坐标:>");scanf("%d%d", &X, &Y);if (X == 0 && Y == 0){printf("\n结束此次游戏\n\n");break;}change(show,X,Y,'F');display(show, WID, LON);}} while (1);}void chu(char mane[WIDS][LONS], char siz,int x, int y){int i = 0;for (i = 1; i <= x ; i++){int j = 0;for (j = 1; j <= y ; j++){mane[i][j] = siz;}}}void game(){char mane[WIDS][LONS];char show[WIDS][LONS];char include[WIDS][LONS];initialization(mane, '0', WID, LON);initialization(show, '*', WID, LON);initialization(include, '1', WID, LON);chu(include, '0', WID, LON);random(mane,RAN);//display(mane, WID, LON);//display(show, WID, LON);judge(mane,show,include);}

跳转到目录

我写的这个小游戏还很粗糙,不过才开始学,进步空间还是很大的,我们继续加油,未来可期

代码就上传到gitee了,看到这里了,不点一个赞再走嘛,嘿嘿