如何在随机生成的六边形地图中找到岛屿?

发布于 2024-08-02 10:50:09 字数 399 浏览 3 评论 0原文

我正在 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 技术交流群。

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

发布评论

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

评论(5

孤独难免 2024-08-09 10:50:09

这个问题有一个标准名称,但在我的脑海中,以下可能会起作用:

  • 随机选择任何图块,为
  • 为其
  • 它的邻居着色,
  • 邻居的邻居着色
  • ,为邻居的邻居着色,等等。

当你完成后(即当所有邻居都着色时),循环遍历所有图块的列表以查看是否有任何仍然/未着色的(如果是这样,它们是一个岛屿)。

There's a standard name for this problem but off the top of my head the following might work:

  • Pick any tile at random
  • Color it
  • Color its neighbours
  • Color its neighbours' neighbours
  • Color its neighbours' neighbours' neighbours, etc.

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).

情话已封尘 2024-08-09 10:50:09

你如何进行随机生成?或许这个时候最好的办法就是解决。当你生成地图时,如果你发现你刚刚创建的地图无法到达,你可以通过添加适当的元素来解决。

不过我们需要知道你是如何进行这一代的。

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.

迷你仙 2024-08-09 10:50:09

这是从随机图块开始的基本深度优先遍历,用类似 python 的语言进行伪编码:

visited = set()
queue = Queue()
r = random tile
queue.add(r)
while not queue.empty():
    current = queue.pop()
    visited.add(current)
    for neighbor in current.neighbors():
        if neighbor not in visited:
            queue.add(neighbor)
if visited == set(all tiles):
    print "No islands"
else:
    print "Island starting at ", r

Here's your basic depth-first traversal starting at a random tile, pseudo-coded in python-like language:

visited = set()
queue = Queue()
r = random tile
queue.add(r)
while not queue.empty():
    current = queue.pop()
    visited.add(current)
    for neighbor in current.neighbors():
        if neighbor not in visited:
            queue.add(neighbor)
if visited == set(all tiles):
    print "No islands"
else:
    print "Island starting at ", r
尛丟丟 2024-08-09 10:50:09

这有望提供另一种解决方案。我使用术语“断开连接的组件”而不是“岛屿”,因为它只影响所有图块是否可以从所有其他组件到达(如果存在断开连接的组件,那么如果玩家自己的领土都在一个组件中,则玩家无法通过征服获胜) 。

  • 迭代所有“土地”图块(很容易做到),并为每个图块生成图中的一个节点。
  • 对于每个顶点,用无向边将其连接到代表其相邻图块的顶点(最多 6 个)。
  • 选择任何顶点并从中运行深度优先搜索(或面包优先)。
  • 如果 DFS 找到的顶点集合等于所有顶点的集合,则不存在断开组件,否则存在断开组件(岛)。

(我认为)这应该在 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).

  • Iterate over all 'land' tiles (easy enough to do) and for each tile generate a node in a graph.
  • For each vertex, join it with an undirected edge to the vertices representing its neighbour tiles (maximum of 6).
  • Pick any vertex and run depth-first search (or bread-first) from it.
  • If the set of vertices found by the DFS is equal to the set of all vertices then there are no disconnected components, otherwise a disconnected component (island) exists.

This should (I think) run in time O(n) where n is the number of land tiles.

无尽的现实 2024-08-09 10:50:09

对您的数据集运行模糊内核。

将六角网格视为图像(它是某种图像)

值(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.

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