C语言实现扫雷小游戏
实现的步骤如下:
1、首先为了能够简单的实现扫雷游戏,我们采用两个char类型的二维数组来实现,mine数组个用来布置雷的位置信息,show数组用来显某一点周边存在雷的个数,并且在mine数组中使用字符’1’来代表雷,字符’0’代表非雷。
2、接着对两个数组分别进行初始化,将mine数组全部初始化为字符’0’,show数组初始化为字符’*’。
3、然后需要SetMine函数对mine数组内布置雷的位置信息,以及打印函数DisplayBoard将布置雷的信息在屏幕上显示。
4、最后一步是排查雷的个数,当完全排除出mine数组中雷的个数时,排雷成功。
game.h
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2 //设置棋盘的大小
#define COLS COL+2
#define EASY_COUNT 80//设置雷的个数
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard(char board[ROWS][COLS], int rows, int cols);
void SetMine(char board[ROWS][COLS], int rows, int cols, int count);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols);
game.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.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 rows, int cols)
{
int i = 0;
int j = 0;
printf("--------------------------------------\n");
for (i = 0; i <=rows; i++)
{
printf(" %d", i);
}
printf("\n");
for (i = 1; i <= rows; i++)
{
printf(" %d", i);
for (j = 1; j <= cols; j++)
{
printf(" %c", board[i][j]);
}
printf("\n");
}
printf("--------------------------------------\n");
}
void SetMine(char board[ROWS][COLS], int rows, int cols, int count)
{
int x = 0;
int y = 0;
while (count)
{
x = rand() % rows + 1;
y = rand() % cols + 1;
if ((board[x][y] == '0')&&(board[x][y] != '1'))
{
board[x][y] = '1';
count--;
}
}
}
int MineNum(char mine[ROWS][COLS], int x, int y)
{
return 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] + mine[x-1][y] - 8 * '0';
}
void recursion(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int ret = MineNum(mine, x, y);
if (ret==0)
{
show[x][y] = '=';
if ((x - 1) > 0 && (y - 1) > 0 && (show[x - 1][y - 1] == '*'))
{
recursion(mine, show, x - 1, y - 1);
}
if ((x) > 0 && (y - 1) > 0 && (show[x][y - 1] == '*'))
{
recursion(mine, show, x, y - 1);
}
if ((x+1) > 0 && (y - 1) > 0 && (show[x+1][y - 1] == '*'))
{
recursion(mine, show, x + 1, y - 1);
}
if ((x + 1) > 0 && (y +1) > 0 && (show[x + 1][y - 1] == '*'))
{
recursion(mine, show, x + 1, y + 1);
}
if ((x) > 0 && (y + 1) > 0 && (show[x][y +1] == '*'))
{
recursion(mine, show, x, y + 1);
}
if ((x-1) > 0 && (y + 1) > 0 && (show[x-1][y + 1] == '*'))
{
recursion(mine, show, x-1, y + 1);
}
if ((x - 1) > 0 && (y ) > 0 && (show[x - 1][y ] == '*'))
{
recursion(mine, show, x - 1, y);
}
}
else
{
show[x][y] = ret + '0';
}
}
int looknum(char show[ROWS][COLS], int rows, int cols)
{
int num=0;
int i = 0;
int j = 0;
for (i = 1; i <= 9; i++)
{
for (j = 1; j <= 9; j++)
{
if (show[i][j] == '*')
num++;
}
}
return num;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols)
{
int x = 0;
int y = 0;
int ret = 0;
int num = looknum(show, ROW, COL);//在show数组里面找*的个数当*只有ROW*COL-ROW*COL-雷的个数时扫雷成功
while (num!=EASY_COUNT)
{
if (num == EASY_COUNT)
break;
printf("请输入棋子的坐标: ");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (mine[x][y] == '0')
{
ret = MineNum(mine, x, y);//MineNum的功能使以x,y坐标查找周围八个坐标有多上个雷
//以MineNum函数的返回值做为递归的条件当返回雷的个数为0时函数recursion开始递归将不是雷的位置改为 '='
if (ret == 0)
{
recursion(mine,show,x,y);
DisplayBoard(show, ROW, COL);
}
else
{
show[x][y] = ret + '0';
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("很遗憾,您被炸死了\n");
break;
}
}
else
{
printf("输入坐标不合法请重新输入\n");
}
num = looknum(show, ROW, COL);
}
if (num == EASY_COUNT)
{
printf("恭喜您,扫雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void menue()
{
printf("*******************************************\n");
printf("************* 1、play **************\n");
printf("************* 0、exit **************\n");
printf("*******************************************\n");
}
void game()
{
char mine[ROWS][COLS];//创建一个存放雷的信息的二维数组
char show[ROWS][COLS];//用来显示某一位置八个方向雷的个数
InitBoard(mine, ROWS, COLS, '0');//将mine初始化设置为字符零
InitBoard(show, ROWS, COLS, '*');
//DisplayBoard(mine, ROW, COL);
//DisplayBoard(show, ROW, COL);
SetMine(mine, ROW, COL,EASY_COUNT);//对mine内设置雷,其中EASY_COUNT代表雷的个数
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menue();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("欢迎下次继续玩扫雷游戏\n");
break;
default:
printf("选择错误,请重新输入\n");
break;
}
} while (input);
return 0;
}