用C语言写了一个简单的扫雷项目。实现了一下几个功能
(1)布置雷
(2)排雷(如果当前坐标不不是雷雷,当前坐标的周围也没雷雷,就会向外扩展搜索。)
(3)踩雷,炸死(第一次踩雷不被炸死)
(4)排雷成功判断
实现原理:
雷的信息存储和排查出来的信息存储,用二维数组来存储
为了了实现⼀一个 9*9 的雷雷阵,我们需要创建⼀一个 11*11的数组 。(为了了⽅方便便统计雷雷区边上坐标周围雷的个数)
实现关键步骤:
(1)布置雷
利用rand函数生成随机坐标进行布雷
(2)扫雷
第一次不被炸死
遇到雷炸死
不是雷统计周围雷的个数,如果周围雷的个数为0就进行展开
(3)循环扫雷,直到所有的雷被排出来,就排雷成功
测试函数
test.c
#define _CRT_SECURE_NO_WARNINGS 0
#include"game.h"
void menu()
{
printf("******************************\n");
printf("****** 1.play 0.exit *****\n");
printf("******************************\n");
}//菜单
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
int count = 0;
int x = 0, y = 0;
InitBoard(show, ROWS, COLS, '*');//初始化显示棋盘
InitBoard(mine, ROWS, COLS, '0');//初始化放雷的棋盘
SetMine(mine, ROW, COL);//设置雷的个数
DisplayBoard(show, ROW, COL);//打印显示棋盘
DisplayBoard(mine, ROW, COL);//打印雷的棋盘
Sweep(mine, show,ROW,COL);//扫雷
}
void test()
{
int input = 0;
do
{
menu();
printf("请选择模式:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择错误,请重新选择:\n");
break;
}
} while (input);
}
int main()
{
srand((unsigned int)time(NULL));
test();
system("pause");
return 0;
}
头文件
game.h
#ifndef __GAME_H__
#define __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 10//雷的个数
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);//初始化
void DisplayBoard(char board[ROWS][COLS], int row, int col);//打印
void SetMine(char board[ROWS][COLS], int row, int col);//设置雷
void Sweep(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//排雷
#endif//__GAME_H__
游戏实现:
game.c
#define _CRT_SECURE_NO_WARNINGS 0
#include"game.h"
void InitBoard(char board[ROWS][COLS], 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[i][j] = set;
}
}
}//初始化
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
printf(" ");
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (int 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 i = EASY_COUNT;
while (i)
{
x = (rand() % row) + 1;
y = (rand() % col) + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
i--;
}
}
}//随机设置雷
int GetMineCount(char board[ROWS][COLS], int x, int y)
{
return (board[x + 1][y] + board[x + 1][y - 1] + board[x][y - 1] + board[x - 1][y - 1]
+ board[x - 1][y] + board[x - 1][y + 1] + board[x][y + 1] + board[x + 1][y + 1] - 8 * '0');
}//获取坐标周围雷的个数
void check_around(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col)
{
int ret = GetMineCount(mine, x, y);
if (ret == 0)
{
show[x][y] = ' ';
if ((x - 1) > 0 && (x - 1) <= row && (y - 1) > 0 && (y - 1) <= col && (show[x - 1][y - 1] == '*'))
check_around(mine, show, x - 1, y - 1, row, col);
if ((x - 1) > 0 && (x - 1) <= row && (y) > 0 && (y) <= col && (show[x - 1][y] == '*'))
check_around(mine, show, x - 1, y, row, col);
if ((x - 1) > 0 && (x - 1) <= row && (y + 1) > 0 && (y + 1) <= col && (show[x - 1][y + 1] == '*'))
check_around(mine, show, x - 1, y + 1, row, col);
if ((x) > 0 && (x) <= row && (y - 1) > 0 && (y - 1) <= col && (show[x][y - 1] == '*'))
check_around(mine, show, x, y - 1, row, col);
if ((x) > 0 && (x) <= row && (y + 1) > 0 && (y + 1) <= col && (show[x][y + 1] == '*'))
check_around(mine, show, x, y + 1, row, col);
if ((x + 1) > 0 && (x + 1) <= row && (y - 1) > 0 && (y - 1) <= col && (show[x + 1][y - 1] == '*'))
check_around(mine, show, x + 1, y - 1, row, col);
if ((x + 1) > 0 && (x + 1) <= row && (y) > 0 && (y) <= col && (show[x + 1][y] == '*'))
check_around(mine, show, x + 1, y, row, col);
if ((x + 1) > 0 && (x + 1) <= row && (y + 1) > 0 && (y + 1) <= col && (show[x + 1][y + 1] == '*'))
check_around(mine, show, x + 1, y + 1, row, col);
}
else
{
show[x][y] = ret + '0';
}
}//检测坐标周围的雷,进行展开
void MoveMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int x,int y)
{
mine[x][y] = '0';
int i = 0, j = 0;
check_around(mine, show, x, y, row, col);
while (1)
{
i = (rand() % row) + 1;
j = (rand() % col) + 1;
if (mine[i][j] == '0' && (!((i == x) && (j == y))))
{
mine[i][j] = '1';
break;
}
}
}//第一次扫雷不被炸死,把雷移走
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++;
}
}
if (count == EASY_COUNT)
return 1;
else return 0;
}//判断是否赢
void Sweep(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int flag = 1;//flag为1时表示第一次输入
while (1)
{
printf("请输入:");
scanf("%d%d", &x, &y);
if (mine[x][y] == '1')//如果是雷
{
if (flag)//第一次进行扫雷时将雷移走
{
MoveMine(mine,show,row,col,x,y);
}
else
{
break;//不是第一次扫雷就跳出循环,炸死了
}
}
else//如果不是雷
{
check_around(mine, show, x, y, row, col);//进行展开和扫雷
}
flag = 0;//第一次扫雷后就把flag置为0
DisplayBoard(show, row, col);//扫雷后打印显示棋盘
/*DisplayBoard(mine, row, col);*/
if (IsWin(show, row, col))//如果所有雷都扫完后,跳出循环,赢了
break;
}
if (IsWin(show, row, col))
{
printf("恭喜您!排雷成功!\n");
}
else
{
printf("很遗憾!您被炸死了!\n");
}
}//扫雷