如何在随机生成的六边形地图中找到岛屿?
我正在 Codigniter 和 JQuery 中编写一个类似风险的游戏。我想出了一种方法来创建随机生成的地图,方法是制作完整的图块布局,然后删除随机的图块。然而,这有时会产生我所说的岛屿。
在危险中,你只能攻击一格。因此,如果一名玩家碰巧拥有一座属于自己的岛屿,他们将永远无法失去。
我正在尝试找到一种方法,可以在游戏开始之前检查地图,看看它是否有岛屿。
我已经想出了一个函数来找出每个空间有多少个相邻空间,但不知道如何实现它来找到岛屿。
每个缺失的点也被识别为“水”。
我不允许使用图像标签: https://i.sstatic.net/DgI7c.gif
I'm programming a Risk like game in Codigniter and JQuery. I've come up with a way to create randomly generated maps by making a full layout of tiles then deleting random ones. However, this sometimes produces what I call islands.
In risk, you can only attack one space over. So if one player happens to have an island all to them self, they would never be able to loose.
I'm trying to find a way that I can check the map before the game begins to see if it has islands.
I have already come up with a function to find out how many adjacent spaces there are to each space, but am not sure how to implement it in order to find islands.
Each missing spot is also identified as "water."
I'm not allowed to use image tags:
https://i.sstatic.net/DgI7c.gif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个问题有一个标准名称,但在我的脑海中,以下可能会起作用:
当你完成后(即当所有邻居都着色时),循环遍历所有图块的列表以查看是否有任何仍然/未着色的(如果是这样,它们是一个岛屿)。
There's a standard name for this problem but off the top of my head the following might work:
When you're done (i.e. when all neighbours are colored), loop through the list of all tiles to see whether there are any still/left uncolored (if so, they're an island).
你如何进行随机生成?或许这个时候最好的办法就是解决。当你生成地图时,如果你发现你刚刚创建的地图无法到达,你可以通过添加适当的元素来解决。
不过我们需要知道你是如何进行这一代的。
How do you do the random generation? Probably the best way is to solve it at this time. When you're generating the map, if you notice that you just created is impossible to get to, you can resolve it by adding the appropriate element.
Though we'll need to know how you do the generation.
这是从随机图块开始的基本深度优先遍历,用类似 python 的语言进行伪编码:
Here's your basic depth-first traversal starting at a random tile, pseudo-coded in python-like language:
这有望提供另一种解决方案。我使用术语“断开连接的组件”而不是“岛屿”,因为它只影响所有图块是否可以从所有其他组件到达(如果存在断开连接的组件,那么如果玩家自己的领土都在一个组件中,则玩家无法通过征服获胜) 。
(我认为)这应该在 O(n) 时间内运行,其中 n 是地块的数量。
This hopefully provides another solution. Instead of "island" I'm using the term "disconnected component" since it only matters whether all tiles are reachable from all others (if there are disconnected components then a player cannot win via conquest if his own territories are all in one component).
This should (I think) run in time O(n) where n is the number of land tiles.
对您的数据集运行模糊内核。
将六角网格视为图像(它是某种图像)
值(x,y)=此(x,y)周围所有图块的平均值
这将稍微侵蚀海滩并消除岛屿。
留给学生的练习是在结果数据集上运行边缘检测内核以填充海滩图块。
Run a blurring kernel over your data set.
treating the hex grid as an image ( it is , sort of)
value(x,y) = average of all tiles around this (x,y)
this will erode beaches slightly, and eliminate islands.
It is left as an exercise for the student to run an edge-detection kernel over the resulting dataset to populate the beach tiles.