메모리에서 16 진 / 16 진 격자를 어떻게 표현합니까?
Catan의 정착민 과 같이 16 진수 그리드로 보드 게임을 만들고 있다고 가정 해보십시오 .
각 정점과 모서리에는 특성 (위의 도로 및 정착지)이있을 수 있습니다.
이 보드를 나타내는 데이터 구조를 어떻게 만들 수 있습니까? 각 타일의 이웃, 가장자리 및 정점에 액세스하기위한 패턴은 무엇입니까?
Amit Patel 이이 주제에 놀라운 페이지를 게시했습니다 . 매우 포괄적이고 훌륭하여이 질문에 대한 결정적인 답이되어야합니다 : 6 각 격자
이러한 그리드는 2 차원 배열로 표현 될 수 있습니다.
만약
2
7 3
1
6 4
5
16 진수 그리드에 이웃이있는 1 번이면 다음과 같이 2D 배열에 넣을 수 있습니다.
2 3
7 1 4
6 5
이 그리드에서 분명히 인접성은 수평 또는 수직으로 인접 할뿐만 아니라 하나의 대각선을 사용하여 결정됩니다.
원하는 경우 그래프를 사용할 수도 있습니다.
이 기사 는 이성질체 / 육각 그리드 게임을 설정하는 방법을 설명합니다. Forcing Isometric and Hexagonal Maps onto a Rectangular Grid
섹션과 이동 섹션을 살펴 보는 것이 좋습니다 . 찾고있는 것과 다르지만 원하는 것을 수행하는 방법을 공식화하는 데 도움이 될 수 있습니다.
나는 육각형을 많이 다루었습니다. 이와 같은 경우 16 진 경계의 6 개 지점 각각을 추적합니다. 이것은 당신이 아주 쉽게 그릴 수 있습니다.
육각형을 나타내는 단일 객체 배열이 있습니다. 이 16 진 객체들 각각에는 또 다른 "변"배열을 가리키는 6 개의 "포인터"(또는 다른 배열에 대한 인덱스)가 있습니다. "꼭지점"도 마찬가지입니다. 물론 정점에는 인접한 육각형에 대한 3 개의 포인터가 있고 측면에는 2가 있습니다.
따라서 16 진수는 X, Y, Point (6), Vertices (6), Sides (6)와 같습니다.
그런 다음 Hex 배열, vertice 배열 및 side 배열이 있습니다.
그런 다음 16 진수 등의 꼭지점 /면을 찾는 것이 매우 간단합니다.
포인터를 말할 때 vertice 또는 side 배열 또는 다른 요소의 요소를 가리키는 정수 일 수 있습니다. 물론 배열은 목록이거나 무엇이든 될 수 있습니다.
2
7 3
1
6 4
5
지도의 행을 '평평하게'하려고 할 수도 있습니다. 이 예에서는 다음과 같습니다.
2
7 1 3
6 5 4
때로는 한 행에 행을 갖는 것이 더 유용합니다.
나는 다음과 같은 것을 제안 할 것이다 (나는 델파이 스타일 선언을 사용할 것이다) :
type
THexEdge = record
Hexes: array[1..2] of Integer; // Index of adjoining hexes.
// Other edge stuff goes here.
end;
THexVertex = record
Hexes: array[1..3] of Integer; // Index of adjoining hexes.
// Other vertex stuff goes here.
end;
THex = record
Edges: array[1..6] of Integer; // Index of edge.
Vertices: array[1..6] of Integer; // Index of vertex.
// Other hex stuff goes here.
end;
var
Edges: array of THexEdge;
Vertices: array of THexVertex;
HexMap: array of THex;
각 육각형에는 6 개의 모서리와 6 개의 정점이 있습니다. 각 모서리는 인접한 2 개의 헥스를 추적하고 각 정점은 인접한 3 개의 헥스를 추적합니다 (지도 가장자리의 육각형은 특별한 경우입니다).
다른 방법으로 할 수있는 일이 많이 있습니다. 배열 대신 포인터를 사용하고 레코드 대신 개체를 사용할 수 있으며 다른 응답자가 제안한대로 16 진수를 2 차원 배열에 저장할 수 있습니다.
바라건대, 그것은 그것에 접근하는 한 가지 방법에 대한 아이디어를 줄 수 있습니다.
우리는 클래스 프로젝트를 위해 Catan AI의 Settlers를 구현 하고이 답변 에서 코드를 수정 하여 정점과 가장자리에 지속적으로 무작위로 액세스 할 수있는 보드를 만들었습니다. 그것은 재미있는 문제 였지만 보드는 많은 시간이 걸렸으므로 누군가가 여전히 간단한 구현을 찾고있는 경우 Python 코드가 있습니다.
class Board:
# Layout is just a double list of Tiles, some will be None
def __init__(self, layout=None):
self.numRows = len(layout)
self.numCols = len(layout[0])
self.hexagons = [[None for x in xrange(self.numCols)] for x in xrange(self.numRows)]
self.edges = [[None for x in xrange(self.numCols*2+2)] for x in xrange(self.numRows*2+2)]
self.vertices = [[None for x in xrange(self.numCols*2+2)] for x in xrange(self.numRows*2+2)]
for row in self.hexagons:
for hexagon in row:
if hexagon == None: continue
edgeLocations = self.getEdgeLocations(hexagon)
vertexLocations = self.getVertexLocations(hexagon)
for xLoc,yLoc in edgeLocations:
if self.edges[xLoc][yLoc] == None:
self.edges[xLoc][yLoc] = Edge(xLoc,yLoc)
for xLoc,yLoc in vertexLocations:
if self.vertices[xLoc][yLoc] == None:
self.vertices[xLoc][yLoc] = Vertex(xLoc,yLoc)
def getNeighborHexes(self, hex):
neighbors = []
x = hex.X
y = hex.Y
offset = 1
if x % 2 != 0:
offset = -1
if (y+1) < len(self.hexagons[x]):
hexOne = self.hexagons[x][y+1]
if hexOne != None: neighbors.append(hexOne)
if y > 0:
hexTwo = self.hexagons[x][y-1]
if hexTwo != None: neighbors.append(hexTwo)
if (x+1) < len(self.hexagons):
hexThree = self.hexagons[x+1][y]
if hexThree != None: neighbors.append(hexThree)
if x > 0:
hexFour = self.hexagons[x-1][y]
if hexFour != None: neighbors.append(hexFour)
if (y+offset) >= 0 and (y+offset) < len(self.hexagons[x]):
if (x+1) < len(self.hexagons):
hexFive = self.hexagons[x+1][y+offset]
if hexFive != None: neighbors.append(hexFive)
if x > 0:
hexSix = self.hexagons[x-1][y+offset]
if hexSix != None: neighbors.append(hexSix)
return neighbors
def getNeighborVertices(self, vertex):
neighbors = []
x = vertex.X
y = vertex.Y
offset = -1
if x % 2 == y % 2: offset = 1
# Logic from thinking that this is saying getEdgesOfVertex
# and then for each edge getVertexEnds, taking out the three that are ==vertex
if (y+1) < len(self.vertices[0]):
vertexOne = self.vertices[x][y+1]
if vertexOne != None: neighbors.append(vertexOne)
if y > 0:
vertexTwo = self.vertices[x][y-1]
if vertexTwo != None: neighbors.append(vertexTwo)
if (x+offset) >= 0 and (x+offset) < len(self.vertices):
vertexThree = self.vertices[x+offset][y]
if vertexThree != None: neighbors.append(vertexThree)
return neighbors
# used to initially create vertices
def getVertexLocations(self, hex):
vertexLocations = []
x = hex.X
y = hex.Y
offset = x % 2
offset = 0-offset
vertexLocations.append((x, 2*y+offset))
vertexLocations.append((x, 2*y+1+offset))
vertexLocations.append((x, 2*y+2+offset))
vertexLocations.append((x+1, 2*y+offset))
vertexLocations.append((x+1, 2*y+1+offset))
vertexLocations.append((x+1, 2*y+2+offset))
return vertexLocations
# used to initially create edges
def getEdgeLocations(self, hex):
edgeLocations = []
x = hex.X
y = hex.Y
offset = x % 2
offset = 0-offset
edgeLocations.append((2*x,2*y+offset))
edgeLocations.append((2*x,2*y+1+offset))
edgeLocations.append((2*x+1,2*y+offset))
edgeLocations.append((2*x+1,2*y+2+offset))
edgeLocations.append((2*x+2,2*y+offset))
edgeLocations.append((2*x+2,2*y+1+offset))
return edgeLocations
def getVertices(self, hex):
hexVertices = []
x = hex.X
y = hex.Y
offset = x % 2
offset = 0-offset
hexVertices.append(self.vertices[x][2*y+offset]) # top vertex
hexVertices.append(self.vertices[x][2*y+1+offset]) # left top vertex
hexVertices.append(self.vertices[x][2*y+2+offset]) # left bottom vertex
hexVertices.append(self.vertices[x+1][2*y+offset]) # right top vertex
hexVertices.append(self.vertices[x+1][2*y+1+offset]) # right bottom vertex
hexVertices.append(self.vertices[x+1][2*y+2+offset]) # bottom vertex
return hexVertices
def getEdges(self, hex):
hexEdges = []
x = hex.X
y = hex.Y
offset = x % 2
offset = 0-offset
hexEdges.append(self.edges[2*x][2*y+offset])
hexEdges.append(self.edges[2*x][2*y+1+offset])
hexEdges.append(self.edges[2*x+1][2*y+offset])
hexEdges.append(self.edges[2*x+1][2*y+2+offset])
hexEdges.append(self.edges[2*x+2][2*y+offset])
hexEdges.append(self.edges[2*x+2][2*y+1+offset])
return hexEdges
# returns (start, end) tuple
def getVertexEnds(self, edge):
x = edge.X
y = edge.Y
vertexOne = self.vertices[(x-1)/2][y]
vertexTwo = self.vertices[(x+1)/2][y]
if x%2 == 0:
vertexOne = self.vertices[x/2][y]
vertexTwo = self.vertices[x/2][y+1]
return (vertexOne, vertexTwo)
def getEdgesOfVertex(self, vertex):
vertexEdges = []
x = vertex.X
y = vertex.Y
offset = -1
if x % 2 == y % 2: offset = 1
edgeOne = self.edges[x*2][y-1]
edgeTwo = self.edges[x*2][y]
edgeThree = self.edges[x*2+offset][y]
if edgeOne != None: vertexEdges.append(edgeOne)
if edgeTwo != None: vertexEdges.append(edgeTwo)
if edgeThree != None: vertexEdges.append(edgeThree)
return vertexEdges
def getHexes(self, vertex):
vertexHexes = []
x = vertex.X
y = vertex.Y
xOffset = x % 2
yOffset = y % 2
if x < len(self.hexagons) and y/2 < len(self.hexagons[x]):
hexOne = self.hexagons[x][y/2]
if hexOne != None: vertexHexes.append(hexOne)
weirdX = x
if (xOffset+yOffset) == 1: weirdX = x-1
weirdY = y/2
if yOffset == 1: weirdY += 1
else: weirdY -= 1
if weirdX >= 0 and weirdX < len(self.hexagons) and weirdY >= 0 and weirdY < len(self.hexagons):
hexTwo = self.hexagons[weirdX][weirdY]
if hexTwo != None: vertexHexes.append(hexTwo)
if x > 0 and x < len(self.hexagons) and y/2 < len(self.hexagons[x]):
hexThree = self.hexagons[x-1][y/2]
if hexThree != None: vertexHexes.append(hexThree)
return vertexHexes
나는 헥스와 함께 "여가 시간에 재미를 위해 코딩"하고 있습니다. 그리고 이것은 다음과 같이 진행됩니다 ... 나는 그것이 어떻게 보이는지 말해 줄 것입니다.
- Hexagon: it has six neighbour hexagons. It can deliver the reference for each neighbouring hex tile. It can tell you what it consists of(water ,rock, dust). It can connect itself to others and vice versa. It can even automatically connect the others surrounding him to create a greater field and or making sure all fields can be adressed by its neighbours.
- A building references up to three roads and three Hex Tiles. They can tell you which they are.
- A road references two hexes and other roads when they are adressed by neighbouring tiles. They can tell which tiles that are and which roads or buildings they connect to.
This is just an idea how I would work on it.
참고URL : https://stackoverflow.com/questions/1838656/how-do-i-represent-a-hextile-hex-grid-in-memory
'IT story' 카테고리의 다른 글
배경 : 없음 대 배경 : 투명 차이점은 무엇입니까? (0) | 2020.07.24 |
---|---|
“pip install --user…”의 목적은 무엇입니까? (0) | 2020.07.24 |
Markdown과 reStructuredText 모두에 동일한 README가 있습니다. (0) | 2020.07.24 |
rreplace-문자열에서 마지막으로 나타나는 표현식을 바꾸는 방법? (0) | 2020.07.24 |
ggplot2에 x 및 y 축 레이블 추가 (0) | 2020.07.24 |