对于上面的图,相信大家不会陌生,它是于1992年发行的一款风靡全球的益智小游戏。游戏目标是根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。网上还有八条秘诀,简称雷诀八条。
第一条:基本定式不要忘,现场推理真够呛。
第二条:鼠标点击不要快,稳定节奏把空开。
第三条:顺手标雷不要惯,积累下来记录悬。
第四条:无从下手不要愣,就近猜雷把心横。
第五条:遇到猜雷不要怕,爆了脸上不留疤。
第六条:猜雷猜错不要悔,哭天抢地也白费。
第七条:碰上好局不要慌,紧盯局部慢扩张。
第八条:痛失好局不要恨,既然有缘定有份。
(秘诀源自百度文库)
接下来博主将带大家用C语言设计扫雷游戏。
设计游戏首先要确立游戏规则, 不以规矩不成方圆,确立游戏规则可以让我们的思路更加清晰。
扫雷是在一个矩阵中进行的,这次就设计一个9*9的简易版本。
玩家通过选择0(退出游戏)或1(进行游戏)来决定是否游戏。
然后选择坐标来排雷,如果选择的坐标是雷,输出很遗憾你被炸死了,并结束游戏。如果选择的坐标不是雷,统计该坐标周围雷的个数并打印出来,若该坐标周围雷的个数为0,就展开直到有雷为止。当把雷都排查完时,输出恭喜你,扫雷成功。
玩家在输入坐标前可以选择是否进行标记,若选择需要标记,接下来输入要标记的坐标,如果该坐标已被标记则取消该标记。
void menu()
{
printf("****************************\n");
printf("******** 1、Play *******\n");
printf("******** 0、Exit *******\n");
printf("****************************\n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();//打印菜单
printf("请选择:> ");
scanf("%d", &input);
while (getchar() != '\n');//清除缓存区
switch (input)//选择是否进行游戏
{
case 1:
system("cls");//清空屏幕
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (input);
}
说明:
void game()
{
char mine[ROWS][COLS] = {
0 };//存放雷的数组
char show[ROWS][COLS] = {
0 };//展示数组
//初始化两个数组
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印扫雷的界面
PrintBoard(show, ROW, COL);
//布置雷的位置
SetMine(mine, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
说明:
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0, j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
Board[i][j] = set;
}
}
}
用set接受初始化的类型,这里将mine数组初始化为‘0’,show数组初始化为‘*’。
void PrintBoard(char Board[ROWS][COLS], int row, int col)
{
printf("----------扫雷游戏----------\n");
int i = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", Board[i][j]);
}
printf("\n");
}
printf("----------扫雷游戏----------\n");
}
void SetMine(char Board[ROWS][COLS], int row, int col)
{
int count = NumMine;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Board[x][y] == '0')
{
Board[x][y] = '1';//雷为'1'
count--;
}
}
}
有10个雷,用随机数生成一个坐标来存放雷的位置,每存放一个雷,雷数就减一,直到雷数为0。
void FindMine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col)
{
int win = 0;//排雷区域'*'和'&'的总数
int x = 0, y = 0;
int a = 0, b = 0;
while (1)
{
int sign = 0;
printf("请输入想要排雷的坐标:>");
scanf("%d %d", &x, &y);
while (getchar() != '\n');
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
PrintBoard(Mine, row, col);
break;
}
else
{
//计算该坐标周围有多少雷
Count_Spread(Mine, Show, x, y);
system("cls");
//递归展开
PrintBoard(Show, ROW, COL);
win = To_Win(Show, ROW, COL);
if (win == NumMine)
{
printf("恭喜你,扫雷成功!\n");
PrintBoard(Mine, row, col);
break;
}
}
}
else
{
printf("坐标不合法,请重新输入\n");
continue;
}
do
{
printf("标记与否(1表示标记,0表示不标记):>");
scanf("%d", &sign);
printf("\n");
switch (sign)
{
case 1:
while (1)
{
printf("请输入想要标记的坐标:>");
scanf("%d %d", &a, &b);
while (getchar() != '\n');
if (a >= 1 && a <= row && b >= 1 && b <= col)
{
if (Show[a][b] == '*')
{
Show[a][b] = '&';
system("cls");
PrintBoard(Show, ROW, COL);
break;
}
if (Show[a][b] == '&')
{
Show[a][b] = '*';
system("cls");
PrintBoard(Show, ROW, COL);
break;
}
}
else
{
printf("坐标不合法,请重新输入\n");
}
}
break;
case 0:
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (sign);
}
}
static int RoundMine(char Mine[ROWS][COLS], int x, int y)
{
return Mine[x - 1][y - 1] +
Mine[x - 1][y] +
Mine[x - 1][y + 1] +
Mine[x][y + 1] +
Mine[x + 1][y + 1] +
Mine[x + 1][y] +
Mine[x + 1][y - 1] +
Mine[x][y - 1] - 8 * '0';
}
void Count_Spread(char Mine[ROWS][COLS], char Show[ROWS][COLS], int x, int y)
{
int count = RoundMine(Mine, x, y);
if (count != 0)
{
Show[x][y] = count + '0';
}
else
{
Show[x][y] = ' ';
if ((x - 1) > 0 && (y - 1) > 0 && Show[x - 1][y - 1] == '*')
Count_Spread(Mine, Show, x - 1, y - 1);
if ((x - 1) > 0 && (y + 0) > 0 && Show[x - 1][y + 0] == '*')
Count_Spread(Mine, Show, x - 1, y + 0);
if ((x - 1) > 0 && (y + 1) <= COL && Show[x - 1][y + 1] == '*')
Count_Spread(Mine, Show, x - 1, y + 1);
if ((x + 0) > 0 && (y + 1) <= COL && Show[x + 0][y + 1] == '*')
Count_Spread(Mine, Show, x + 0, y + 1);
if ((x + 1) <= ROW && (y + 1) <= COL && Show[x + 1][y + 1] == '*')
Count_Spread(Mine, Show, x + 1, y + 1);
if ((x + 1) <= ROW && (y - 0) > 0 && Show[x + 1][y - 0] == '*')
Count_Spread(Mine, Show, x + 1, y - 0);
if ((x + 1) <= ROW && (y - 1) > 0 && Show[x + 1][y - 1] == '*')
Count_Spread(Mine, Show, x + 1, y - 1);
if ((x - 0) > 0 && (y - 1) > 0 && Show[x - 0][y - 1] == '*')
Count_Spread(Mine, Show, x - 0, y - 1);
}
}
说明:
do
{
printf("标记与否(1表示标记,0表示不标记):>");
scanf("%d", &sign);
while (getchar() != '\n');//清除缓存区
printf("\n");
switch (sign)
{
case 1:
while (1)
{
printf("请输入想要标记的坐标:>");
scanf("%d %d", &a, &b);
while (getchar() != '\n');
if (a >= 1 && a <= row && b >= 1 && b <= col)
{
if (Show[a][b] == '*')
{
Show[a][b] = '&';
system("cls");
PrintBoard(Show, ROW, COL);
break;
}
if (Show[a][b] == '&')
{
Show[a][b] = '*';
system("cls");
PrintBoard(Show, ROW, COL);
break;
}
if ((Show[a][b] != '*') && (Show[a][b] != '&'))
{
printf("坐标不合法,请重新输入\n");
}
}
else
{
printf("坐标不合法,请重新输入\n");
}
}
break;
case 0:
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (sign);
说明:
if (Mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
PrintBoard(Mine, row, col);
break;
}
int To_Win(char Show[ROWS][COLS], int row, int col)
{
int i = 0, j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (Show[i][j] == '*' || Show[i][j] == '&')
{
count++;
}
}
}
return count;
}
if (win == NumMine)
{
printf("恭喜你,扫雷成功!\n");
PrintBoard(Mine, row, col);
break;
}
说明:
代码分为test.c game.c game.h这三个文件
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("****************************\n");
printf("******** 1、Play *******\n");
printf("******** 0、Exit *******\n");
printf("****************************\n");
}
void game()
{
char mine[ROWS][COLS] = {
0 };//存放雷的数组
char show[ROWS][COLS] = {
0 };//扫雷的数组
//初始化两个数组
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印扫雷的界面
PrintBoard(show, ROW, COL);
//布置雷的位置
SetMine(mine, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:> ");
scanf("%d", &input);
while (getchar() != '\n');//清除缓存区
switch (input)
{
case 1:
system("cls");
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (input);
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0, j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
Board[i][j] = set;
}
}
}
void PrintBoard(char Board[ROWS][COLS], int row, int col)
{
printf("----------扫雷游戏----------\n");
int i = 0;
for (i = 0; i <= col; i++)
{
if (i == 0)
{
printf(" ");
}
else
{
printf("%d ", i);
}
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", Board[i][j]);
}
printf("\n");
}
printf("----------扫雷游戏----------\n");
}
void SetMine(char Board[ROWS][COLS], int row, int col)
{
int count = NumMine;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Board[x][y] == '0')
{
Board[x][y] = '1';//雷为'1'
count--;
}
}
}
//x,y坐标周围有的雷的个数
static int RoundMine(char Mine[ROWS][COLS], int x, int y)
{
return Mine[x - 1][y - 1] +
Mine[x - 1][y] +
Mine[x - 1][y + 1] +
Mine[x][y + 1] +
Mine[x + 1][y + 1] +
Mine[x + 1][y] +
Mine[x + 1][y - 1] +
Mine[x][y - 1] - 8 * '0';
}
void Count_Spread(char Mine[ROWS][COLS], char Show[ROWS][COLS], int x, int y)
{
int count = RoundMine(Mine, x, y);
if (count != 0)
{
Show[x][y] = count + '0';
}
else
{
Show[x][y] = ' ';
if ((x - 1) > 0 && (y - 1) > 0 && Show[x - 1][y - 1] == '*')
Count_Spread(Mine, Show, x - 1, y - 1);
if ((x - 1) > 0 && (y + 0) > 0 && Show[x - 1][y + 0] == '*')
Count_Spread(Mine, Show, x - 1, y + 0);
if ((x - 1) > 0 && (y + 1) <= COL && Show[x - 1][y + 1] == '*')
Count_Spread(Mine, Show, x - 1, y + 1);
if ((x + 0) > 0 && (y + 1) <= COL && Show[x + 0][y + 1] == '*')
Count_Spread(Mine, Show, x + 0, y + 1);
if ((x + 1) <= ROW && (y + 1) <= COL && Show[x + 1][y + 1] == '*')
Count_Spread(Mine, Show, x + 1, y + 1);
if ((x + 1) <= ROW && (y - 0) > 0 && Show[x + 1][y - 0] == '*')
Count_Spread(Mine, Show, x + 1, y - 0);
if ((x + 1) <= ROW && (y - 1) > 0 && Show[x + 1][y - 1] == '*')
Count_Spread(Mine, Show, x + 1, y - 1);
if ((x - 0) > 0 && (y - 1) > 0 && Show[x - 0][y - 1] == '*')
Count_Spread(Mine, Show, x - 0, y - 1);
}
}
//通过计算Show[ROWS][COLS]中有多少'*'或'&',该个数等于雷的个数时,获得胜利
int To_Win(char Show[ROWS][COLS], int row, int col)
{
int i = 0, j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (Show[i][j] == '*' || Show[i][j] == '&')
{
count++;
}
}
}
return count;
}
void FindMine(char Mine[ROWS][COLS], char Show[ROWS][COLS], int row, int col)
{
int win = 0;//排雷区域'*'和'&'的总数
int x = 0, y = 0;
int a = 0, b = 0;
while (1)
{
int sign = 0;//标记与否
printf("请输入想要排雷的坐标:>");
scanf("%d %d", &x, &y);
while (getchar() != '\n');
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
PrintBoard(Mine, row, col);
break;
}
else
{
//计算该坐标周围有多少雷,并进行递归展开
Count_Spread(Mine, Show, x, y);
system("cls");
PrintBoard(Show, ROW, COL);
win = To_Win(Show, ROW, COL);
if (win == NumMine)
{
printf("恭喜你,扫雷成功!\n");
PrintBoard(Mine, row, col);
break;
}
}
}
else
{
printf("坐标不合法,请重新输入\n");
continue;
}
do
{
printf("标记与否(1表示标记,0表示不标记):>");
scanf("%d", &sign);
while (getchar() != '\n');//清除缓存区
printf("\n");
switch (sign)
{
case 1:
while (1)
{
printf("请输入想要标记的坐标:>");
scanf("%d %d", &a, &b);
while (getchar() != '\n');
if (a >= 1 && a <= row && b >= 1 && b <= col)
{
if (Show[a][b] == '*')
{
Show[a][b] = '&';
system("cls");
PrintBoard(Show, ROW, COL);
break;
}
if (Show[a][b] == '&')
{
Show[a][b] = '*';
system("cls");
PrintBoard(Show, ROW, COL);
break;
}
if ((Show[a][b] != '*') && (Show[a][b] != '&'))
{
printf("坐标不合法,请重新输入\n");
}
}
else
{
printf("坐标不合法,请重新输入\n");
}
}
break;
case 0:
break;
default:
printf("输入错误请重新输入\n");
break;
}
} while (sign);
}
}
game.h
#pragma once
#include
#include
#include
#define NumMine 10//布置雷的个数
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char Board[ROWS][COLS], int rows, int cols, char set);//初始化
void PrintBoard(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);//排查雷
本次的扫雷创作实属不易,参考了许多大佬的文章,修改了许多细节,虽然不太美观但也实现了扫雷的基本功能。