当前位置:网站首页>【实现简易版扫雷小游戏】
【实现简易版扫雷小游戏】
2022-07-28 05:26:00 【JuLiJuLi.】
想要实现扫雷小游戏需要做些什么?
1.首先我们要创建两个二维数组,第一个用来展示给玩家供玩家扫雷的,第二个数组用来后面布置雷的,这个数组要扩大一圈,用来防止后面计算附近有几颗雷是边缘位置越界问题。
2.先将第一个二维数组初始化为全0;再将第二个棋盘中部分位置上布置上雷。
3.如何生成随机的雷,这里我们用srand函数以及time函数配合来完成随机值,以此来布置随机的雷。
4.判断玩家输入的坐标是否是雷。
5.玩家扫到不是雷的地方如何产生提示附近有几颗雷。
具体如何实现,代码如下.
(1).Li.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 init_board(char arr[ROWS][COLS], int rows, int cols, char set);
void show_board(char arr[ROWS][COLS], int row, int col);
void set_mine(char mine[ROWS][COLS], int row, int col);
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);(2)game.c源文件用来实现游戏的逻辑.
#include "Li.h"
void menu()
{
printf("******************************\n");
printf("********* 1. play ********\n");
printf("********* 0. exit ********\n");
printf("******************************\n");
}
void game()
{
//扫雷游戏的实现
char mine[ROWS][COLS] = { 0 };//'0' //用来存放布置好的雷的信息
char show[ROWS][COLS] = { 0 };//'*' //用来存放排查出的雷的信息
init_board(mine, ROWS, COLS, '0'); //初始化棋盘为'0'
init_board(show, ROWS, COLS, '*'); //初始化棋盘为'*'
//布置雷
set_mine(mine, ROW, COL);
show_board(show, ROW, COL);
find_mine(mine, show, ROW, COL); //排查雷
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL)); //利用srand函数与time函数生成随机数
do
{
menu();
printf("请输入-->:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("选择错误,请重新选择!\n");
break;
}
} while (input);
return 0;
}(3).Li.c源文件用来实现扫雷小游戏.
#include "Li.h"
void init_board(char arr[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++)
{
arr[i][j] = set;
}
}
}
void show_board(char arr[ROWS][COLS], int row, int col) //打印棋盘
{
int i = 0;
int j = 0;
printf("--------扫雷-------\n");
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 ", arr[i][j]);
}
printf("\n");
}
printf("--------扫雷-------\n");
}
void set_mine(char mine[ROWS][COLS], int row, int col) //布置雷
{ //布置雷"1"为雷
int count = EASY_COUNT;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1; //控制随机数的生成范围
y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
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';
}
void find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) //排查结果
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("请输入要排查的坐标-->:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了!\n");
show_board(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
show_board(show, ROW, COL);
win++;
}
}
else
{
printf("坐标非法,重新输入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功了!\n");
show_board(mine, ROW, COL);
}
}边栏推荐
- 量化交易机器人系统开发
- Getting started with hugging face
- 使用wampserver3.2.6时切换中文时造成启动失败
- What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation
- qt设置加载界面的几种方法
- 小程序:生命周期
- 解决内存占用比应用进程占用高的问题
- MFC uses the console to print program information
- 如何模拟实现strcpy库函数
- 七夕送什么礼物最实用?送人绝对不会出错的礼物值得买
猜你喜欢
随机推荐
The startup fails when switching Chinese when using wampserver3.2.6
Introduction to Perl (IX) quotation
根据IP地址和子网掩码求主机所在的网络地址和广播地址
Solve the problem that the memory occupation is higher than that of the application process
我的注解笔记
MySQL安装与使用
mysql join技巧
Perl Introduction (10) formatted output
What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation
NFT数藏盲盒+模式系统开发
【学习笔记】vim 编辑器
Pytorch learning note 4 - automatic calculation of gradient descent autograd
多个ics日历合并成单个ics日历
qt批量操作控件,并设置信号槽
[c语言]简易通讯录的实现
Vs code basic configuration and beautification
C语言的编译和预处理
VI and VIM commands
2022-07-19 达梦数据库-实例创建和管理
Listener









