当前位置:网站首页>扫雷游戏
扫雷游戏
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.
边栏推荐
- Leetcode 216. combined sum III
- 2022山东养老展,中国国际养老服务业展览会,济南老龄产业展
- R language uses the ggarrange function of ggpubr package to combine multiple images, and uses the ggexport function to save the visual images in BMP format (width parameter specifies width, height par
- task03笔记2
- Relevant interfaces of [asp.net core] option mode
- 梅科尔工作室-华为14天鸿蒙设备开发实战笔记六
- 20.ref与props
- Energy principle and variational method note 14: summary + problem solving
- 能量原理與變分法筆記19:最小餘能原理+可能功原理
- 【无标题】
猜你喜欢

17. Life cycle

能量原理与变分法笔记17:广义变分原理(识别因子方法)

Energy principle and variational method note 16: solution of virtual displacement principle

Hongke dry goods | teaches you how to parse floating-point data in MODBUS

Leetcode 151. invert words in strings

MySQL master-slave replication

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

Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
![[development experience] development project trample pit collection [continuous update]](/img/02/7bea3bf09e9a27b6ab74399639f197.png)
[development experience] development project trample pit collection [continuous update]

TASK03|回归
随机推荐
17.生命周期
Redux summation case explanation version tutorial
能量原理與變分法筆記19:最小餘能原理+可能功原理
Set asp Net MVC site default page is the specified page
Huawei cloud stack [interview]
Win11小组件怎么添加待办事项?Win11添加待办事项小组件的方法
21. Mix in details
Meiker Studio - Huawei 14 day Hongmeng equipment development practical notes 4
[interview: concurrent Article 22 multithreading: reentrantlock]
Attack and defense world web question Fakebook
能量原理与变分法笔记15:微元法的求解
安装Win11找不到固态硬盘如何解决?
Leetcode 152. 乘积最大子数组(暴力破解居然可以通过!)
Leetcode 238. product of arrays other than itself
JDK installation package and MySQL installation package sorting
如何给电脑系统重置系统?方法其实很简单
2022上半年中国十大收缩行业
Failure after reinstalling the system (error: Reboot and select proper boot device or insert boot media in selected boot device)
深入浅出边缘云 | 1. 概述
White paper on adaptive robot interaction