用 Java 表示十六进制映射

发布于 2024-10-27 15:00:16 字数 342 浏览 2 评论 0原文

好吧,我可能只是错过了一些东西,但我正在尝试制作一个六角板地图,就像在文明中一样。这个想法是将每个十六进制作为一个具有各种属性(即颜色、类型等)的对象。然后有一个由所有这些不同的六角形组成的板。有 3 种板类型:矩形、圆形、三角形。

我遇到的问题是如何表示数据类型中的每个十六进制。我认为二维数组是可行的方法,如果我只需要知道十六进制位置本身就可以了。但我需要知道每个六角形的顶点和边缘,以便我可以在这些六角形之间的边缘上添加此内容。这就是让我困惑的地方。如何用所有边和点来表示每个六边形,以便我可以询问连接到该点或边等的邻居。

任何想法,我已经阅读了很多关于此的文章,但由于某种原因我仍然不明白,也许因为这都是想法,我不知道如何实施它。

Ok so I might just be missing something but I am trying to make a hex board map, like in civilizations. The idea is to have each hex as an object that has various properties(ie, color, type, etc). Then have a board of all these various hexes. There would be 3 board types, rectangle, circle, triangle.

The issue I am having is how to represent each hex in a data type. I figured a 2 dimensional array would be the way to go, which would be fine if I just needed to know the hex locations themselves. But I will need to know the vertexes and edges of each hex so that I can do things like on the edge between these to hexes add this. That is what is confusing me. How to represent each hex with all sides and points in an way in which I can ask for neighbors connected to that point or edge etc.

Any ideas, I have read many articles about this, but for some reason I still don't understand, maybe because it is all ideas and I have no clue hoe to implement it.

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

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

发布评论

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

评论(2

腻橙味 2024-11-03 15:00:16

二维数组将强化矩形网格的概念。

为什么不定义一个 Cell 类,并引用相邻单元格:

public class Cell {
   private Cell north;
   private Cell south;
   private Cell northWest;

   // etc.
}

网格边界上的 Cells 的引用将为空。

您必须通过迭代行、创建每个 Cell 并根据需要将其与其邻居绑定来构建网格。

A 2-dimensional array would reinforce the concept of a rectangular grid.

Why not instead define a Cell class, with references to the neighbouring cells thus:

public class Cell {
   private Cell north;
   private Cell south;
   private Cell northWest;

   // etc.
}

The references would be null for Cells on the boundaries of your grid.

You'd have to construct the grid by iterating through the rows, creating each Cell and tying it to its neighbours as appropriate.

天气好吗我好吗 2024-11-03 15:00:16

您可以使用基于二维数组的类以及一些附加方法来计算邻居。

在标准数组中,单元格 E 与 B、D、F 和 H 共享边

+-----+-----+-----+
|     |     |     | 
|  A  |  B* |  C  | 
|     |     |     | 
+-----+-----+-----+
|     |     |     | 
|  D* | *E* |  F* | 
|     |     |     | 
+-----+-----+-----+
|     |     |     | 
|  G  |  H* |  I  | 
|     |     |     | 
+-----+-----+-----+

如果您想象将交替列替换半个单元格;

+-----+     +-----+
|     |     |     | 
|  A  +-----+  C  | 
|     |     |     | 
+-----+  B* +-----+
|     |     |     | 
|  D* +-----+  F* | 
|     |     |     | 
+-----+ *E* +-----+
|     |     |     | 
|  G* +-----+  I* | 
|     |     |     | 
+-----+  H* +-----+
      |     |
      +-----+

现在单元格 E 与 B、D、F、GH I 共享边。事实上,这种排列与六角形网格相同 - 只是六角形变形。如果我的 ASCII 艺术技巧足够的话,这些正方形可以重新绘制为六角形。

当然,您无法物理地替换网格,但您可以提供控制对网格的访问并实现相邻单元格计算的方法。

+-----+-----+-----+
|     |     |     | 
|  A  |  B* |  C  |   B = E - 1xRowLength
|     |     |     |   D = E - 1 
+-----+-----+-----+   F = E + 1
|     |     |     |   G = E + 1xRowLength - 1
|  D* | *E* |  F* |   H = E + 1xRowLength 
|     |     |     |   I = E + 1xRowLength + 1
+-----+-----+-----+
|     |     |     | 
|  G* |  H* |  I* | 
|     |     |     | 
+-----+-----+-----+
hexGrid.listNeighbors(E) = B,D,F,G,H,I
hexGrid.NeighborAbove(E) = B
hexGrid.NeighborAboveLeft(E) = D
hexGrid.NeighborAboveRight(E) = F
hexGrid.NeighborBelowLeft(E) = G
hexGrid.NeighborBelowRight(E) = I
hexGrid.NeighborBelow(E) = H
etc

You can use a class based on a 2D array, with some additional methods to compute neighbors.

In a standard array, cell E shares sides with B, D, F and H

+-----+-----+-----+
|     |     |     | 
|  A  |  B* |  C  | 
|     |     |     | 
+-----+-----+-----+
|     |     |     | 
|  D* | *E* |  F* | 
|     |     |     | 
+-----+-----+-----+
|     |     |     | 
|  G  |  H* |  I  | 
|     |     |     | 
+-----+-----+-----+

If you imagine displacing alternate columns by half a cell;

+-----+     +-----+
|     |     |     | 
|  A  +-----+  C  | 
|     |     |     | 
+-----+  B* +-----+
|     |     |     | 
|  D* +-----+  F* | 
|     |     |     | 
+-----+ *E* +-----+
|     |     |     | 
|  G* +-----+  I* | 
|     |     |     | 
+-----+  H* +-----+
      |     |
      +-----+

Now cell E shares sides with B, D, F, G H I. In fact, this arrangement is the same as for a hex grid - just with misshapen hexes. If my ASCII art skills were up to it, the squares could be redrawn as hexes.

You can't physically displace the grid, of course, but you can provide methods that control access to the grid and implement the neighbor cell calculations.

+-----+-----+-----+
|     |     |     | 
|  A  |  B* |  C  |   B = E - 1xRowLength
|     |     |     |   D = E - 1 
+-----+-----+-----+   F = E + 1
|     |     |     |   G = E + 1xRowLength - 1
|  D* | *E* |  F* |   H = E + 1xRowLength 
|     |     |     |   I = E + 1xRowLength + 1
+-----+-----+-----+
|     |     |     | 
|  G* |  H* |  I* | 
|     |     |     | 
+-----+-----+-----+
hexGrid.listNeighbors(E) = B,D,F,G,H,I
hexGrid.NeighborAbove(E) = B
hexGrid.NeighborAboveLeft(E) = D
hexGrid.NeighborAboveRight(E) = F
hexGrid.NeighborBelowLeft(E) = G
hexGrid.NeighborBelowRight(E) = I
hexGrid.NeighborBelow(E) = H
etc
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文