扫雷游戏大家应该都有玩过,今天的我们用C语言实现一下扫雷游戏。点开时第一步不炸死,而且没有雷的周围会展开。
实现思路
1.棋盘设计:
必须要设计俩个棋盘,一个设计者棋盘,一个玩家棋盘。
2.棋盘打印:
打印棋盘
3.雷的布置
我的代码中采用9*9的棋盘,布置了10个雷。用二维数组来存棋盘的信息,将二维数组初始化为0,把布置雷的地方改为1.
4.开局不炸死:
实现第一次不炸死,如果第一次输入的位置为1,就必须将这个位置的1改为0.而寻找另一个为0的位置,将其坐标改为1,来维持雷的总数不变。
5.实现展开:
扫描二维码关注公众号,回复:
4912767 查看本文章
在玩家输入一个周围有雷的坐标时,显示周围雷的个数
6.输赢判断:
由于雷的总数不变,则判断雷的个数是否为10个,为10 时,玩家扫雷成功,游戏结束。
跟前面写过的三子棋游戏一样,还是创建一个game.h的头文件,用于定义游戏程序内的函数。
创建一个一个game.c和一个test.c的源文件,实现游戏。
首先给出game.h文件,后面的游戏实现都会调用到头文件中的函数:
然后给出test.c文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include<stdlib.h>
void menu()
{
printf("*********欢迎来到扫雷***********\n");
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, '*');//初始化玩家棋盘
SetMine(mine, ROW, COL);//布雷
// DisplayBoard(mine, ROW, COL);//打印设计者棋盘(不能给玩家看,所以不打印)
DisplayBoard(show, ROW, COL);//打印玩家棋盘
SafeMine(mine, show, ROW, COL);//第一次不炸死
FindMine(mine, show, ROW, COL);//排雷
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0 :
printf("退出游戏\n");
break;
default :
printf("选择错误\n");
break;
}
system("pause");
return 0;
}
game.c文件:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdlib.h>
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
memset(board, set, rows*cols*sizeof(board[0][0]));
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= row; 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");
}
printf("\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASY_COUNT;//定义count来数雷,总共要布置十个雷
while (count)
{
x = rand() % row + 1;//生成随机下标1-9
y = rand() % col + 1;
if (board[x][y] == '0')//判断这个位置有没有雷,没有雷的话将非雷的坐标由0改为1,每布置好一个雷,count--(这里不用判断坐标是否合法)
{
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
return 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] +
mine[x - 1][y + 1] - 8 * '0';
}
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
int ret = 0;
ret = GetMineCount(mine, x, y);
if (ret == 0)
{
show[x][y] = ' ';
if (x - 1>0 && y>0 && show[x - 1][y] == '*')
OpenMine(mine, show, row, col, x - 1, y);
if (x - 1>0 && y + 1 <= col && show[x - 1][y + 1] == '*')
OpenMine(mine, show, row, col, x - 1, y + 1);
if (x>0 && y + 1 <= col && show[x][y + 1] == '*')
OpenMine(mine, show, row, col, x, y + 1);
if (x + 1 <= row && y + 1 <= col && show[x + 1][y + 1] == '*')
OpenMine(mine, show, row, col, x + 1, y + 1);
if (x + 1 <= row && y>0 && show[x + 1][y] == '*')
OpenMine(mine, show, row, col, x + 1, y);
if (x + 1 <= row && y - 1>0 && show[x + 1][y - 1] == '*')
OpenMine(mine, show, row, col, x + 1, y - 1);
if (x>0 && y - 1>0 && show[x][y - 1] == '*')
OpenMine(mine, show, row, col, x, y - 1);
if (x - 1>0 && y - 1>0 && show[x - 1][y - 1] == '*')
OpenMine(mine, show, row, col, x - 1, y - 1);
}
else
{
show[x][y] = GetMineCount(mine, x, y) + '0';
}
}
void SafeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int ret = 1;
int ch = 0;
printf("请输入要排查的坐标:>");
scanf("%d%d", &x, &y);
if (mine[x][y] == '1')//如果第一次输入坐标为雷将坐标改为0即非雷
{
mine[x][y] = '0';//获取周围雷的个数
ch = GetMineCount(mine, x, y);
show[x][y] = ch + '0';////数字对应的ASCII值和数字字符对应的ASCII值相差48,即'0'的ASCII
while (ret)//在其余有空的地方设置一个雷
{
x = rand() % 9 + 1;//生成随机数1-9
y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
}
ret--;
}
}
OpenMine(mine, show, row, col, x, y);
DisplayBoard(show, row, col);
}
int IsWin(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int count = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
{
count++;
}
}
}
return count;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入要排查的坐标:>");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row &&y >= 1 && y <= col)//判断坐标的有效性
{
if (mine[x][y] == '1')//判断是否为雷,如果是雷提示被炸死并打印设计者棋盘
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else//如果不是雷,获取周围雷的个数并展开
{
int c = GetMineCount(mine, x, y);
show[x][y] = c + '0';
OpenMine(mine, show, row, col, x, y);
DisplayBoard(show, row, col);
if (IsWin(show, row, col) == EASY_COUNT)
{
break;
}
}
}
else//如果坐标无意义,提示坐标非法
{
printf("坐标非法\n");
}
}
if (IsWin(show, row, col) == EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}