当前位置:网站首页>C语言实现【扫雷游戏】完整版(实现源码)
C语言实现【扫雷游戏】完整版(实现源码)
2022-07-01 06:52:00 【周周汪】
游戏规则介绍
扫雷大家没玩过也听说过吧,它长这个样子
游戏规则
你点到一个数字,如果是3,那就说明最靠近他它周围的8个格里有3个雷。然后通过相邻或者相间的数字之间的交集来判断哪些是雷。如果你确定一个格是雷,就对他点右键;若果不是雷,就点左键排除。当你找到所有地雷,那么恭喜你胜利!
需要注意的几点
- 我们其实需要两个棋盘,一个是底层布置雷的棋盘,这个棋盘来记录地雷位置,哪些是安全位置;另一个是给玩家展示出来的棋盘,这个棋盘刚开始位置都是未知,随着玩家的探索,一步步展开,此位置是地雷,还是数字来提示周围有多少地雷,哪些是未探索的。
- 假如我们要玩一个10x10的扫雷,那么底层我们要设计的实际棋盘大小应该是12x12的,多出一圈,目的是为了我们要标记最外面一圈的信息,如果不多一圈,那么就越界了。
- 我们的版本中涉及了一片递归展开,就是当一周都没有雷的话会自动为你递归展开。
- 还涉及了防止第一步就踩到雷的方法,就是如果判断你不幸第一步就踩雷了,底层会自动给你把这个雷随机换一个空位置,防止你第一步就Gameover。
- 缺点就是我们这是黑窗口的,比较丑,大家可以设计的更美观,甚至是联网的,非窗口式的。
实现代码
我们需要一个game.h的头文件,包含所有需要的函数接口的声明
#pragma once
#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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//展开
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
//防止第一步就踩中地雷
void Prevent_jinx(char mine[ROWS][COLS], int x, int y);
//统计安全区*的数量
int Countshow(char board[ROWS][COLS], int row, int col, char sign);
然后我们在game.c中实现函数的定义
#define _CRT_SECURE_NO_WARNINGS 1
#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("------------------------\n");
for (i = 0; i <= 9; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
int j = 0;
printf("%d ", i);
for (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 count = EASY_COUNT;
while (count)
{
//1. 生成随机下标
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] != '1')
{
board[x][y] = '1';
count--;
}
}
}
void Prevent_jinx(char mine[ROWS][COLS], int x, int y)
{
int m, n;
while (1)
{
m = rand() % ROW + 1;
n = rand() % COL + 1;
if (mine[m][n] != '1')
{
mine[m][n] = '1';
mine[x][y] = '0';
break;
}
}
}
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
if (x >= 1 && x <= ROW && y >= 1 && y <= COL && mine[x][y] != '1') //x,y在合法范围内,而且不是雷区
{
int count = GetMineCount(mine, x, y);
if (count == 0) //如果count=0,则需要继续扩展找雷
{
show[x][y] = '0';
int i, j;
//依次遍历周围八个点
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (show[i][j] != '*') continue;
//如果已被标记过则不用进入递归,避免重复查找
OpenMine(mine, show, i, j);
}
}
}
else //count不为0 停止扩展找雷
{
show[x][y] = count + '0';
}
}
else return;
}
int GetMineCount(char mine[ROWS][COLS], int x, int y)
{
int i, j, count = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
if (mine[i][j] == '1')
count++;
}
}
return count;
}
int Countshow(char board[ROWS][COLS], int row, int col, char sign)
{
int count = 0, i, j;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (board[i][j] == sign)
count++;
}
}
return count;
}
void FindMine(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 (win == 0 && mine[x][y] == '1')//如果一上来就踩雷
{
Prevent_jinx(mine, x, y);
}
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了(1是炸弹所在位置)\n");
DisplayBoard(mine, ROW, COL);
break;
}
else
{
OpenMine(mine, show, x, y);
win = ROW * COL - Countshow(show, ROW, COL, '*');
if (Countshow(show, ROW, COL, '*') == EASY_COUNT)
{
printf("恭喜你,排雷成功(1是炸弹所在位置)\n");
DisplayBoard(mine, ROW, COL);
break;
}
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("坐标非法,重新输入\n");
}
}
}
最后我们在test.c中实现测试
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("********************************\n");
printf("********* 1. play ********\n");
printf("********* 0. exit ********\n");
printf("********************************\n");
}
void game()
{
char mine[ROWS][COLS] = {
0 };//存放雷的信息
char show[ROWS][COLS] = {
0 };//存放排查出的雷的信息
//初始化一下棋盘
InitBoard(mine, ROWS, COLS, '0');//'0'
InitBoard(show, ROWS, COLS, '*');//'*'
//布置雷
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
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;
}
效果展示
感谢阅读,我们下期再见
如有错 欢迎提出一起交流
边栏推荐
- Problem: officeexception: failed to start and connect (II)
- [wechat applet low code development] second, resolve the code composition of the applet in practice
- Several ways of gson's @jsonadapter annotation
- 【计网】(一) 集线器、网桥、交换机、路由器等概念
- 树莓派4的WiFi设置
- ESP32 ESP-IDF ADC监测电池电压(带校正)
- AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
- Jena基于OWL的默认推理查询
- Is fixed investment fund a high-risk product?
- RestTemplate使用
猜你喜欢
Introduction to spark (one article is enough)
Dirty reading, unreal reading and unrepeatable reading
Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor
2022 Jiangsu Vocational College skills competition (secondary vocational school) network construction and application open competition volume
【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现
Spark入门(一篇就够了)
图解事件坐标screenX、clientX、pageX, offsetX的区别
WiFi settings for raspberry Pie 4
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
【LINGO】求解二次规划
随机推荐
AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
【分类模型】Q 型聚类分析
如何通过cdn方式使用阿里巴巴矢量图字体文件
8 figures | analyze Eureka's first synchronization registry
Esp32 - ULP coprocessor reading Hall sensor in low power mode
Software engineering review
用手机在指南针上开户靠谱吗?这样有没有什么安全隐患
常用快捷键
【LINGO】求七个城市最小连线图,使天然气管道价格最低
为什么这么多人转行产品经理?产品经理发展前景如何?
Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
记一次线上接口慢查询问题排查
Esp32 esp-idf ADC monitors battery voltage (with correction)
How to permanently configure local opencv4.5.5 for vs2019
Several ways of gson's @jsonadapter annotation
Dirty reading, unreal reading and unrepeatable reading
产品学习(三)——需求列表
Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
Product learning (I) - structure diagram