C语言扫雷游戏实现方法详解(包含递归,变色,记录时间等)
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>前言:</li><li>一、扫雷游戏简介</li><li>二、扫雷游戏的设计思路</li><li>三、代码的逐步实现</li><ul class="second_class_ul"><li>1.创建头文件以及源文件</li><li>2.创建主函数main</li><li>3.创建游戏菜单</li><li>4、初始化棋盘</li><li>5、放置雷(随机数的应用)</li><li>6、打印棋盘(设置多彩颜色)</li><li>7、正式开始扫雷游戏</li><ul class="third_class_ul"><li>( 1 ) 规则</li><li>( 2 ) 踩雷时</li><li>( 3 ) 没踩雷时</li><li>( 4 ) 四周没雷时(函数递归)</li><li>( 5 ) 标记和取消标记</li><li>( 6 ) 游戏结束(所有雷已找出)</li></ul><li>8、记录游戏时间</li><ul class="third_class_ul"></ul></ul><li>四、扫雷游戏完整代码</li><ul class="second_class_ul"></ul><li>五、结语</li><ul class="second_class_ul"></ul></ul></div><p class="maodian"></p><h2>前言:</h2><p>本期需要用到大量自定义函数</p>
<p>可以很好的练习函数</p>
<p>代码内均有注释可查看</p>
<p>最终代码放在结尾</p>
<p class="maodian"></p><h2>一、扫雷游戏简介</h2>
<blockquote><p>胜利条件:<br />你需要在不点错雷的情况下尽可能快的将所有的雷都标记出来,<br />如果你点错,就得重新开始,所以扫雷也有一定的运气成分。<br />如何操作:<br />可翻开这一格,也可标记地雷<br />你点出了一个数字,<br />是1,就说明它周围的8的格子里有1个雷,<br />是2就有两个雷,是3就有三个雷···以此类推。<br />但如果你标记错了雷,那就会"boom!"一切重新开始。</p></blockquote>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231211.png" /></p>
<p>OK,知道了游戏规则,那么该如何设计游戏呢?</p>
<p class="maodian"></p><h2>二、扫雷游戏的设计思路</h2>
<p>1.创建游戏菜单。</p>
<p>2.初始化棋盘。</p>
<p>3.随机布置雷。</p>
<p>4.打印出棋盘。</p>
<p>5.操作:排查雷,标记雷,或取消标记雷。</p>
<p>7.循环,直到游戏结束。</p>
<p>8.当然其中的细节处还有很多,带着你一步一步实现。</p>
<p class="maodian"></p><h2>三、代码的逐步实现</h2>
<p class="maodian"></p><h3>1.创建头文件以及源文件</h3>
<p>在我们写代码时,若代码很长很复杂</p>
<p>可以设置多个文件来拆分代码</p>
<blockquote><p>我这里创造了三个文件:<br /><strong>头文件</strong>—game.h<br />用来放函数的声明和一些库函数的头文件<br /><strong>源文件</strong>–game.c<br />用来放函数的定义<br />(游戏代码的主体)<br /><strong>源文件</strong>–main.c<br />用来放主函数</p></blockquote>
<p>这样条理就很清晰也不会乱</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231255.png" /></p>
<p>当然,当声明写在game.h中时,不要忘记在源文件调用game.h</p>
<p>在源文件顶部写上#include“game.h"就行啦</p>
<p>如下</p>
<div class="jb51code"><pre class="brush:cpp;">#include"game.h"
</pre></div>
<p class="maodian"></p><h3>2.创建主函数main</h3>
<blockquote><p>写出创建游戏主体<br />这里我用了一个<strong>do while循环</strong>来判断是否继续游戏</p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">int main()
{
do
{
char x = 0;
menu();
//打印菜单
scanf("%d", &g);
switch (g)
{
case 1:
game();
//进入游戏
break;
case 0:
printf("退出\n");
//退出游戏
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (g);
return 0;
}
</pre></div>
<p class="maodian"></p><h3>3.创建游戏菜单</h3>
<blockquote><p>这没什么好说的,创建一个菜单<br />当然大家也可以设计出更好看的菜单</p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">void menu()//打印菜单
{
printf("**********************\n");
printf("******扫雷******\n");
printf("**********************\n");
printf("***** 1.开 始*****\n");
printf("***** 0.退 出*****\n");
printf("**********************\n");
printf("请输入 1 或 2 \n\n");
printf("请输入:>> ");
}
</pre></div>
<p class="maodian"></p><h3>4、初始化棋盘</h3>
<blockquote><p>首先,扫雷是一个<strong>9*9的矩形</strong><br />那我们可以想到用<strong>二维数组来放置雷</strong><br /><strong>用” * “来表示雷,用” 0 “来表示非雷</strong><br />排查雷的时候就判断周围有几个雷就显示数字几</p></blockquote>
<p><strong>但是有一个问题</strong>,如果我要排查边框的雷的时候,如下:</p>
<p><strong>此时就会出现数组的越界访问的问题</strong></p>
<p>故为了防止越界,我们在设计的时候,需<strong>给数组扩大一圈</strong></p>
<p>雷布置在中间的9 * 9的格子内,但是周围扩大一圈,这样就解决了越界的问题。</p>
<p><strong>所以我们将存放数据的数组创建成11 * 11最合适</strong></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231275.png" /></p>
<p>还有,<strong>我们需要将排查出的雷的数量信息记录存储,并打印出来作为排雷的重要参考信息</strong>。但是雷的个数信息存放在哪里呢?如果存放在布置雷的数组中,这样雷的信息和雷的个数信息就会产生混淆和打印上的困难。</p>
<p>所以,我们需要<strong>设置两个棋盘</strong>,给⼀个棋盘(mine)<strong>存放布置好的雷的信息</strong>,再给另外⼀个棋盘(show)<strong>存放排查出的雷的信息</strong>。这样就互不干扰了,<strong>把雷布置到mine数组,在mine数组中排查雷,排查出雷的数据存放在show数组,并且打印出来。</strong></p>
<p>还有一点,这两个数组<strong>既要存放字符" * ",又要存放数字</strong></p>
<p>故我们可以<strong>设置字符数组来存放信息</strong></p>
<p>OK,现在就可以开始写代码了</p>
<p>在game.h头文件写到:</p>
<div class="jb51code"><pre class="brush:cpp;">#define ROW 9
#define COL 9
//定义9*9小边框
#define ROWS ROW+2
#define COLS COL+2
//定义11*11大边框
void SetBoard(char board, int rows, int cols, char set);
//声明SetBoard函数(初始化棋盘)
</pre></div>
<p>在game.c源文件中写到:</p>
<div class="jb51code"><pre class="brush:cpp;">void SetBoard(char board, int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board = set;
}
}
}
</pre></div>
<p>在main.c源文件中写到:</p>
<div class="jb51code"><pre class="brush:cpp;">char mine;//放置雷
char show;//展示信息
//定义数组大小
SetBoard(mine, ROWS, COLS, '0');//放置雷
SetBoard(show, ROWS, COLS, '*');//展示信息
//初始化数组,传入要初始化的值
</pre></div>
<p class="maodian"></p><h3>5、放置雷(随机数的应用)</h3>
<blockquote><p>这里需要注意,<strong>每一把游戏的雷位置是随机的</strong><br />故我们要在这里<strong>设置一个随机数来随机摆放雷</strong></p></blockquote>
<p>要使用到<strong>rand函数</strong>,头文件为<stdlib.d></p>
<p><strong>rand的具体使用方法详见【C语言】rand函数的应用(随机数的生成)</strong></p>
<p><strong>还可以去c++官网查看:https://legacy.cplusplus.com/</strong></p>
<p>因为是在 <strong>9 * 9</strong> 的框中放置雷</p>
<p>故我们将 <strong>i 和 j 设置成0到8的随机数</strong>就可以了</p>
<p><strong>还要注意,已经放置雷的格子就不能放雷了</strong></p>
<p><strong>创建自定义函数SetMine来放置雷</strong></p>
<p>在game.h头文件写到:</p>
<div class="jb51code"><pre class="brush:cpp;">#define EASY_COUNT 10
//设置雷的个数(10个)
void SetMine(char mine, int row, intcol);
//声明SetMine函数(放置雷)
</pre></div>
<p>在game.c源文件中写到:</p>
<div class="jb51code"><pre class="brush:cpp;">void SetMine(char mine, introw, int col)
{
int count = EASY_COUNT;
//规定放置雷的个数
srand((unsigned int)time(NULL));
//生成随机数
while (count)
{
int i = rand() % row + 1;//生成1-9随机数
int j = rand() % col + 1;//生成1-9随机数
if (mine != '*')//若此处没雷就放置雷
{
mine = '*';
count--;
}
}
}
</pre></div>
<p>在main.c源文件中写到:</p>
<div class="jb51code"><pre class="brush:cpp;">SetMine(mine, ROW, COL);
//放置雷
</pre></div>
<p class="maodian"></p><h3>6、打印棋盘(设置多彩颜色)</h3>
<p>为了使棋盘更加美观,我们还可以增加颜色元素</p>
<p>如下,1,2,3,4的颜色各不相同</p>
<p><strong>创建自定义函数DisplayBoard来打印棋盘</strong></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231244.png" /></p>
<p>这里我们用到了颜色定义函数:</p>
<p><strong>这样可以同时设置多种颜色</strong></p>
<p><strong>对于不同数字展现不同颜色</strong></p>
<div class="jb51code"><pre class="brush:cpp;">void setColor(int color)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void red() { setColor(12); }
void green() { setColor(10); }
void yellow() { setColor(14); }
void blue() { setColor(9); }
void purple() { setColor(13); }
void cyan() { setColor(11); }
void white() { setColor(15); }
void reset() { setColor(7); }
</pre></div>
<p>在game.h头文件写到:</p>
<div class="jb51code"><pre class="brush:cpp;">void DisplayBoard(char board, int row, int col);
//声明DisplayBoard函数(打印棋盘)
</pre></div>
<p>在game.c源文件中写到:</p>
<div class="jb51code"><pre class="brush:cpp;">void setColor(int color)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void red() { setColor(12); }
void green() { setColor(10); }
void yellow() { setColor(14); }
void blue() { setColor(9); }
void purple() { setColor(13); }
void cyan() { setColor(11); }
void white() { setColor(15); }
void reset() { setColor(7); }
void DisplayBoard(char board, int row, int col)
{
printf("-------扫雷--------\n");
printf("");
int i = 1;
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
int j = 1;
for (j = 1; j <= col; j++)
{
switch (board)
{
case '0':
cyan();
break;
case '1':
blue();
break;
case '2':
yellow();
break;
case '3':
purple();
break;
case '4':
case '5':
case '6':
case '7':
case '8':
case 'F':
red();
break;
}
printf("%c ", board);
reset();
}
printf("\n");
}
}
</pre></div>
<p>在main.c源文件中写到:</p>
<div class="jb51code"><pre class="brush:cpp;">DisplayBoard(show, ROW, COL);
//打印棋盘
</pre></div>
<p>打印颜色后的运行结果:</p>
<p><strong>这样就有了多种颜色,使棋盘更加美观</strong></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231258.png" /></p>
<p class="maodian"></p><h3>7、正式开始扫雷游戏</h3>
<p class="maodian"></p><h4>( 1 ) 规则</h4>
<blockquote><p>首先我们要打印出我们的规则,让玩家能更好的操作<br /><strong>可以排查雷,可以标记雷,也可以取消标记雷</strong></p></blockquote>
<p>如下:</p>
<div class="jb51code"><pre class="brush:cpp;">printf("1·排查地雷:请输入要排查的坐标例如‘2 4',第二行第四列\n");
printf("2·标记地雷:输入要标记雷的坐标例如‘2+4',注意要加'+'\n");
printf("3·取消标记:输入要取消标记雷的坐标例如‘2-4',注意要加'-'\n");
printf("请输入:");
</pre></div>
<p class="maodian"></p><h4>( 2 ) 踩雷时</h4>
<blockquote><p>当排雷时,若踩雷了,要退出游戏,<br /><strong>并且打印出雷的位置,至少让玩家知道自己是怎么死的</strong><br /><strong>创建自定义函数DisplayBoard2来打印死亡后的棋盘</strong></p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">if (mine == '*')
{
printf("\a很遗憾,\a您踩雷了,\a游戏结束\a\n");
DisplayBoard2(mine, ROW, COL);
break;
}
</pre></div>
<p><strong>而且,为了更好区分出雷,雷我用了红色颜色</strong></p>
<div class="jb51code"><pre class="brush:cpp;">void DisplayBoard2(char board, int row, int col)
{
printf("-------扫雷--------\n");
printf("");
int i = 1;
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
int j = 1;
for (j = 1; j <= col; j++)
{
if (board == '*')
{
red();
}
printf("%c ", board);
reset();
}
printf("\n");
}
}
</pre></div>
<p>运行结果:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231211.png" /></p>
<p class="maodian"></p><h4>( 3 ) 没踩雷时</h4>
<blockquote><p>若没踩雷,就在格子上统计出周围雷的个数<br />这里我用到了暴力统计<br /><strong>发现一个雷就+1</strong><br /><strong>并且在棋盘上打印出信息</strong><br /><strong>创建自定义函数GetMineCount来获取周围雷的个数</strong></p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">int GetMineCount(char mine, int x, int y)
{
int i, j;
int count = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine == '*')
{
count++;
}
}
}
return count;
}
</pre></div>
<div class="jb51code"><pre class="brush:cpp;">int count = GetMineCount(mine, x, y);
show = count + '0';
</pre></div>
<p class="maodian"></p><h4>( 4 ) 四周没雷时(函数递归)</h4>
<blockquote><p>在我们排雷时,若周围9格均没雷<br />就会返回0,但是此时周围可以确定没雷<br />再一一输入排查就太浪费时间了<br /><strong>而在传统扫雷中,周围没雷就会 ” 炸 “ 开一片</strong></p></blockquote>
<p>如下:</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231273.png" /></p>
<p>为了达到这种效果,我们可以用到函数归递</p>
<p><strong>周围没雷时,自动排除周围,若周围还是0(没雷),就再自动排除周围</strong></p>
<p><strong>以此往复,知道没有可以自动排除的区块</strong></p>
<p><strong>创建自定义函数Chain来炸开周围格子</strong></p>
<p>代码实现:</p>
<div class="jb51code"><pre class="brush:cpp;">if (count == 0)
Chain(mine, show, x, y, ROWS);
//归递ROWS(边长)次
</pre></div>
<div class="jb51code"><pre class="brush:cpp;">void Chain(char mine, char show, int x, int y,int g)
{
int i = x - 1, j = y - 1;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show == '*' && i >= 1 && i <= ROW && j >= 1 && j <= COL)
//1.当show=="*"时再进入防止死循环 2.设置边界防止越界访问
{
show = GetMineCount(mine, i, j) + '0';
if (show == '0' && g)//当g==0时退出
{
g--;
Chain(mine, show, i, j, g);
//设置归递g次,这里设置成棋盘边长
}
}
}
}
}
</pre></div>
<p><strong>最后再打印出操作完后的棋盘</strong></p>
<p><strong>(上面提到了就不赘述了)</strong></p>
<p class="maodian"></p><h4>( 5 ) 标记和取消标记</h4>
<blockquote><p>我这里用 <strong>x + y</strong> 表示标记雷,用 <strong>x - y</strong> 表示取消标记雷<br /><strong>标记就把格子改成红色F,取消就复原就行了</strong><br /><strong>创建自定义函数Sign来标记雷</strong><br /><strong>创建自定义函数Unsign来取消标记雷</strong></p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">else if (s == '+' && x >= 1 && x <= row && y >= 1 && y <= col)
Sign(show, x, y);
else if (s == '-' && x >= 1 && x <= row && y >= 1 && y <= col)
Unsign(show, x, y);
</pre></div>
<div class="jb51code"><pre class="brush:cpp;">void Sign(char show, int x, int y)
{
if (show == '*')
show = 'F';
DisplayBoard(show, ROW, COL);
}
void Unsign(char show, int x, int y)
{
if (show == 'F')
show = '*';
DisplayBoard(show, ROW, COL);
}
</pre></div>
<p class="maodian"></p><h4>( 6 ) 游戏结束(所有雷已找出)</h4>
<blockquote><p><strong>当排查了除了雷的所有格子时,游戏结束(算上自动排查)</strong><br /><strong>创建自定义函数Judge判断是否扫完雷</strong><br /><strong>若扫完雷时,返回0,否则返回1。</strong></p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">flag = Judge(show, ROW, COL);
</pre></div>
<div class="jb51code"><pre class="brush:cpp;">int Judge(char show, int row, int col)
{
int i, j, count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show != '*')
{
count++;//逐个计数
if (count == row * col - EASY_COUNT)
//当所有除了雷的所有格子扫完时
return 0;
}
}
}
return 1;
}
</pre></div>
<div class="jb51code"><pre class="brush:cpp;">if (flag == 0)
{
printf("恭喜你扫雷成功!!!\n");
}
</pre></div>
<p class="maodian"></p><h3>8、记录游戏时间</h3>
<blockquote><p>以上 <strong>7、正式开始扫雷游戏</strong>的所有代码是整个游戏过程<br />我们把他们合到一起(不包含自定义函数)<br /><strong>创建自定义函数FindMine表示整个游戏过程</strong></p></blockquote>
<div class="jb51code"><pre class="brush:cpp;">void FindMine(char mine, char show, int row, int col)
{
char s;
int x, y, flag = 1;
while (flag)
{
printf("1·排查地雷:请输入要排查的坐标例如‘2 4',第二行第四列\n");
printf("2·标记地雷:输入要标记雷的坐标例如‘2+4',注意要加'+'\n");
printf("3·取消标记:输入要取消标记雷的坐标例如‘2-4',注意要加'-'\n");
printf("请输入:");
scanf("%d%c%d", &x, &s, &y);
printf("\n\n");
if (s == ' ' && x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine == '*')
{
printf("\a很遗憾,\a您踩雷了,\a游戏结束\a\n");
DisplayBoard2(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show = count + '0';
if (count == 0)
Chain(mine, show, x, y, ROWS);
DisplayBoard(show, ROW, COL);
flag = Judge(show, ROW, COL);
}
}
else if (s == '+' && x >= 1 && x <= row && y >= 1 && y <= col)
Sign(show, x, y);
else if (s == '-' && x >= 1 && x <= row && y >= 1 && y <= col)
Unsign(show, x, y);
else
{
printf("输入错误,坐标范围X(1-9) Y(1-9),请重新输入\n");
}
}
if (flag == 0)
{
printf("恭喜你扫雷成功!!!\n");
}
}
</pre></div>
<p><strong>若要记录整个游戏时间,就是自定义函数FindMine这整个代码时间</strong></p>
<p>可以在主函数中写:</p>
<div class="jb51code"><pre class="brush:cpp;">clock_t start = clock();//记录游戏时间
{
FindMine(mine, show, ROW, COL);
}
double time = (double)(clock() - start) / CLOCKS_PER_SEC;
printf("您一共用时: %.2f 秒\n\n\n", time);
</pre></div>
<p><strong>这样就可以记下游戏时间了,精度为0.01秒</strong></p>
<p>注意:记时代码要包含头文件<windows.h></p>
<p>运行结果:(虽然这里没成功扫雷)</p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231289.png" /></p>
<p class="maodian"></p><h2>四、扫雷游戏完整代码</h2>
<p>在game.h头文件写到:</p>
<p><strong>(头文件的声明)</strong></p>
<p><strong>(以及用来放函数的声明和一些库函数的头文件)</strong></p>
<div class="jb51code"><pre class="brush:cpp;">#pragma once
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#define ROW 9
#define COL 9
//定义9*9小边框
#define ROWS ROW+2
#define COLS COL+2
//定义11*11大边框
#define EASY_COUNT 10
//设置雷的个数
void SetBoard(char board, int rows, int cols, char set);
//声明SetBoard函数(初始化棋盘)
void SetMine(char mine, int row, intcol);
//声明SetMine(函数(放置雷)
void DisplayBoard(char board, int row, int col);
//声明DisplayBoard函数(打印棋盘)
void DisplayBoard2(char board, int row, int col);
//声明DisplayBoard2函数(打印失败棋盘)
void FindMine(char mine, char show, int row, int col);
//声明函数FindMine(整个游戏过程)
</pre></div>
<p>在game.c源文件中写到:</p>
<p><strong>(游戏主要代码)</strong></p>
<div class="jb51code"><pre class="brush:cpp;">#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void setColor(int color)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
void red() { setColor(12); }
void green() { setColor(10); }
void yellow() { setColor(14); }
void blue() { setColor(9); }
void purple() { setColor(13); }
void cyan() { setColor(11); }
void white() { setColor(15); }
void reset() { setColor(7); }
void menu()//打印菜单
{
printf("**********************\n");
printf("******扫雷******\n");
printf("**********************\n");
printf("***** 1.开 始*****\n");
printf("***** 0.退 出*****\n");
printf("**********************\n");
printf("请输入 1 或 2 \n\n");
printf("请输入:>> ");
}
void SetBoard(char board, int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
{
board = set;
}
}
}
void SetMine(char mine, introw, int col)
{
int count = EASY_COUNT;
//规定放置雷的个数
srand((unsigned int)time(NULL));
//生成随机数
while (count)
{
int i = rand() % row + 1;//生成1-9随机数
int j = rand() % col + 1;//生成1-9随机数
if (mine != '*')//若此处没雷就放置雷
{
mine = '*';
count--;
}
}
}
void DisplayBoard2(char board, int row, int col)
{
printf("-------扫雷--------\n");
printf("");
int i = 1;
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
int j = 1;
for (j = 1; j <= col; j++)
{
if (board == '*')
{
red();
}
printf("%c ", board);
reset();
}
printf("\n");
}
}
void DisplayBoard(char board, int row, int col)
{
printf("-------扫雷--------\n");
printf("");
int i = 1;
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
int j = 1;
for (j = 1; j <= col; j++)
{
switch (board)
{
case '0':
cyan();
break;
case '1':
blue();
break;
case '2':
yellow();
break;
case '3':
purple();
break;
case '4':
case '5':
case '6':
case '7':
case '8':
case 'F':
red();
break;
}
printf("%c ", board);
reset();
}
printf("\n");
}
}
int GetMineCount(char mine, int x, int y)
{
int i, j;
int count = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine == '*')
{
count++;
}
}
}
return count;
}
void Chain(char mine, char show, int x, int y,int g)
{
int i = x - 1, j = y - 1;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show == '*' && i >= 1 && i <= ROW && j >= 1 && j <= COL)
{
show = GetMineCount(mine, i, j) + '0';
if (show == '0' && g)
{
g--;
Chain(mine, show, i, j, g);
}
}
}
}
}
int Judge(char show, int row, int col)
{
int i, j, count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show != '*')
{
count++;
if (count == row * col - EASY_COUNT)
return 0;
}
}
}
return 1;
}
void Sign(char show, int x, int y)
{
if (show == '*')
show = 'F';
DisplayBoard(show, ROW, COL);
}
void Unsign(char show, int x, int y)
{
if (show == 'F')
show = '*';
DisplayBoard(show, ROW, COL);
}
void FindMine(char mine, char show, int row, int col)
{
char s;
int x, y, flag = 1;
while (flag)
{
printf("1·排查地雷:请输入要排查的坐标例如‘2 4',第二行第四列\n");
printf("2·标记地雷:输入要标记雷的坐标例如‘2+4',注意要加'+'\n");
printf("3·取消标记:输入要取消标记雷的坐标例如‘2-4',注意要加'-'\n");
printf("请输入:");
scanf("%d%c%d", &x, &s, &y);
printf("\n\n");
if (s == ' ' && x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine == '*')
{
printf("\a很遗憾,\a您踩雷了,\a游戏结束\a\n");
DisplayBoard2(mine, ROW, COL);
break;
}
else
{
int count = GetMineCount(mine, x, y);
show = count + '0';
if (count == 0)
Chain(mine, show, x, y, ROWS);
DisplayBoard(show, ROW, COL);
flag = Judge(show, ROW, COL);
}
}
else if (s == '+' && x >= 1 && x <= row && y >= 1 && y <= col)
Sign(show, x, y);
else if (s == '-' && x >= 1 && x <= row && y >= 1 && y <= col)
Unsign(show, x, y);
else
{
printf("输入错误,坐标范围X(1-9) Y(1-9),请重新输入\n");
}
}
if (flag == 0)
{
printf("恭喜你扫雷成功!!!\n");
}
}
</pre></div>
<p>在main.c源文件中写到:</p>
<p><strong>(游戏主体框架)</strong></p>
<div class="jb51code"><pre class="brush:cpp;">#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
int g;
void game()
{
char mine;
char show;
//定义数组大小
SetBoard(mine, ROWS, COLS, '0');//放置雷
SetBoard(show, ROWS, COLS, '*');//展示信息
//初始化数组,传入要初始化的值
SetMine(mine, ROW, COL);
//放置雷
DisplayBoard(show, ROW, COL);
//打印棋盘
/*DisplayBoard(mine, ROW, COL); */
clock_t start = clock();//记录游戏时间
{
FindMine(mine, show, ROW, COL);
}
double time = (double)(clock() - start) / CLOCKS_PER_SEC;
printf("您一共用时: %.2f 秒\n\n\n", time);
}
int main()
{
do
{
char x = 0;
menu();
//打印菜单
scanf("%d", &g);
switch (g)
{
case 1:
game();
//进入游戏
break;
case 0:
printf("退出\n");
//退出游戏
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (g);
return 0;
}
</pre></div>
<p class="maodian"></p><h2>五、结语</h2>
<p><strong>OK,本期的扫雷代码到这里就结束了</strong></p>
<p><strong>大家可自己游玩,也可改进我的代码</strong></p>
<p><strong>本期资料来自于:</strong></p>
<p style="text-align:center"><img alt="" src="https://img.jbzj.com/file_images/article/202512/2025121509231254.png" /></p>
<p>https://legacy.cplusplus.com/</p>
<p>到此这篇关于C语言扫雷游戏实现的文章就介绍到这了,更多相关C语言扫雷游戏内容请搜索琼殿技术社区以前的文章或继续浏览下面的相关文章希望大家以后多多支持琼殿技术社区!</p>
<div class="art_xg">
<b>您可能感兴趣的文章:</b><ul><li>基于C语言实现的扫雷游戏代码</li><li>C语言代码实现扫雷游戏</li><li>C语言开发简易版扫雷小游戏</li><li>C语言快速实现扫雷小游戏</li><li>C语言实现简单扫雷小游戏</li><li>C语言实现扫雷小游戏(适合初学者)</li><li>C语言代码实现简单扫雷小游戏</li><li>C语言实现扫雷小游戏详细代码</li><li>C语言实现扫雷小游戏</li><li>C语言实现经典扫雷小游戏完整代码(递归展开 + 选择标记)</li></ul>
</div>
</div>
<!--endmain-->
頁:
[1]