当前位置:网站首页>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;
}
效果展示

感谢阅读,我们下期再见
如有错 欢迎提出一起交流
边栏推荐
- 解决kaniko push镜像到harbor时报错(代理导致):unexpected status code 503 Service Unavailable
- [Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
- Esp32 esp-idf ADC monitors battery voltage (with correction)
- 比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
- Is the account opening of Huafu securities safe and reliable? How to open Huafu securities account
- K8S搭建Redis集群
- [lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
- SQL learning notes 2
- 【微信小程序低代码开发】二,在实操中化解小程序的代码组成
- Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
猜你喜欢

TDB中多个model情况下使用fuseki查询

问题:OfficeException: failed to start and connect(三)

Embedded system

Product learning (III) - demand list
![[lingo] find the shortest path problem of undirected graph](/img/14/1ccae0f33f5857b546d7fd0aa74c35.png)
[lingo] find the shortest path problem of undirected graph

On whether variables are thread safe

【微信小程序】一文解决button、input、image组件

灰度何以跌下神坛?

Software engineering review

女生适合学产品经理吗?有什么优势?
随机推荐
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
Common shortcut keys
ctfshow-web354(SSRF)
比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
为什么这么多人转行产品经理?产品经理发展前景如何?
Docker 安装部署Redis
Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
如何画产品架构图?
Product learning (III) - demand list
[wechat applet] how to build a building block development?
SQL learning notes nine connections 2
Is it safe to buy funds on Alipay? Where can I buy funds
SQL learning notes 2
女生适合学产品经理吗?有什么优势?
ESP32 ESP-IDF ADC监测电池电压(带校正)
Introduction to spark (one article is enough)
软件工程复习
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
Chinese explanation of common rclone subcommands