- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C++学习心得之扫雷游戏由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例为大家分享了C++实现扫雷游戏的具体代码,供大家参考,具体内容如下 。
1、序言 。
创建一个9*9有10个雷的扫雷游戏 文章的顺序是按照我当时的编程顺序写的,顺便写下我当初的一点思路,总的代码在文章最后,前面的都是分散的函数,有需要的朋友直接复制最后的 。
2、创建 。
创建一个头文件,一个放游戏的程序,一个放运行测试的程序 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>//生成随机数
#include<stdio.h>
#include<time.h>//生成时间戳
#define ROW 9//行数
#define COL 9//列数
#define ROWS ROW+2
#define COLS COL+2
#define EASY 10//雷数
void
InitBoard(
char
board[ROWS][COLS],
int
rows,
int
cols,
char
set);
//初始函数
void
DisplayBoard(
char
board[ROWS][COLS],
int
row,
int
col);
//展示函数
void
SetBoard(
char
board[ROWS][COLS],
int
row,
int
col);
//造雷函数
void
CheckBoard(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
row,
int
col);
//扫描函数
|
3、选择界面 。
进入游戏,可能出现情况三种,分别是退出,游戏和输入错误 也许有人一局完了还想玩,所以要设置循环,写完代码可以运行一下,避免最后出bug范围太大 。
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
|
#include"game.h"
void
test()
{
int
input = 0;
do
{
menu();
printf
(
"input choice:>"
);
scanf
(
"%d"
, &input);
switch
(input)
{
case
0:
printf
(
"over\n"
);
break
;
case
1:
game();
break
;
default
:
printf
(
"input wrong\n"
);
break
;
}
}
while
(input);
}
void
menu()
{
printf
(
"*********************\n"
);
printf
(
"******* 1.game ******\n"
);
printf
(
"****** 0.over ******\n"
);
printf
(
"*********************\n"
);
}
int
main()
{
test();
return
0;
}
|
4、游戏部分 。
1、声明变量和初始化 。
建立存储扫雷的元素的数组,这里咱们可以设置两个字符形数组,一个是标识着炸弹‘1'的mine数组,一个是用来给玩家展示的show数组 虽然是99的大小,但是在之后要由电脑扫描咱们选中点周围的区域,如果数组为9行9列,电脑在扫描最外面一行时就跟中间的部分不一样了,为了方便,咱们建立1111的数组 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
void
game()
{
srand
((unsigned)
time
(NULL));
//这里使用了time.h制造时间戳,以便随机生成数
char
mine[ROWS][COLS];
char
show[ROWS][COLS];
InitBoard(mine, ROWS, COLS,
'0'
);
InitBoard(show, ROWS, COLS,
'*'
);
DisplayBoard(show, ROW, COL);
SetBoard(mine, ROW, COL);
CheckBoard(mine, show, ROW, COL);
}
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、展示函数 。
申明和定义好变量,肯定要让玩家看到游戏盘的变化情况才能玩,所以写一个展示函数 mine数组中炸弹用‘1'来表示,不是炸弹用‘0'表示,show数组中我们用‘*'表示一个区域,然后选中的点要是周围无炸弹,就是‘ ',否则就标识出周围的炸弹数.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
void
DisplayBoard(
char
board[ROWS][COLS],
int
row,
int
col)
{
int
i = 0;
int
j = 0;
printf
(
"----------------------------"
);
printf
(
"\n"
);
for
(i = 0; i <= row; 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"
);
}
printf
(
"----------------------------"
);
printf
(
"\n"
);
}
|
3、造炸弹 。
这里咱们在头文件定义炸弹数,以后想玩多点炸弹,修改一个数就行,方便快捷 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
void
SetBoard(
char
board[ROWS][COLS],
int
row,
int
col)
{
int
x = 0;
int
y = 0;
int
num = EASY;
while
(num)
{
x =
rand
() % ROW + 1;
y =
rand
() % COL + 1;
if
(board[x][y] ==
'0'
)
{
board[x][y] =
'1'
;
num--;
}
}
}
|
4、扫描函数 。
进入游戏,玩家只有选择了要检查的点才能继续,这里有三种情况,因为要有很多次选择,所以采用循环 (1)选中雷区,那么直接跳出循环,游戏结束 (2)没选中雷区,电脑会扫描周围的区域,把周围无雷的点展开,展开周围有雷的点 这里还要说一下mine数组为什么要用‘0'和‘1'来做标记,因为0和1这两个字符在ascII码表里是连续的,一会在电脑扫描周围时可以直接通过减法算出周围的雷数 。
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
|
void
CheckBoard(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
row,
int
col)
{
int
ret = 0;
int
x = 0;
int
y = 0;
int
num = 0;
while
(ret < ROW*COL - EASY)
{
printf
(
"输入排查坐标\n"
);
scanf
(
"%d%d"
, &x, &y);
if
(x > 0 && x <= row && y > 0 && y <= col)
{
if
(mine[x][y] ==
'1'
)
//选中雷区,游戏结束
{
printf
(
"炸死\n"
);
DisplayBoard(mine, row, col);
//展示mine区域
break
;
//跳出循环
}
else
//没踩中雷区
{
ZeroLine(mine, show, x, y);
//展开周围的区域
DisplayBoard(show, row, col);
ret++;
}
}
else
{
printf
(
"input wrong\n"
);
}
}
if
(ret == ROW * COL - EASY)
{
printf
(
"胜利\n"
);
}
}
void
ZeroLine(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
x,
int
y)
{
int
ret = 0;
ret = AroundNum(mine, x, y);
//扫描函数,扫描该点周围雷数
if
(x >= 0 && y >= 0 && x < ROWS && y < COLS)
{
if
(ret == 0)
{
show[x][y] =
' '
;
//无雷则为空白
if
(mine[x][y + 1] ==
'0'
&& show[x][y + 1] ==
'*'
)
{
ZeroLine(mine, show, x, y + 1);
}
if
(mine[x][y - 1] ==
'0'
&& show[x][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x, y - 1);
}
if
(mine[x - 1][y] ==
'0'
&& show[x - 1][y] ==
'*'
)
{
ZeroLine(mine, show, x - 1, y);
}
if
(mine[x + 1][y] ==
'0'
&& show[x + 1][y] ==
'*'
)
{
ZeroLine(mine, show, x + 1, y);
}
if
(mine[x + 1][y + 1] ==
'0'
&& show[x + 1][y + 1] ==
'*'
)
{
ZeroLine(mine, show, x + 1, y + 1);
}
if
(mine[x - 1][y - 1] ==
'0'
&& show[x - 1][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x - 1, y - 1);
}
if
(mine[x + 1][y - 1] ==
'0'
&& show[x + 1][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x + 1, y - 1);
}
if
(mine[x - 1][y + 1] ==
'0'
&& show[x - 1][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x - 1, y + 1);
}
}
else
{
show[x][y] = ret +
'0'
;
}
}
}
int
AroundNum(
char
mine[ROWS][COLS],
int
x,
int
y)
{
return
mine[x - 1][y - 1] + mine[x][y - 1] + mine[x - 1][y] +
mine[x + 1][y + 1] + mine[x][y + 1] + mine[x + 1][y] +
mine[x - 1][y + 1] + mine[x + 1][y - 1] - 8 * mine[x][y];
}
|
5、总代码 。
1、头文件 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY 10
void
InitBoard(
char
board[ROWS][COLS],
int
rows,
int
cols,
char
set);
void
DisplayBoard(
char
board[ROWS][COLS],
int
row,
int
col);
void
SetBoard(
char
board[ROWS][COLS],
int
row,
int
col);
void
CheckBoard(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
row,
int
col);
|
2、游戏部分 。
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
|
#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;
printf
(
"----------------------------"
);
printf
(
"\n"
);
for
(i = 0; i <= row; 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"
);
}
printf
(
"----------------------------"
);
printf
(
"\n"
);
}
void
SetBoard(
char
board[ROWS][COLS],
int
row,
int
col)
{
int
x = 0;
int
y = 0;
int
num = EASY;
while
(num)
{
x =
rand
() % ROW + 1;
y =
rand
() % COL + 1;
if
(board[x][y] ==
'0'
)
{
board[x][y] =
'1'
;
num--;
}
}
}
int
AroundNum(
char
mine[ROWS][COLS],
int
x,
int
y)
{
return
mine[x - 1][y - 1] + mine[x][y - 1] + mine[x - 1][y] +
mine[x + 1][y + 1] + mine[x][y + 1] + mine[x + 1][y] +
mine[x - 1][y + 1] + mine[x + 1][y - 1] - 8 * mine[x][y];
}
void
ZeroLine(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
x,
int
y)
{
int
ret = 0;
ret = AroundNum(mine, x, y);
if
(x >= 0 && y >= 0 && x < ROWS && y < COLS)
{
if
(ret == 0)
{
show[x][y] =
' '
;
if
(mine[x][y + 1] ==
'0'
&& show[x][y + 1] ==
'*'
)
{
ZeroLine(mine, show, x, y + 1);
}
if
(mine[x][y - 1] ==
'0'
&& show[x][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x, y - 1);
}
if
(mine[x - 1][y] ==
'0'
&& show[x - 1][y] ==
'*'
)
{
ZeroLine(mine, show, x - 1, y);
}
if
(mine[x + 1][y] ==
'0'
&& show[x + 1][y] ==
'*'
)
{
ZeroLine(mine, show, x + 1, y);
}
if
(mine[x + 1][y + 1] ==
'0'
&& show[x + 1][y + 1] ==
'*'
)
{
ZeroLine(mine, show, x + 1, y + 1);
}
if
(mine[x - 1][y - 1] ==
'0'
&& show[x - 1][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x - 1, y - 1);
}
if
(mine[x + 1][y - 1] ==
'0'
&& show[x + 1][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x + 1, y - 1);
}
if
(mine[x - 1][y + 1] ==
'0'
&& show[x - 1][y - 1] ==
'*'
)
{
ZeroLine(mine, show, x - 1, y + 1);
}
}
else
{
show[x][y] = ret +
'0'
;
}
}
}
void
CheckBoard(
char
mine[ROWS][COLS],
char
show[ROWS][COLS],
int
row,
int
col)
{
int
ret = 0;
int
x = 0;
int
y = 0;
int
num = 0;
while
(ret < ROW*COL - EASY)
{
printf
(
"输入排查坐标\n"
);
scanf
(
"%d%d"
, &x, &y);
if
(x > 0 && x <= row && y > 0 && y <= col)
{
if
(mine[x][y] ==
'1'
)
{
printf
(
"炸死\n"
);
DisplayBoard(mine, row, col);
break
;
}
else
{
ZeroLine(mine, show, x, y);
DisplayBoard(show, row, col);
ret++;
}
}
else
{
printf
(
"input wrong\n"
);
}
}
if
(ret == ROW * COL - EASY)
{
printf
(
"胜利\n"
);
}
}
|
3、检测部分 。
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
|
#include"game.h"
void
menu()
{
printf
(
"*********************\n"
);
printf
(
"******* 1.game ******\n"
);
printf
(
"****** 0.over ******\n"
);
printf
(
"*********************\n"
);
}
void
game()
{
srand
((unsigned)
time
(NULL));
char
mine[ROWS][COLS];
char
show[ROWS][COLS];
InitBoard(mine, ROWS, COLS,
'0'
);
InitBoard(show, ROWS, COLS,
'*'
);
DisplayBoard(show, ROW, COL);
SetBoard(mine, ROW, COL);
CheckBoard(mine, show, ROW, COL);
}
void
test()
{
int
input = 0;
do
{
menu();
printf
(
"input choice:>"
);
scanf
(
"%d"
, &input);
switch
(input)
{
case
0:
printf
(
"over\n"
);
break
;
case
1:
game();
break
;
default
:
printf
(
"input wrong\n"
);
break
;
}
}
while
(input);
}
int
main()
{
test();
return
0;
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.
原文链接:https://blog.csdn.net/Samature/article/details/103731803 。
最后此篇关于C++学习心得之扫雷游戏的文章就讲到这里了,如果你想了解更多关于C++学习心得之扫雷游戏的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 9 年前。 Improve
介绍篇 什么是MiniApis? MiniApis的特点和优势 MiniApis的应用场景 环境搭建 系统要求 安装MiniApis 配置开发环境 基础概念 MiniApis架构概述
我正在从“JavaScript 圣经”一书中学习 javascript,但我遇到了一些困难。我试图理解这段代码: function checkIt(evt) { evt = (evt) ? e
package com.fastone.www.javademo.stringintern; /** * * String.intern()是一个Native方法, * 它的作用是:如果字
您会推荐哪些资源来学习 AppleScript。我使用具有 Objective-C 背景的传统 C/C++。 我也在寻找有关如何更好地开发和从脚本编辑器获取更快文档的技巧。示例提示是“查找要编写脚本的
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
关闭。这个问题不符合 Stack Overflow guidelines 。它目前不接受答案。 想改善这个问题吗?更新问题,以便堆栈溢出为 on-topic。 6年前关闭。 Improve this
我是塞内加尔的阿里。我今年60岁(也许这是我真正的问题-笑脸!!!)。 我正在学习Flutter和Dart。今天,我想使用给定数据模型的列表(它的名称是Mortalite,请参见下面的代码)。 我尝试
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题? Update the question所以它是on-topic对于堆栈溢出。 9年前关闭。 Improve this que
学习 Cappuccino 的最佳来源是什么?我从事“传统”网络开发,但我对这个新框架非常感兴趣。请注意,我对 Objective-C 毫无了解。 最佳答案 如上所述,该网站是一个好地方,但还有一些其
我正在学习如何使用 hashMap,有人可以检查我编写的这段代码并告诉我它是否正确吗?这个想法是有一个在公司工作的员工列表,我想从 hashMap 添加和删除员工。 public class Staf
我正在尝试将 jQuery 与 CoffeScript 一起使用。我按照博客中的说明操作,指示使用 $ -> 或 jQuery -> 而不是 .ready() 。我玩了一下代码,但我似乎无法理解我出错
还在学习,还有很多问题,所以这里有一些。我正在进行 javascript -> PHP 转换,并希望确保这些做法是正确的。是$dailyparams->$calories = $calories;一条
我目前正在学习 SQL,以便从我们的 Magento 数据库制作一个简单的 RFM 报告,我目前可以通过导出两个查询并将它们粘贴到 Excel 模板中来完成此操作,我想摆脱 Excel 模板。 我认为
我知道我很可能会因为这个问题而受到抨击,但没有人问,我求助于你。这是否是一个正确的 javascript > php 转换 - 在我开始不良做法之前,我想知道这是否是解决此问题的正确方法。 JavaS
除了 Ruby-Doc 之外,哪些来源最适合获取一些示例和教程,尤其是关于 Ruby 中的 Tk/Tile?我发现自己更正常了 http://www.tutorialspoint.com/ruby/r
我只在第一次收到警告。这正常吗? >>> cv=LassoCV(cv=10).fit(x,y) C:\Python27\lib\site-packages\scikit_learn-0.14.1-py
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我是一名优秀的程序员,十分优秀!