当前位置:网站首页>扫雷游戏
扫雷游戏
2022-07-23 19:03:00 【51CTO】
game.h文件
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 4
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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);
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
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;
int
j
=
0;
for (
i
=
0;
i
<
rows;
i
++)
{
for (
j
=
0;
j
<
cols;
j
++)
{
board[
i][
j]
=
set;
}
}
}
void
DisplayBoard(
char
board[
ROWS][
COLS],
int
row,
int
col)
//打印扫雷区
{
int
i
=
0;
int
j
=
0;
for (
i
=
0;
i
<=
col;
i
++)
//打印列号
{
printf(
"%-2d ",
i);
}
printf(
"\n");
for (
i
=
1;
i
<=
row;
i
++)
{
printf(
"%-2d ",
i);
//打印行号
for (
j
=
1;
j
<=
col;
j
++)
{
printf(
"%-2c ",
board[
i][
j]);
}
printf(
"\n");
}
}
void
SetMine(
char
board[
ROWS][
COLS],
int
row,
int
col)
//布置雷
{
int
count
=
EASY_COUNT;
while(
count)
{
int
x
=
rand()
%
row
+
1;
int
y
=
rand()
%
col
+
1;
if (
board[
x][
y]
==
'0')
{
board[
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
Expand(
char
mine[
ROWS][
COLS],
char
show[
ROWS][
COLS],
int
x,
int
y)
//递归排雷
{
if (
x
==
0
||
y
==
0
||
x
==
ROWS
-
1
||
y
==
COLS
-
1)
return ;
if (
show[
x][
y]
!=
'*')
return ;
int
count
=
get_mine_count(
mine,
x,
y);
if (
count
!=
0)
{
show[
x][
y]
=
count
+
'0';
}
else
{
show[
x][
y]
=
' ';
/*Expand(mine, show, x - 1, y);
Expand(mine, show, x - 1, y - 1);
Expand(mine, show, x, y - 1);
Expand(mine, show, x + 1, y - 1);
Expand(mine, show, x + 1, y);
Expand(mine, show, x + 1, y + 1);
Expand(mine, show, x, y + 1);
Expand(mine, show, x - 1, y + 1);*/
int
i
=
0;
int
j
=
0;
for (
i
=
-
1;
i
<=
1;
i
++)
{
for (
j
=
-
1;
j
<=
1;
j
++)
{
Expand(
mine,
show,
x
+
i,
y
+
j);
}
}
}
}
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 (
show[
x][
y]
!=
'*')
{
printf(
"此位置已经排除,请输入其他位置\n");
}
else
{
if (
mine[
x][
y]
==
'1')
{
printf(
"很遗憾,你被炸死了\n");
DisplayBoard(
mine,
ROW,
COL);
break;
}
else
{
Expand(
mine,
show,
x,
y);
DisplayBoard(
show,
ROW,
COL);
int
i
=
0;
int
j
=
0;
int
win1
=
0;
for (
i
=
1;
i
<=
row;
i
++)
{
for (
j
=
1;
j
<=
col;
j
++)
{
if (
show[
i][
j]
!=
'*')
{
win1
++;
}
}
}
win
=
win1;
}
}
}
else
{
printf(
"输入坐标非法,请重新输入!\n");
}
}
if (
win
==
row
*
col
-
EASY_COUNT)
{
printf(
"恭喜你,排雷成功\n");
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
test.c文件
#define _CRT_SECURE_NO_WARNINGS 1
#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 };
InitBoard(
mine,
ROWS,
COLS,
'0');
InitBoard(
show,
ROWS,
COLS,
'*');
DisplayBoard(
show,
ROW,
COL);
SetMine(
mine,
ROW,
COL);
//DisplayBoard(mine, ROW, COL);
FindMine(
mine,
show,
ROW,
COL);
}
void
test()
{
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);
}
int
main()
{
test();
return
0;
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
边栏推荐
- 2022/7/21 training summary
- Set asp Net MVC site default page is the specified page
- [ar learning] - II. Environment construction
- 能量原理与变分法笔记12:最小势能原理
- 21. Mix in details
- Energy principle and variational method note 16: solution of virtual displacement principle
- R language uses dwilcox function to generate Wilcoxon rank sum statistical distribution density function data, and uses plot function to visualize Wilcoxon rank sum statistical distribution density fu
- Leetcode 219. 存在重复元素 II(可以,已解决)
- 3D point cloud course (VI) -- 3D target detection
- osgearth2.8编译siverlining云效果
猜你喜欢

能量原理与变分法笔记19:最小余能原理+可能功原理

【ASP.NET Core】选项模式的相关接口

如何给电脑系统重置系统?方法其实很简单

【AR学习】-- 二、 环境搭建

使用Jmeter和VisualVW进行压测准备

Osgearth uses sundog's Triton ocean and silverlining cloud effects

攻防世界web题-fakebook

Leetcode 228. summary interval (yes, solved)

Robot decision-making system based on self-learning (daki technology, Zhao kaiyong)

Leetcode 219. 存在重复元素 II(可以,已解决)
随机推荐
How important is 5g dual card and dual access?
Leetcode 219. 存在重复元素 II(可以,已解决)
osgearth2.8编译siverlining云效果
Typescript use of new data type symbol
线性代数行列式计算方法之降阶法
Leetcode 238. product of arrays other than itself
Edge cloud | 1. overview
Osgearth2.8 compiling silvering cloud effect
搭建自己的目标检测环境,模型配置,数据配置 MMdetection
Leetcode 238. product of arrays other than itself
能量原理與變分法筆記19:最小餘能原理+可能功原理
干货!神经网络中的隐性稀疏正则效应
QT下assimp库的模型加载
3D point cloud course (VII) -- feature point description
使用多态时,判断能否向下转型的两种思路
Osgearth uses sundog's Triton ocean and silverlining cloud effects
Leetcode 152. product maximum subarray (brute force cracking can actually pass!)
Robot decision-making system based on self-learning (daki technology, Zhao kaiyong)
百度地图数据可视化
2022/7/22 训练日志