当前位置:网站首页>C语言实现扫雷游戏
C语言实现扫雷游戏
2022-07-24 05:16:00 【王红花x】
一、游戏逻辑
在开始之前,我们要理清游戏的逻辑。
场景布置
游戏首先给我们一个方阵,当我们点击其中一个格子的时候,这个格子会显示周围有几个雷,我们可以使用一个二维数组来存放雷的信息,用0表示不是雷,1表示雷。这样,当我们点击一个格子时,我们只用检测它周围1的个数,就可以展示它有几个雷了,但是这时我们会遇到2个问题
1、检测出来的雷的信息放到哪里,比如检测到一个雷,如果存放到存放雷的二维数组中,那当点击与这个格子相邻的格子时,这个存放的1无法辨别到底时存了一个雷,还是它周围有一个雷。
所以我们需要2个同样大小的二维数组,一个存放雷的信息,一个存放用户知道的信息。


2、当我们点击边上的格子,很明显我们不能检测其周围8个格子,因为会越界,这时候有2种办法,其一,我们可以设置其检测的范围,其二,我们可以将数组扩大一圈。这里我们采用第二种方法。

有了棋盘,我们就可以将它们初始化,初始化之后用随机数生成器布置好雷,舞台就基本布置好了。
排雷
接下来我们开始排雷,首先我们需要玩家给我们一个坐标,来排查雷,查雷的时候我们先看这个位置是不是雷,是的话游戏结束,不是的话就显示周围有几个雷;
具体的代码实现以及一些细节都放到下面了,game.h中存放的是函数的声明以及宏,test.c是主程序的逻辑实现,game.c是游戏函数的具体实现。
二、完整代码
game.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW + 2
#define COLS COL + 2
#define EASY 10
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void InitializeBoard(char board[ROWS][COLS], int row, int col, char ch);
void DisplayBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col, int count);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col, int *pwin);test.c
#include "game.h"
void menu()
{
printf("---------------------------\n");
printf("-----1.play 0.exit-----\n");
printf("---------------------------\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
InitializeBoard(mine, ROWS, COLS, '0');
InitializeBoard(show, ROWS, COLS, '*');
SetMine(mine, ROW, COL, EASY);
DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
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;
}game.c
#include "game.h"
void InitializeBoard(char board[ROWS][COLS], int row, int col, char ch)
{
int i = 0;
int j = 0;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
board[i][j] = ch;
}
}
}
void DisplayBoard(char board[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++)
{
if (board[i][j] == '0')
printf(" ");
else
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("-----扫雷游戏-----\n");
}
void SetMine(char board[ROWS][COLS], int row, int col, int count)
{
int i = 0;
int j = 0;
while (count)
{
i = rand() % row + 1;
j = rand() % col + 1;
if (board[i][j] == '0')
{
board[i][j] = '1';
count--;
}
}
}
void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int row, int col, int *pwin)
{
if (show[x][y] == '*')
{
int i, j, count = 0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if (mine[x + i][y + j] == '1')
count++;
}
}
show[x][y] = count + '0';
(*pwin)++;
if (show[x][y] == '0')
{
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if (x + i >= 1 && x + i <= row && y + j >= 1 && y + j <= col)
GetMineCount(mine, show, x + i, y + j, row, col, pwin);
}
}
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y;
int win = 0;
while (win < row * col - EASY)
{
printf("请输入排查雷的坐标:");
scanf("%d%d", &x, &y);
system("cls");
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("你输入的坐标为(%d,%d),很遗憾,你被炸死了!\n", x, y);
int i, j;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (mine[i][j] == '1')
{
show[i][j] = '!';
}
}
}
DisplayBoard(show, row, col);
break;
}
else
{
if (show[x][y] != '*')
{
printf("该坐标已经被排查过!\n");
}
else
{
GetMineCount(mine, show, x, y, row, col, &win);
DisplayBoard(show, row, col);
}
}
}
else
{
printf("坐标输入不合法!\n");
}
if (win == row * col - EASY)
{
printf("恭喜你,排雷成功!\n");
DisplayBoard(mine, row, col);
}
}
}边栏推荐
- 【深度学习】(三)图像分类
- Web development
- 【Pytorch】conv2d torchvision.transforms
- Tips for using the built-in variable props of BeanShell
- Career planning route
- String的字符串常量池和intern()详解
- Binary SCA fingerprint extraction black Technology: go language Reverse Technology
- Pointer learning diary (III)
- 文本摘要 ACL2021
- Support complex T4 file systems such as model group monitoring and real-time alarm. e
猜你喜欢

PXE efficient batch network installation

Tips for using BeanShell built-in variable prev

1. Pedestrian recognition based on incremental occlusion generation and confrontation suppression

安装Pytorch+anaconda+cuda+cudnn

thread

c2-随机产生函数种子seed、numpy.random.seed()、tf.random.set_seed学习+转载整理
![Embedded system transplantation [2] - Construction of cross development environment](/img/96/8d209c04e41675fc0efaa872c35615.png)
Embedded system transplantation [2] - Construction of cross development environment

Drools development decision table

Using a* heuristic search to solve maze routing problem

T 11-20
随机推荐
手写orm框架
使用swagger2markup生成API文档
On the dilemma faced by non transferable reputation points NFT SBTS
明星逆市入局的NFT,如何能走出独立行情?
Basic knowledge of MySQL database
Optional consistency
Teach you how to weld CAD design board bottom (for beginners) graphic tutorial
线程的介绍
Handwritten ORM framework
Blue Bridge Cup 31 day sprint 21 day (C language)
C#表格数据去重
[basic 8] - processes and threads
scikit-learn笔记
The network NN can calculate the NTCP provided by the host system
OSS文件上传
C语言进阶篇 七.程序的编译和预处理
Read the summary of "machine learning - Zhou Zhihua"
ssm的整合
【【【递归】】】
MySQL insight