前言
❤️ :热爱编程学习,期待一起交流!
🙏:博主水平有限,如有发现错误,求告知,多谢!
- 为了游戏代码方便测试,和为了方便理解。我们将扫雷游戏分为三个部分。
- game.h头文件部分(进行符号常量的声明,宏定义等)源文件test.c和game.c用时需要引用。
格式为:#include “game.h” - test.c游戏测试部分,整个游戏的大致思路在这里面放着。
- game.c游戏实现部分。包括布置雷区,埋雷,扫雷,判断输赢等。
游戏规则
- 扫雷就是要把所有非地雷的格子揭开即胜利;踩到地雷格子就算失败。
- 游戏主区域由9*9方格组成
- 输入坐标选择一个方格
- 方格即被打开并显示出方格中的数字,方格中数字则表示其周围的8个方格隐藏了几颗雷
- 如果点开的格子为空白格,即其周围有0颗雷,则其周围格子自动打开
生成菜单
- 玩完一把不过瘾再玩一把,所以要用do while语句,让游戏还没开始玩就已经生成菜单。
menu()//菜单的实现
{
printf("******* 1.ply *****\n");
printf("******* 0.exit *****\n");
}
test()
{
srand((unsigned int)time(NULL));//设置随机数的生成器
int n = 0;
do
{
menu();//调用menu函数
scanf("%d", &n);
switch (n)
{
case 1:
game();//调用game函数,游戏的大致思路都在这里面。
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (n);
}
int main()
{
test();
return 0;
}
创建雷区(创建9 * 9大小的二维数组)
- 以下是我们game.h里的宏定义,函数的声明。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define EASY_COUNT 10//我们布置10个雷
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
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 mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
- 到这里我们就进入game()函数里面了,这里面是游戏的大致思路。我们需要先认识一下大致思路。
game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine, ROWS, COLS, '0');//在game()函数里需要穿两次实参,第一次传mine数组
InitBoard(show, ROWS, COLS, '@');//第二次传show数组,但在game.c里定义的时候只需要一次。一次就可以完成两次函数调用的实现。
SetMine(mine, ROW, COL);//设置雷的时候只需要在9*9的雷区布置雷就好。所以传参传的是ROW,COL。但需要注意,接收的时候需要用11*11接收。这是因为防止在扫雷(FindMine)的时候数组越界。
DisplayBoard(show, ROW, COL);//给玩家看只需要展出9*9就行,所以传参还是传的ROW和COL,但在game.c中接收的时候需要用11*11的接收。
FindMine(mine, show, ROW, COL);//需要程序员根据第一个雷区的信息,来判断周围八个格子有几颗雷,然后呈现到第二个雷区上给玩家看。所以需要把两个雷区mine和show两个实参都传到game.c中。
}
- 重点说一下,这里需要创建两个雷区A和B,A雷区用来得到埋雷的相关信息,B雷区用来显示你扫的那个格子的周围八个格子中有几个雷。
- A雷区在程序员脑子里,不展示出来。
- B雷区呈现在界面上,玩家看的。
以上大致意思就是(需要程序员根据第一个雷区的信息,来判断周围八个格子有几颗雷,然后呈现到第二个雷区上给玩家看)
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
初始化雷区(遍历后赋值)
- 将A雷区初始化为字符0
将B雷区初始化为字符@
(注意是字符) - 这里只需要一个函数就可以初始化两个雷区。
void InitBoard(char mine[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++)
{
mine[i][j] = set;
}
}
}
布置雷(随机数的生成)
- rand()为C语言中的函数,调用该函数需要加头文件#include<stdlib.h>,而在调用rand()函数的时候系统会自动调用srand()函数,srand()会设置供rand()使用的随机数种子。
- rand()的取值范围是0~RAND_MAX(32767)
- 将雷布置 ‘1’,不是雷为 ‘0’ (注意这里是字符1和字符0)
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;//这里是设置雷的个数。
while (count)
{
int x = rand() % row + 1;//这里实参传的row为9,一个数对9求余得到的0~8的数字,再加1就是1~9。
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
展览雷区
- 我们布置好雷后需要将B雷区展览出来后,玩家才能进行排雷操作。
void DisplayBoard(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
}
排雷
根据ASCII码值表,字符2减去字符0就是数字2。eg:‘2’ - ‘0’ = 2
- 所以2 + ‘0’ = ‘2’
void SpreadMine(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
int count = get_mine_count(mine, x, y);
if (0 == count)
{
show[x][y] = ' ';
if (show[x][y + 1] == '@')
SpreadMine(mine, show, x, y+1);
if (show[x][y-1] == '@')
SpreadMine(mine, show, x, y-1);
if (show[x+1][y] == '@')
SpreadMine(mine, show, x+1, y);
if (show[x+1][y-1] == '@')
SpreadMine(mine, show, x+1, y-1);
if (show[x+1][y+1] == '@')
SpreadMine(mine, show, x+1, y+1);
if (show[x-1][y] == '@')
SpreadMine(mine, show, x-1, y);
if (show[x-1][y+1] == '@')
SpreadMine(mine, show, x-1, y+1);
if (show[x-1][y-1] == '@')
SpreadMine(mine, show, x-1, y-1);
}
else
{
show[x][y] = count + '0';
}
}
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{//加static的原因让其无法在另外一个源文件中访问这个自定义函数
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';//计算周围多少个字符1,然后减去八个‘0’,就是得到的数字了。
}
int is_win(char show[ROWS][COLS], int row, int col)
{
int c = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if(show[i][j]=='@')
c++;
}
}
return c;
}
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);
}
else
{
SpreadMine(mine, show, x, y);
DisplayBoard(show, ROW, COL);
if (is_win(show, ROW, COL) == EASY_COUNT)//判断是否赢了。
{
printf("恭喜你赢了");
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("输入坐标非法,无法排雷,请重新输入\n");
}
}
}
game.h
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
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 mine[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
test.c
#include "game2.h"
menu()
{
printf("*******1.ply *****\n");
printf("*******0.exit *****\n");
}
game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitBoard(mine, ROWS, COLS, '0');//在game()函数里需要穿两次实参,第一次传mine数组
InitBoard(show, ROWS, COLS, '@');//第二次传show数组,但在game.c里定义的时候只需要一次。一次就可以完成两次函数调用的实现。
SetMine(mine, ROW, COL);//设置雷的时候只需要在9*9的雷区布置雷就好。所以传参传的是ROW,COL。但需要注意,接收的时候需要用11*11接收。这是因为防止在扫雷(FindMine)的时候数组越界。
DisplayBoard(show, ROW, COL);//给玩家看只需要展出9*9就行,所以传参还是传的ROW,和COL,但在game.c中接收的时候需要用11*11的接收。
FindMine(mine, show, ROW, COL);//需要程序员根据第一个雷区的信息,来判断周围八个格子有几颗雷,然后呈现到第二个雷区上给玩家看。所以需要把两个雷区mine和show两个实参都传到game.c中。
}
test()
{
srand((unsigned int)time(NULL));
int n = 0;
do
{
menu();
scanf("%d", &n);
switch (n)
{
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("请重新选择\n");
break;
}
} while (n);
}
int main()
{
test();
return 0;
}
game.c
#include "game2.h"
void InitBoard(char mine[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++)
{
mine[i][j] = set;
}
}
}
void DisplayBoard(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
void SpreadMine(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
int count = get_mine_count(mine, x, y);
if (0 == count)
{
show[x][y] = ' ';
if (show[x][y + 1] == '@')
SpreadMine(mine, show, x, y+1);
if (show[x][y-1] == '@')
SpreadMine(mine, show, x, y-1);
if (show[x+1][y] == '@')
SpreadMine(mine, show, x+1, y);
if (show[x+1][y-1] == '@')
SpreadMine(mine, show, x+1, y-1);
if (show[x+1][y+1] == '@')
SpreadMine(mine, show, x+1, y+1);
if (show[x-1][y] == '@')
SpreadMine(mine, show, x-1, y);
if (show[x-1][y+1] == '@')
SpreadMine(mine, show, x-1, y+1);
if (show[x-1][y-1] == '@')
SpreadMine(mine, show, x-1, y-1);
}
else
{
show[x][y] = count + '0';
}
}
static int get_mine_count(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';
}
int is_win(char show[ROWS][COLS], int row, int col)
{
int c = 0;
for (int i = 1; i <= row; i++)
{
for (int j = 1; j <= col; j++)
{
if(show[i][j]=='@')
c++;
}
}
return c;
}
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
{
SpreadMine(mine, show, x, y);
DisplayBoard(show, ROW, COL);
if (is_win(show, ROW, COL) == EASY_COUNT)
{
printf("恭喜你赢了");
DisplayBoard(show, ROW, COL);
}
}
}
else
{
printf("输入坐标非法,无法排雷,请重新输入\n");
}
}
}
效果图
- 菜单
- 扫雷展开一片
最后
最后,如果你觉得我的文章对你有帮助🎉欢迎关注🔎点赞👍收藏⭐️留言📝。