1.建立工程,分为game.h ,game.c Test_game.c 三个文件实现游戏相关内容
2.排雷,排到雷被炸死
3.递归展开排雷点附近没有雷的地方
4.显示结束用时
头文件的声明 game.h
#define ROW 11 // 9行
#define COL 11 // 9列 // 多出的两行两列方便统计8个点的雷的数量
#define NUMS 20
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// 1.初始化棋盘
void InitsBoard(char board[ROW][COL], int row, int col);
//2. 布置雷
void PutDanger(char board[ROW][COL], int row, int col, int nums);
// 打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col);
//拆除雷
char DeleteDanger(char rboard[ROW][COL], char sboard[ROW][COL], int row, int col);
游戏的主函数 (主函数简陋)Test_game.c
#include"game.h"
double start, finish;
void meau()
{
printf("#####################################\n");
printf("########## 1.play ########\n");
printf("########## 0.exit ########\n");
printf("#####################################\n");
}
void game()
{
// 扫雷
char sboard[ROW][COL];
char rboard[ROW][COL] = { 0 };
//初始化展示棋盘
InitsBoard(sboard, ROW, COL);
//布置雷
PutDanger(rboard, ROW, COL,NUMS);
// 打印棋盘
DisplayBoard(sboard, ROW, COL);
int times = ((ROW - 2) * (COL - 2) - NUMS) ;
while (times) // times用于检验是否排掉了所有雷还没爆炸
{
//拆除雷
char ret = DeleteDanger(rboard, sboard, ROW, COL);
if (ret == 'D')
{
// 打印棋盘
DisplayBoard(rboard, ROW, COL);
printf("你被炸死了 ");
finish = clock();//取结束时间
printf("用时%d 秒\n", (int)(finish - start) / CLOCKS_PER_SEC);
break;
}
else if(ret == 'C')
{
// 打印棋盘
DisplayBoard(sboard, ROW, COL);
// 只有没被炸才会减减
times--;
}
}
if (times == 0)
{
// 打印棋盘
DisplayBoard(rboard, ROW, COL);
printf("你赢了 ");
finish = clock();//取结束时间
printf("用时%d 秒\n", (int)(finish - start) / CLOCKS_PER_SEC);
}
}
int main()
{
srand((unsigned)time(NULL));
int input = 0;
do
{
meau();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
必要函数的实现 game.c
#include"game.h"
// 1.初始化展示棋盘
void InitsBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = '*';
}
}
}
//2.布置雷
void PutDanger(char board[ROW][COL], int row, int col,int nums)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
board[i][j] = '0';
}
}
int count = 0;
while(count < nums)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count++;
}
}
}
// 打印棋盘
void DisplayBoard(char board[ROW][COL], int row, int col)
{
for (int i = 0; i < row - 1; i++)
{
printf("%d ", i);
}
printf("\n");
for (int i = 1; i < row - 1; i++)
{
printf("%d ", i);
for (int j = 1; j < col - 1; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
// 统计雷的数量
int Sum(char sboard[ROW][COL],char rboard[][COL] ,int x,int y)
{
int Dnums = 0;
for (int i = x - 1; i <= x + 1; i++)
{
for (int j = y - 1; j <= y + 1; j++)
{
if (rboard[i][j] == '1')
{
Dnums++;
}
}
}
return Dnums;
}
void Strech(char sboard[ROW][COL],char rboard[ROW][COL],int x,int y)
{
if (x < 1 || x > 9 || y < 1 || y > 9) // 递归到排雷点出到界外需要停下了
{
return;
}
if (sboard[x][y] != '*')// 碰到已经排查过的就停下
{
return;
}
int Dnums = Sum(sboard, rboard, x, y); // 排雷点旁八个格子内有雷也停下
if (Dnums != 0)
{
sboard[x][y] = Dnums + '0';
return;
}
sboard[x][y] = '0';
// 八个方向延伸
Strech(sboard, rboard, x - 1, y - 1);
Strech(sboard, rboard, x - 1, y );
Strech(sboard, rboard, x - 1, y + 1);
Strech(sboard, rboard, x , y - 1);
Strech(sboard, rboard, x , y + 1);
Strech(sboard, rboard, x + 1, y - 1);
Strech(sboard, rboard, x + 1, y );
Strech(sboard, rboard, x + 1, y + 1);
}
//拆除雷
char DeleteDanger(char rboard[ROW][COL], char sboard[ROW][COL], int row, int col)
{
printf("请输入要排插位置的坐标:\n");
while (1)
{
int x = 0;
int y = 0;
scanf("%d %d", &x, &y);
//int x = rand() % 9 + 1;
//int y = rand() % 9 + 1;
if (x >= 1 && x < row - 1 && y >= 1 && y < col - 1 && sboard[x][y] == '*')
{
if (rboard[x][y] == '1')
{
return 'D';
break;
}
else
{
//int Dnums = Sum(rboard, x, y);
Strech(sboard,rboard, x, y); // 延申
//sboard[x][y] = Dnums + '0';
return 'C';
break;
}
}
else
{
printf("输入坐标有误,请重新输入\n");
}
}
}