如何在 C++ 中创建单元格或网格 随机迷宫?

发布于 2024-07-11 02:00:12 字数 250 浏览 6 评论 0原文

我正在尝试用 C++ 创建一个随机迷宫,但无法开始,因为我不知道如何创建网格或单元格。 我怎样才能创建它? 我还想使用 ASCII 字符创建它。 我怎样才能将它存储在数组中? (任何人都可以给出示例代码和一些解释,以便我更好地理解它)

另一个问题:我应该学习和使用哪些数据结构? 我打算使用 Eller 算法或 Kruskal 算法。

谢谢你们帮助我! 我是一名初级程序员,我想了解这一点,因为这是我项目的一部分,非常感谢您!

I'm trying to create a randomized maze in C++, but I can't start because I don't know how to create grids or cells. How could I create it? And I also want to create it using ASCII characters. how can i store it in array? (can any one give a sample code and some explanation so i can understand it better)

Another question: What data stuctures should I need to learn and use? I'm planning to use Eller's algorithm or Kruskal's algorithm.

Thank you guys for helping me! im a begginer programmer, and i want to learn about this, because this is a part of my project, thank you vary much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风渺 2024-07-18 02:00:12

您是否在寻找迷宫生成算法 (更多)? 您的问题是算法还是图形?

典型的算法的工作原理是将迷宫中的每个“单元”视为图的顶点,从所有“墙”开始,并删除与生成树相对应的一组墙。 (因此,为了随机化它,许多人从随机权重开始并找到最小生成树。)至少对于小型迷宫,您不需要任何特殊的数据结构来表示单元格; 您可以将每个单元格视为一对 (x,y) (其坐标)。 并且您也不需要任何数据结构(邻接矩阵/邻接列表)来存储图的边,因为 (x,y) 的邻居只是 (x,y ±1)(x±1,y)(忽略那些落在边界之外的)。

无论如何,一旦有了生成树,您就可以准确地知道哪些墙“存在”,哪些墙“不存在”,因此您对迷宫有了完整的描述。 如果你要画迷宫,你就知道要画哪些迷宫。

要使用 ASCII 字符进行绘制,只需逐行遍历每一行:绘制“上墙”(如果 (x,y)--” code> 和 (x,y+1) 存在),然后绘制实际行(如果 (x,y) 之间有墙,则绘制“|(x+1,y) 存在)。 最后画出底部边界。

Are you looking for Maze generation algorithms (more)? Is your problem with the algorithms, or graphics?

The typical algorithms work by considering each "cell" in the maze as a vertex of a graph, start with all "walls", and remove a set of walls that corresponds to a spanning tree. (So in order to randomize it, many of them start with random weights and find the minimum spanning tree.) For small mazes at least, you don't need any special data structure to represent the cells; you can just think of each cell as a pair (x,y) (its coördinates). And you don't need any data structure (adjacency matrix / adjacency list) to store the edges of the graph either, because the neighbours of (x,y) are just (x,y±1) and (x±1,y) (ignoring those that fall outside the boundaries).

In any case, once you have the spanning tree, you know exactly which of the walls "exist" and which do not, so you have a complete description of the maze. If you're going to draw the maze, you know which ones to draw.

To draw with ASCII characters, you just pass through each row one by one: draw the "upper walls" (put a "--" if the wall between (x,y) and (x,y+1) exists), then draw the actual row (put a "|" if the wall between (x,y) and (x+1,y) exists). Finally draw the bottom boundary.

青巷忧颜 2024-07-18 02:00:12

您可能想将迷宫存储在二维字符数组中。 您可以在 C++ 中声明一个数组,无论是否对其进行初始化。

char a[30][10];  // declares a char array of 30 rows and 10 columns.

// declare an array with 3 rows and 3 columns, and provide initial values
char ticTacToeBoard[3][3] = {{'x', 'x', 'o'},
                             {'o', 'o', 'x'},
                             {'x', 'o', ' '}
                            };

您可以将迷宫中墙壁的初始值更改为 '|''-',并使用空格字符 ' ',用于通道。
两种初始化方法都有效,但您始终以相同的方式使用元素。 以下是如何清除上面初始化数组中的棋盘。

// clear the board
for (int row=0; row<3; row++) {
    for (int col=0; col<3; col++) {
        ticTacToeBoard[row][col] = ' ';
    }
}

如果您想读取某个元素的值(当您尝试在迷宫中导航时很有用),您可以使用与设置其值时相同的下标表示法。

char y = a[2][2]; // reads the character in row 2, column 2

You probably want to store your maze in a 2-dimension char array. You can declare an array with or without initializing it in C++.

char a[30][10];  // declares a char array of 30 rows and 10 columns.

// declare an array with 3 rows and 3 columns, and provide initial values
char ticTacToeBoard[3][3] = {{'x', 'x', 'o'},
                             {'o', 'o', 'x'},
                             {'x', 'o', ' '}
                            };

You could change the initial values to '|' and '-' for walls in your maze, and use a space character, ' ', for the passageways.
Either initialization method works, but you always use the elements the same way. Here's how you clear the board in the initialized array above.

// clear the board
for (int row=0; row<3; row++) {
    for (int col=0; col<3; col++) {
        ticTacToeBoard[row][col] = ' ';
    }
}

If you want to read the value of an element (useful when you're trying to navigate a maze), you use the same subscript notation as when you're setting its value.

char y = a[2][2]; // reads the character in row 2, column 2
百善笑为先 2024-07-18 02:00:12

垂直墙:|
水平。 Wall:_

如果您使用固定宽度字体:

 _____
| |  _
|_  | |
 __ | |
|_____|

我不确定该怎么做,但这就是我要开始的地方。

确定网格上的起点和终点的位置。 然后,使用您想要的任何曲线创建一条路径。 基本上,它应该是随机移动,每次检查这条路径是否还有办法到达终点。 然后,从此路径中移除一定数量的墙壁,并从这些孔中创建其他路径。 继续此操作,直到用完可用空间。 然后,也许可以确保没有创建更短的路径。 如果有,请将其阻止。

Vertical Wall: |
Horiz. Wall: _

If you're using fixed-width fonts:

 _____
| |  _
|_  | |
 __ | |
|_____|

I'm not sure exactly what to do, but here's where I'd start.

Determine where on your grid the start and end points will be. Then, create a single path, with whatever squiggles you want. Basically, it should be random movement, checking each time that there is still a way for this path to reach the end. Then, remove a certain amount of walls from this path, and create other paths from these holes. Continue this until you run out of empty space. Then, perhaps, ensure that no shorter paths have been created. If they have, block those up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文