简体   繁体   English

算法帮助:构建游戏板,但需要知道何时锁定正方形

[英]Algorithm Help: Building game board, but need to know when square is locked in

I have built a gameboard that consists of a grid, the grid is then randomly assigned, "Walls" to a cell. 我建立了一个由网格组成的游戏板,然后将网格随机分配给“ Walls”到一个单元。 Once the cells are built, how can I check to see if a certain cell is 'locked in' so that I don't place a player there. 构建好单元后,如何检查某个单元是否已“锁定”,这样我就不会再放置播放器了。

I have thought about this and the first ago I came up with check all sides for four walls, but obviously, a cell could be surrounded by open cells, which are then surrounded by walls. 我已经考虑过这一点,在第一年前,我想出了四面都检查墙壁的方法,但是很明显,一个牢房可以被敞开的牢房包围,然后再被墙壁包围。

The other is a "escape to outside" algo, which basically tries to find a path to an outside wall, which would then mean it is not locked in, but if the block is on an outside wall and surrounded by blocks it would be locked in. 另一个是“逃逸到外部”算法,它基本上试图找到通往外墙的路径,这意味着它没有被锁定,但是如果该块位于外墙上并且被块包围,则它将被锁定。在。

How is this typically handled? 通常如何处理? I'm using python if that matters for any code examples. 如果任何代码示例都重要,我将使用python。

Thanks! 谢谢!

You basically want a floodfill algorithm. 您基本上需要一个泛洪算法。
http://en.wikipedia.org/wiki/Floodfill http://en.wikipedia.org/wiki/Floodfill

edit 编辑
I think I misunderstood your definitions of 'locked' and 'escape'. 我想我误解了您对“锁定”和“逃逸”的定义。
If you have limited game board, every cell there is locked in some space. 如果您的游戏板有限,则每个单元格都锁定在某个空间中。 If I understand you correctly, you just want that space to be big enough. 如果我对您的理解正确,那么您只是希望该空间足够大。 Well, you compute its area easily with flood fill algorithm. 好了,您可以使用洪水填充算法轻松计算其面积。

It really depends on the size of your game board. 这实际上取决于游戏板的大小。 If we are speaking of small boards, a quick path finding algorithm can give you the distance between every cell and each other. 如果我们说的是小型电路板,则快速路径查找算法可以为您提供每个单元之间的距离。 This might have a double use if you do not want to place the player too close to other players or other features. 如果您不想将播放器放置在与其他播放器或其他功能过于靠近的位置,则可能有双重用途。

A second option is to design a wall generator that by principle cannot create locked rooms. 第二种选择是设计一种原则上不能创建锁定房间的墙式发生器。 The roguelike community has done a decent amount of research into the topic of generating random (dungeon-sized) maps without closed rooms. 流氓类社区对生成不带封闭房间的随机(地牢大小)地图的主题进行了大量研究

Finally, using a level of detail approach can help you if you have to find a suitably large empty space on a huge map. 最后,如果您必须在巨大的地图上找到适当的大空白空间,则使用详细程度的方法可以为您提供帮助。 Find an interconnection graph for a set of sparse points distributed over your map (this can be a direct result of your map generator). 查找分布在地图上的一组稀疏点的互连图(这可能是地图生成器的直接结果)。 That should be sufficient to place a player. 这足以放置一个玩家。 But if you need more detail on a specific point, finding a path to one of these points can tell you if a room is locked in. This might not work in extremely dense labyrinths. 但是,如果您需要有关特定点的更多详细信息,则找到通往这些点之一的路径可以告诉您房间是否被锁定。在迷宫般的密集迷宫中,这可能不起作用。

I'm not sure exactly what your game board can look like, but if you have something like this: 我不确定您的游戏板到底是什么样子,但是如果您有这样的事情:

+----------------------+
|      +-----+         |
|      | c   |         |
|      |     |         |
|      +-----+         |
|                      |
|                      |
|                      |
+----------------------+

And you want to avoid putting the character on the c because it's 'walled in', even though it's it's not exactly surrounded by the walls, you could easily implement the Left-or-Right Hand algorithm - only instead of trying to get out of the maze you're checking to see if you make it back to the same coordinate. 而且,您希望避免将字符放到c上,因为它已“围入”,即使它没有被墙壁完全包围,您也可以轻松实现左右手算法 -只是尝试摆脱这种情况您正在检查的迷宫,以查看是否使其回到相同的坐标。

Maybe this previous post is what you are looking for. 也许以前的帖子就是您想要的。 In that post, I answered how to count the number of different areas of dots there was on a grid on which dots could be placed. 在那篇文章中,我回答了如何计算可以放置点的网格上点的不同区域的数量。 Your problem is similar, except instead of having "dots", you have walls. 您的问题是相似的,除了有“点”而不是墙。 The algorithm given in that post will give you a version of your grid G, where 该帖子中给出的算法将为您提供网格G的版本,其中

G[x1][y1] == G[x2][y2]

only if it is possible for a player to get from (x1,y1) to (x2,y2), ie, if there is a clear path with no walls from that first point to the second point. 仅当玩家有可能从(x1,y1)到达(x2,y2)时,即,从第一个点到第二个点之间没有障碍物的畅通无阻的路径。

Is this what you are looking for? 这是你想要的? Do you want additional details, or do you want me to adapt that code to your particular problem? 您是否需要其他详细信息,还是要我根据您的特定问题修改代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM