- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C语言实现扫雷游戏(可展开)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下 。
先理清游戏大概需要实现的功能,菜单功能的实现、棋盘初始化、打印棋盘、布置雷等。运用两个数组,一个放入布置雷的信息,另一个放入排查雷的信息。选一个坐标扫雷,坐标有雷则游戏结束,没有就计算选中坐标的周围8个格子中雷的总数放入选中的坐标中,若选中的坐标周围8个格子中都没有雷则自动展开。考虑到棋盘边框的情况,实际数组要比打印出的棋盘多两行两列。下面是宏定义和函数声明:
ROW、COL为打印行、列,ROWS、COLS为实际的数组行列 EASY_COUNT为雷的个数,可根据需要调整行列和雷的个数 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.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);
void
ExcludeMine(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
x,
int
y);
|
游戏实现的大致思路体现和菜单的实现,代码如下:
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
|
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void
menu()
{
printf
(
"##############################\n"
);
printf
(
"###### 1. play 0.exit ######\n"
);
printf
(
"##############################\n"
);
}
//布置雷 - 字符组存储 - 雷用1表示,非雷用0表示 - 最外层一圈放0
//排查雷 - 为避免歧义,再用一个字符组存储排查出来的雷的信息 - 未排除的用#表示
//最外层加一圈字符,只在中间设置雷,并打印展示棋盘中间位置,因此实际存放数组要比打印的棋盘多两行两列
void
game()
{
//雷的信息存储
//1.布置好的雷的信息
char
mine[ROWS][COLS] = { 0 };
//2.排查出的雷的信息
char
show[ROWS][COLS] = { 0 };
//初始化
InitBoard(mine, ROWS, COLS,
'0'
);
InitBoard(show, ROWS, COLS,
'#'
);
//打印棋盘
//DisplayBoard(mine, ROW, COL);//测试使用
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();
printf
(
"将返回主菜单\n"
);
Sleep(5 * 1000);
break
;
case
0:
printf
(
"退出游戏\n"
);
break
;
default
:
printf
(
"选择错误,请重新选择\n"
);
break
;
}
}
while
(input);
}
int
main()
{
test();
return
0;
}
|
存放函数的源文件需要引用头文件 。
1
2
|
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
|
1.初始化棋盘 。
1
2
3
4
5
6
7
8
9
10
11
12
|
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;
}
}
}
|
2.棋盘打印 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
void
DisplayBoard(
char
board[ROWS][COLS],
int
row,
int
col)
{
int
i = 0;
int
j = 0;
//打印列号
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++)
{
printf
(
"%c "
, board[i][j]);
}
printf
(
"\n"
);
}
}
|
3.布置雷 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
void
SetMine(
char
board[ROWS][COLS],
int
row,
int
col)
{
int
count = EASY_COUNT;
while
(count)
{
int
x =
rand
() % row + 1;
//1-9
int
y =
rand
() % col + 1;
if
(board[x][y] ==
'0'
)
{
board[x][y] =
'1'
;
count--;
}
}
}
|
4.查找雷和胜负判断 。
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
|
int
CheckShow(
char
show[ROWS][COLS],
int
row,
int
col)
{
int
win = 0;
int
i = 0;
int
j = 0;
for
(i = 1; i <= row; i++)
{
for
(j = 1; j <= col; j++)
{
if
(show[i][j] ==
'#'
)
win++;
}
}
return
win;
}
void
FindMine(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
row,
int
col)
{
int
x = 0;
int
y = 0;
int
win = 0;
//9*9 - 10 = 71
while
(1)
{
printf
(
"请输入排查雷的坐标:>"
);
scanf
(
"%d%d"
, &x, &y);
if
(x >= 1 && x <= row && y >= 1 && y <= col)
{
//坐标合法
//1.踩雷
if
(mine[x][y] ==
'1'
)
{
printf
(
"很遗憾,你被炸死了\n"
);
DisplayBoard(mine, row, col);
break
;
}
else
//不是雷
{
//计算x,y坐标周围有几个雷
ExcludeMine(mine, show, x, y);
DisplayBoard(show, row, col);
win = CheckShow(show, row, col);
if
(win == EASY_COUNT)
break
;
}
}
else
{
printf
(
"坐标非法,请重新输入!\n"
);
}
}
if
(win == EASY_COUNT)
{
printf
(
"恭喜你,排雷成功\n"
);
DisplayBoard(mine, row, col);
}
}
|
5.扫雷的展开和提醒 。
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
|
//'1' - '0' = 1
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
ExcludeMine(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
x,
int
y)
{
int
count = get_mine_count(mine, x, y);
if
(count != 0)
{
show[x][y] = count +
'0'
;
}
else
{
show[x][y] =
' '
;
if
(show[x - 1][y] ==
'#'
)
ExcludeMine(mine, show, x - 1, y);
if
(show[x - 1][y - 1] ==
'#'
)
ExcludeMine(mine, show, x - 1, y - 1);
if
(show[x][y - 1] ==
'#'
)
ExcludeMine(mine, show, x, y - 1);
if
(show[x + 1][y - 1] ==
'#'
)
ExcludeMine(mine, show, x + 1, y - 1);
if
(show[x + 1][y] ==
'#'
)
ExcludeMine(mine, show, x + 1, y);
if
(show[x + 1][y + 1] ==
'#'
)
ExcludeMine(mine, show, x + 1, y + 1);
if
(show[x][y + 1] ==
'#'
)
ExcludeMine(mine, show, x, y + 1);
if
(show[x - 1][y + 1] ==
'#'
)
ExcludeMine(mine, show, x - 1, y + 1);
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/Flora_26/article/details/114266700 。
最后此篇关于C语言实现扫雷游戏(可展开)的文章就讲到这里了,如果你想了解更多关于C语言实现扫雷游戏(可展开)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
1)扫雷 简介 扫雷 是一种经典的单人电脑游戏,最初由微软公司在 1990 年代开发并内置在 Windows 操作系统中。游戏的目标是在一个由方块组成的网格上揭开所有非地雷的方块,而不触发地雷。每个
我正在制作 MineSweeper 的 Angular 版本,但我遇到了事件问题。我的意思是,我有这样的字段组件: import {Component, Input, OnInit} from '@a
我正在尝试制作一个简单的扫雷器,在 n*n 板上埋下 n*n/3 个地雷。地雷用*标记,空格用0标记。(它还不能作为游戏运行:我正在尝试制作扫雷的“答卷”)请注意,我还没有使用过任何有目的的方法。 我
我必须制作一个扫雷 GUI,但我不知道如何使每个按钮都有自己的 mouseAdapter。当我单击某个按钮时,它会更改屏幕上的每个按钮,而不仅仅是我单击的按钮。我可以提供一些关于如何执行此操作的指示吗
我想制作一款扫雷游戏。首先,这适用于按钮。我想我将研究二维数组,并且会有类似 boolean 数组来显示炸弹在哪里,例如,如果 booleanArray[0][4] 为 true,则有炸弹。 现在,我
我遇到了这个奇怪的问题,但我似乎无法弄清楚。我有一个按钮,上面写着“新游戏”。在我玩一次游戏后,然后按“新游戏”返回到 initGame() ,我遇到一个问题,因为我无法右键单击前一个游戏的地雷单元格
我现在面临一个问题,我认为我的 main 正在一遍又一遍地执行一个方法,而不是一次。如果我根据一个例子来解释一下就更好了。我已经能够编写扫雷游戏了。但我把这一切都写在一个 MAIN 类中。这次我尝试再
几个月来我一直在尝试创建自己的扫雷游戏(为了好玩)。唯一让我真正困惑的是如何使用递归函数(flood-fill)来填充游戏中的空白区域。 洪水填充仅部分起作用。它不会从任何单击的节点延伸到右侧或底部节
我正在尝试在 C++11 中使用 Qt 编写扫雷程序。 如果我按下一个带有 0 个炸弹的按钮,我想检查这个按钮周围的按钮以及它们是否也有 0 个炸弹。如果他们有 0 颗炸弹,我想检查按钮。 (图:红色
我想做的是能够“转动”每个相邻的 0,就像普通的扫雷游戏一样。 #include #include #include #include #include int buscamina(); u
有没有办法让某些事件 Action 特定于鼠标左键和右键单击? 我正在创建一个扫雷器图形用户界面,所以当左键单击一个方 block 时,它会被发现,而当它被右键单击时,它会被标记。 我不确定如何从语法
我正在尝试制作一个控制台版本的扫雷。 尽管我目前很努力,但我无法弄清楚扫雷的“洪水填充”部分,如果所选方格的周围邻居不包含炸弹,我们现在必须检查这些相邻方格以找到相邻方格炸弹。 下面的代码适用于所选方
我是论坛新手,因此如果这不符合该网站的标准,我想表示歉意,但是我对 2D 阵列扫雷程序有疑问。 我试图在棋盘上随机放置地雷,棋盘是通过数组构建的。不幸的是,地雷被随机放置在对角线上,而不是整个棋盘上。
我是 Java 编程新手,想寻求您的帮助。我正在尝试使用 Java 开发一个简单的扫雷游戏。但是,我不断收到错误“线程“main”java.lang.ArrayIndexOutOfBoundsExce
我在这段代码上工作了几个小时,终于接近完成了。一旦我弄清楚如何添加通过右键单击标记地雷的功能,我将添加一个地雷计数器和一个计时器。我了解到 JButton 无法识别右键单击,因此我必须使用 Mouse
for (var i = 0; i < 10; i++) { bommen[i] = [ Math.floor(Math.random() * 10), Math.floor(
我正在完成大学的一项作业,我们获得了扫雷游戏的源代码,并且我们有一定的实现要求。其中之一是,所有游戏都是在用户登录后从磁盘读取的,用户可以随时保存游戏进度。我一直在阅读序列化来保存/加载游戏进度,但我
我正在尝试用 Java 制作类似扫雷的游戏,并且我已经完成了其中的大部分工作。我需要帮助的是 FloodFill - http://en.wikipedia.org/wiki/Flood_fill .
我正在尝试为我正在制作的扫雷游戏制作一个函数。该函数的目的是根据给定的 x 和 y(它所在的位置)显示一个元素。这可能不是实现它的最优雅的方法,但我正在为每个标题为 newField 的图 block
这款游戏是扫雷。我试图实现的人工智能将采用已经在计算机上运行的扫雷游戏实例(本例为 Windows 7),获取窗口的矩形尺寸,计算窗口的位置,将其推送到前台在屏幕上,单击左上角的方 block ,然后
我是一名优秀的程序员,十分优秀!