第一次尝试扫雷游戏,一开始对代码各部分的衔接不是很熟悉,但是通过自己百度与其他大神的帮忙,捋清了整个游戏程序各部分之间的关联,最终使程序成功运行起来,分享到这里,同时希望各位大神可以指出优化的地方。
game.h
#define __GAME_H__
#define ROWS ROW+2
#define COLS COL+2
#define ROW 10
#define COL 10
#define MINECOUNT 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitMine(char mine[ROWS][COLS], int row, int col);
void InitShow(char show[ROWS][COLS], int row, int col);
void DisplayBoard(char arr[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int x, int y, int count);
void PlayGame(char mine[ROWS][COLS], char show[ROWS][COLS]);
int GetMineCount(char mine[ROWS][COLS], int i, int j);
void ResetMine(char mine[ROWS][COLS], int row, int col, int x, int y, int count);
void expand(char mine[ROWS][COLS], int x, int y, char show[ROWS][COLS], int *p);
#endif
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void game()
{
char mine[ROWS][COLS];
char show[ROWS][COLS];
srand((unsigned int)time(NULL));
InitMine(mine, ROWS, COLS);
InitShow(show, ROW, COL);
SetMine(mine, ROW, COL, MINECOUNT);
DisplayBoard(show, ROW, COL);
PlayGame(mine, show);
}
void menu()
{
printf("***************************\n");
printf("**********扫雷游戏*********\n");
printf("******** 1.开始游戏 *******\n");
printf("******** 0.退出程序 *******\n");
printf("***************************\n");
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("输入错误!");
}
} while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
// 初始化雷盘
void InitMine(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
mine[i][j] = '0';
}
}
}
//初始化展示的雷盘
void InitShow(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
show[i][j] = '*';
}
}
}
// 随机布雷
void SetMine(char mine[ROWS][COLS], int row, int col, int count)
{
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
mine[x][y] = '1';
count--;
}
}
// 打印棋盘
void DisplayBoard(char arr[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i <= row; i++)
{
printf(" %d", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%2d", i);
for (j = 1; j <= col; j++)
{
printf(" %c", arr[i][j]);
}
printf("\n");
}
}
// 第一次踩到雷,重新布雷
void ResetMine(char mine[ROWS][COLS], int row, int col, int x, int y, int count)
{
mine[x][y] = '0';
while (count)
{
int i = rand() % row + 1;
int j = rand() % col + 1;
if ((mine[i][j] != '1') && i != x && j != y)
{
mine[i][j] = '1';
count--;
}
}
}
//统计雷的个数
int GetMineCount(char mine[ROWS][COLS], int i, int j)
{
return mine[i - 1][j] +
mine[i - 1][j - 1] +
mine[i][j - 1] +
mine[i + 1][j - 1] +
mine[i + 1][j] +
mine[i + 1][j + 1] +
mine[i][j + 1] +
mine[i - 1][j + 1] - 8 * '0';
}
//扩展函数
void expand(char mine[ROWS][COLS], int x, int y, char show[ROWS][COLS], int *p)
{
int i = -1;
int j = -1;
for (i = -1; i < 2; i++) //边界
{
for (j = -1; j < 2; j++)
{
if (i != 0 || j != 0) // 避免排到自己注意此处的逻辑关系
{
if (x + i >= 1 && x + i <= ROW && y + j >= 1 && y + j <= COL) //x y坐标是否合法
{
if (show[x + i][y + j] == '*'&&mine[x+i][y+j]!='1')
{
int count = GetMineCount(mine, x + i, y + j);
if (count != 0)
{
show[x + i][y + j] = count + '0';
(*p)++;
}
else
{
show[x + i][y + j] = ' ';
(*p)++;
expand(mine, x + i, y + j, show, p);
}
}
}
}
}
}
}
// 玩游戏步骤
void PlayGame(char mine[ROWS][COLS], char show[ROWS][COLS])
{
int x = 0;
int y = 0;
int win = 0; //统计排雷的个数
int count = 0; // 统计雷的个数
while (win < ROW*COL - MINECOUNT)
{
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
if (show[x][y] == count + '0') //避免重复排雷
{
printf("已经排过雷\n");
}
if (x >= 1 && x <= ROW && y >= 1 && y <= COL) //输入坐标是否合法
{
if (mine[x][y] == '1')
{
if (0 == win) //为了游戏体验,第一次踩到雷,重新布雷
{
ResetMine(mine, ROW, COL, x, y, 1);
//display(mine, ROW, COL);
count = GetMineCount(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';
win++;
expand(mine, x, y, show, &win); //如果周围没有雷,进行扩展
DisplayBoard(show, ROW, COL);
}
else
{
show[x][y] = count + '0';
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("很遗憾,你被炸死了\n");
DisplayBoard(mine, ROW, COL);
break;
}
}
else
{
count = GetMineCount(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';
}
else
{
show[x][y] = count + '0';
}
win++;
expand(mine, x, y, show, &win);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("输入坐标不合法\n");
}
}
if (win == ROW*COL - MINECOUNT)
{
printf("排雷成功\n");
DisplayBoard(mine, ROW, COL);
}
}
运行结果界面:
![](https://i-blog.csdnimg.cn/blog_migrate/b2c511d423cc84d8daf939f753c05a96.png)
![](https://i-blog.csdnimg.cn/blog_migrate/64f8fbec8b3c0966f9486adb84f4b8dc.png)
![](https://i-blog.csdnimg.cn/blog_migrate/ccb62d8d0c302ffa0520924ff51b37f6.png)