- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章C++实现随机生成迷宫地牢由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
可以用这个地图核心做成一个无限迷宫类的游戏 。
main.cpp 。
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
// Author: FreeKnight 2014-09-02
#include "stdafx.h"
#include <iostream>
#include <string>
#include <random>
#include <cassert>
/*
简单逻辑流程描述:
将整个地图填满土
在地图中间挖一个房间出来
选中某一房间(如果有多个的话)的墙壁
确定要修建某种新元素
查看从选中的墙延伸出去是否有足够的空间承载新的元素
如果有的话继续,不然就返回第 3 步
从选中的墙处增加新的元素
返回第 3 步,直到地牢建设完成
在地图的随机点上安排上楼和下楼的楼梯
最后,放进去怪兽和物品
*/
//-------------------------------------------------------------------------------
// 暂时支持的最大的地图块个数
#define MAX_TILES_NUM 10000
// 房间的大小
#define MAX_ROOM_WIDTH 8
#define MAX_ROOM_HEIGHT 8
#define MIN_ROOM_WIDTH 4
#define MIN_ROOM_HEIGHT 4
// 房间和走廊的合计最大个数
#define DEFAULT_FEATURE_NUM 1000
// 尝试生成房间和走廊的测试次数(即步长)
#define MAX_TRY_TIMES 1000
// 默认创建房间的概率(100-该值则为创建走廊的概率)
#define DEFAULT_CREATE_ROOM_CHANCE 70
// 走廊长度
#define MIN_CORRIDOR_LEN 3
#define MAX_CORRIDOR_LEN 6
//-------------------------------------------------------------------------------
// 格子块
enum
class
Tile
{
Unused,
// 没用的格子(土块)
DirtWall,
// 墙壁
DirtFloor,
// 房间地板
Corridor,
// 走廊
Door,
// 房门
UpStairs,
// 入口
DownStairs
// 出口
};
//-------------------------------------------------------------------------------
// 朝向
enum
class
Direction
{
North,
// 北
South,
// 南
East,
// 东
West,
// 西
};
//-------------------------------------------------------------------------------
class
Map
{
public
:
Map():
xSize(0), ySize(0),
data() { }
// 构造函数,全屏填土
Map(
int
x,
int
y, Tile value = Tile::Unused):
xSize(x), ySize(y),
data(x * y, value) { }
// 填充某块类型
void
SetCell(
int
x,
int
y, Tile celltype)
{
assert
(IsXInBounds(x));
assert
(IsYInBounds(y));
data[x + xSize * y] = celltype;
}
// 获取某块类型
Tile GetCell(
int
x,
int
y)
const
{
assert
(IsXInBounds(x));
assert
(IsYInBounds(y));
return
data[x + xSize * y];
}
// 设置一块区域为指定类型块
void
SetCells(
int
xStart,
int
yStart,
int
xEnd,
int
yEnd, Tile cellType)
{
assert
(IsXInBounds(xStart) && IsXInBounds(xEnd));
assert
(IsYInBounds(yStart) && IsYInBounds(yEnd));
assert
(xStart <= xEnd);
assert
(yStart <= yEnd);
for
(auto y = yStart; y != yEnd + 1; ++y)
{
for
(auto x = xStart; x != xEnd + 1; ++x)
{
SetCell(x, y, cellType);
}
}
}
// 判断一块是否在有效范围内
bool
IsXInBounds(
int
x)
const
{
return
x >= 0 && x < xSize;
}
// 判断一块是否在有效范围内
bool
IsYInBounds(
int
y)
const
{
return
y >= 0 && y < ySize;
}
// 判断一个区域是否已被使用过
bool
IsAreaUnused(
int
xStart,
int
yStart,
int
xEnd,
int
yEnd)
{
assert
(IsXInBounds(xStart) && IsXInBounds(xEnd));
assert
(IsYInBounds(yStart) && IsYInBounds(yEnd));
assert
(xStart <= xEnd);
assert
(yStart <= yEnd);
for
(auto y = yStart; y != yEnd + 1; ++y)
{
for
(auto x = xStart; x != xEnd + 1; ++x)
{
if
(GetCell(x, y) != Tile::Unused)
{
return
false
;
}
}
}
return
true
;
}
// 判断一个地图块周围是否临接某种地图块
bool
IsAdjacent(
int
x,
int
y, Tile tile)
{
assert
(IsXInBounds(x - 1) && IsXInBounds(x + 1));
assert
(IsYInBounds(y - 1) && IsYInBounds(y + 1));
return
(GetCell(x - 1, y) == tile || GetCell(x + 1, y) == tile ||
GetCell(x, y - 1) == tile || GetCell(x, y + 1) == tile);
}
// 输出地图
void
Print()
const
{
for
(auto y = 0; y != ySize; y++)
{
for
(auto x = 0; x != xSize; x++)
{
switch
(GetCell(x, y))
{
case
Tile::Unused:
std::cout <<
" "
;
break
;
case
Tile::DirtWall:
std::cout <<
"#"
;
break
;
case
Tile::DirtFloor:
std::cout <<
"_"
;
break
;
case
Tile::Corridor:
std::cout <<
"."
;
break
;
case
Tile::Door:
std::cout <<
"+"
;
break
;
case
Tile::UpStairs:
std::cout <<
"<"
;
break
;
case
Tile::DownStairs:
std::cout <<
">"
;
break
;
};
}
std::cout << std::endl;
}
std::cout << std::endl;
}
private
:
// 地图总宽高
int
xSize, ySize;
// 全部地图块数据
std::vector<Tile> data;
};
//-------------------------------------------------------------------------------
class
DungeonGenerator
{
public
:
int
m_nSeed;
// 随机数种子
int
m_nXSize, m_nYSize;
// 地图最大宽高
int
m_nMaxFeatures;
// 房间和走廊的最大个数
int
m_nChanceRoom;
// 创建房间的概率【0,100】
int
m_nChanceCorridor;
// 创建走廊的概率【0,100】 该概率+创建房间的概率应当 = 100
typedef
std::mt19937 RngT;
public
:
DungeonGenerator(
int
XSize,
int
YSize ):
m_nSeed(std::random_device()()),
m_nXSize( XSize ), m_nYSize( YSize ),
m_nMaxFeatures( DEFAULT_FEATURE_NUM ),
m_nChanceRoom( DEFAULT_CREATE_ROOM_CHANCE )
{
m_nChanceCorridor = 100 - m_nChanceRoom;
}
Map Generate()
{
assert
( m_nMaxFeatures > 0 && m_nMaxFeatures <= DEFAULT_FEATURE_NUM);
assert
( m_nXSize > 3 );
assert
( m_nYSize > 3 );
auto rng = RngT(m_nSeed);
// step1: 满地图填土
auto map = Map(m_nXSize, m_nYSize, Tile::Unused);
MakeDungeon(map, rng);
return
map;
}
private
:
// 获取随机int
int
GetRandomInt(RngT& rng,
int
min,
int
max)
const
{
return
std::uniform_int_distribution<
int
>(min, max)(rng);
}
// 获取随机方向
Direction GetRandomDirection(RngT& rng)
const
{
return
Direction(std::uniform_int_distribution<
int
>(
static_cast
<
int
>(Direction::North),
static_cast
<
int
>(Direction::West) )(rng));
}
// 创建走廊
bool
MakeCorridor(Map& map, RngT& rng,
int
x,
int
y,
int
maxLength, Direction direction)
const
{
assert
(x >= 0 && x < m_nXSize);
assert
(y >= 0 && y < m_nYSize);
assert
(maxLength > 0 && maxLength <= std::max(m_nXSize, m_nYSize));
// 设置走廊长度
auto length = GetRandomInt(rng, MIN_CORRIDOR_LEN, maxLength);
auto xStart = x;
auto yStart = y;
auto xEnd = x;
auto yEnd = y;
if
(direction == Direction::North)
yStart = y - length;
else
if
(direction == Direction::East)
xEnd = x + length;
else
if
(direction == Direction::South)
yEnd = y + length;
else
if
(direction == Direction::West)
xStart = x - length;
// 检查整个走廊是否在地图内
if
(!map.IsXInBounds(xStart) || !map.IsXInBounds(xEnd) || !map.IsYInBounds(yStart) || !map.IsYInBounds(yEnd))
return
false
;
// 检查走廊区域是否有被占用
if
(!map.IsAreaUnused(xStart, yStart, xEnd, yEnd))
return
false
;
map.SetCells(xStart, yStart, xEnd, yEnd, Tile::Corridor);
return
true
;
}
// 创造房间
bool
MakeRoom(Map& map, RngT& rng,
int
x,
int
y,
int
xMaxLength,
int
yMaxLength, Direction direction)
const
{
assert
( xMaxLength >= MIN_ROOM_WIDTH );
assert
( yMaxLength >= MIN_ROOM_HEIGHT );
// 创建的房间最小是4 * 4,随机出房间大小
auto xLength = GetRandomInt(rng, MIN_ROOM_WIDTH, xMaxLength);
auto yLength = GetRandomInt(rng, MIN_ROOM_HEIGHT, yMaxLength);
auto xStart = x;
auto yStart = y;
auto xEnd = x;
auto yEnd = y;
// 根据房间朝向随机出房间起始和终结位置
if
(direction == Direction::North)
{
yStart = y - yLength;
xStart = x - xLength / 2;
xEnd = x + (xLength + 1) / 2;
}
else
if
(direction == Direction::East)
{
yStart = y - yLength / 2;
yEnd = y + (yLength + 1) / 2;
xEnd = x + xLength;
}
else
if
(direction == Direction::South)
{
yEnd = y + yLength;
xStart = x - xLength / 2;
xEnd = x + (xLength + 1) / 2;
}
else
if
(direction == Direction::West)
{
yStart = y - yLength / 2;
yEnd = y + (yLength + 1) / 2;
xStart = x - xLength;
}
// 要保证生成的房间一定四个点都在地图中
if
(!map.IsXInBounds(xStart) || !map.IsXInBounds(xEnd) || !map.IsYInBounds(yStart) || !map.IsYInBounds(yEnd))
return
false
;
// 要保证房间所占用土地未被其他地占用
if
(!map.IsAreaUnused(xStart, yStart, xEnd, yEnd))
return
false
;
// 周围种墙
map.SetCells(xStart, yStart, xEnd, yEnd, Tile::DirtWall);
// 房间内铺地板
map.SetCells(xStart + 1, yStart + 1, xEnd - 1, yEnd - 1, Tile::DirtFloor);
return
true
;
}
// 创建一个房间或者走廊
bool
MakeRoomOrCorridor(Map& map, RngT& rng,
int
x,
int
y,
int
xmod,
int
ymod, Direction direction)
const
{
// 随机选择创建类型(房间或者走廊)
auto chance = GetRandomInt(rng, 0, 100);
if
(chance <= m_nChanceRoom)
{
// 创建房间
if
(MakeRoom(map, rng, x + xmod, y + ymod, MAX_ROOM_WIDTH, MAX_ROOM_HEIGHT, direction))
{
// 当前位置设置门
map.SetCell(x, y, Tile::Door);
// 删除门旁边的墙壁,改建为墙壁
map.SetCell(x + xmod, y + ymod, Tile::DirtFloor);
return
true
;
}
return
false
;
}
else
{
// 创建走廊
if
(MakeCorridor(map, rng, x + xmod, y + ymod, MAX_CORRIDOR_LEN, direction))
{
// 当前位置设置门
map.SetCell(x, y, Tile::Door);
return
true
;
}
return
false
;
}
}
// 对全地图进行随机处理生成房间和走廊
bool
MakeRandomFeature(Map& map, RngT& rng)
const
{
for
( auto tries = 0 ; tries != MAX_TRY_TIMES; ++tries)
{
// 获取一个有意义的地形格
int
x = GetRandomInt(rng, 1, m_nXSize - 2);
int
y = GetRandomInt(rng, 1, m_nYSize - 2);
// 获取一个随机墙壁 或者 走廊
if
(map.GetCell(x, y) != Tile::DirtWall && map.GetCell(x, y) != Tile::Corridor)
continue
;
// 保证该墙壁和走廊不临接门
if
(map.IsAdjacent(x, y, Tile::Door))
continue
;
// 找个临接墙壁或者走廊的格子 创建新房间或者走廊
if
(map.GetCell(x, y+1) == Tile::DirtFloor || map.GetCell(x, y+1) == Tile::Corridor)
{
if
(MakeRoomOrCorridor(map, rng, x, y, 0, -1, Direction::North))
return
true
;
}
else
if
(map.GetCell(x-1, y) == Tile::DirtFloor || map.GetCell(x-1, y) == Tile::Corridor)
{
if
(MakeRoomOrCorridor(map, rng, x, y, 1, 0, Direction::East))
return
true
;
}
else
if
(map.GetCell(x, y-1) == Tile::DirtFloor || map.GetCell(x, y-1) == Tile::Corridor)
{
if
(MakeRoomOrCorridor(map, rng, x, y, 0, 1, Direction::South))
return
true
;
}
else
if
(map.GetCell(x+1, y) == Tile::DirtFloor || map.GetCell(x+1, y) == Tile::Corridor)
{
if
(MakeRoomOrCorridor(map, rng, x, y, -1, 0, Direction::West))
return
true
;
}
}
return
false
;
}
// 随机制作出入口
bool
MakeRandomStairs(Map& map, RngT& rng, Tile tile)
const
{
auto tries = 0;
auto maxTries = MAX_TILES_NUM;
for
( ; tries != maxTries; ++tries)
{
// 随机获取一个非边缘的点
int
x = GetRandomInt(rng, 1, m_nXSize - 2);
int
y = GetRandomInt(rng, 1, m_nYSize - 2);
// 如果周围没有地板并且没有走廊(通路)的话,直接放弃
if
(!map.IsAdjacent(x, y, Tile::DirtFloor) && !map.IsAdjacent(x, y, Tile::Corridor))
continue
;
// 周围不允许有门
if
(map.IsAdjacent(x, y, Tile::Door))
continue
;
map.SetCell(x, y, tile);
return
true
;
}
return
false
;
}
// 随机生成地牢
bool
MakeDungeon(Map& map, RngT& rng)
const
{
// step2 : 在正中间创建一个房间
MakeRoom(map, rng, m_nXSize / 2, m_nYSize / 2, MAX_ROOM_WIDTH, MAX_ROOM_HEIGHT, GetRandomDirection(rng));
for
(auto features = 1; features != m_nMaxFeatures; ++features)
{
if
(!MakeRandomFeature(map, rng))
{
std::cout <<
"生成地牢已满。(当前房间和走廊个数为: "
<< features <<
")."
<< std::endl;
break
;
}
}
// 创建随机入口点
if
(!MakeRandomStairs(map, rng, Tile::UpStairs))
std::cout <<
"创建入口点失败!"
<< std::endl;
// 创建随机出口点
if
(!MakeRandomStairs(map, rng, Tile::DownStairs))
std::cout <<
"创建出口点失败!"
<< std::endl;
return
true
;
}
};
#include <windows.h>
void
ResetConsoleSize()
{
HANDLE
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
// 获取标准输出设备句柄
CONSOLE_SCREEN_BUFFER_INFO bInfo;
// 窗口缓冲区信息
GetConsoleScreenBufferInfo(hOut, &bInfo );
COORD size = {1000, 800};
SetConsoleScreenBufferSize(hOut,size);
// 重新设置缓冲区大小
SMALL_RECT rc = {0,0, 1000-1, 800-1};
// 重置窗口位置和大小
SetConsoleWindowInfo(hOut,
true
,&rc);
}
void
FlushReadme()
{
std::cout<<
"================================="
<< std::endl;
std::cout<<
" < 表示入口 "
<< std::endl;
std::cout<<
" > 表示出口 "
<< std::endl;
std::cout<<
" _ 下划线表示地板 "
<< std::endl;
std::cout<<
" # 表示墙壁 "
<< std::endl;
std::cout<<
" . 点表示走廊 "
<< std::endl;
std::cout<<
" + 表示门 "
<< std::endl;
std::cout<<
"纯黑表示啥都没有,是障碍"
<< std::endl;
std::cout<<
"================================="
<< std::endl;
}
int
main()
{
ResetConsoleSize();
FlushReadme();
DungeonGenerator* pGenerator =
new
DungeonGenerator( 150, 50 );
if
( pGenerator == NULL )
return
-1;
auto map = pGenerator->Generate();
map.Print();
int
n;
std::cin >> n;
}
|
演示图:
以上所述就是本文的全部内容了,希望大家能够喜欢.
最后此篇关于C++实现随机生成迷宫地牢的文章就讲到这里了,如果你想了解更多关于C++实现随机生成迷宫地牢的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!