11

編集:フォーマットが正しくなるように、サンプルマップをコードブロックにラップしました。

わかりました。六角形のグリッド上に非常に単純なA*アルゴリズムを書き込もうとしています。私は理解しており、A*部分を行うことができます。実際、私のA*は正方形のグリッドで機能します。頭を悩ませることができないのは、六角形の隣人を見つけることです。六角形グリッドのレイアウトは次のとおりです

0101     0301
    0201      0401
0102     0302
    0202      0402

などなど

ですから、私が助けを必要としているのは、六角形のクラスを作成することです。このクラスは、16進座標であるため、ネイバーのリストを生成できます。A *が並べて配置された複数のマップを追跡する方法であるため、グリッドから「落ちる」ネイバー(20x20グリッドの0000または2101など)を生成できる必要があります。したがって、このコードスニペットで機能するものは次のとおりです。

planet = Hex( '0214')print(planet.neighbors())['Hex 0213'、'Hex 0215'、'Hex 0115'、'Hex 0315'、'Hex 0116'、'Hex 0316']

4

2 に答える 2

9

それはあなたがあなたの六角形のタイルの座標をどのように定義するかに依存します。

どれどれ。

  ,   ,   ,   ,
 / \ / \ / \ / \
| A1| A2| A3| A4|
 \ / \ / \ / \ /
  | B1| B2| B3|
 / \ / \ / \ / \
| C1| C2| C3| C4|
 \ / \ / \ / \ /
  '   '   '   '

この場合、ネイバーの定義は偶数行と奇数行で異なります。

Yが偶数であるセル(X、Y)の場合、隣接セルは(X、Y-1)、(X + 1、Y-1)、(X-1、Y)、(X + 1、Y)です。 、(X、Y + 1)、(X + 1、Y + 1)

Yが奇数であるセル(X、Y)の場合、近傍は次のようになります:(X-1、Y-1)、(X、Y-1)、(X-1、Y)、(X + 1、Y) 、(X-1、Y + 1)、(X、Y + 1)

于 2011-07-12T08:22:46.510 に答える
2

上記の私のコメントによると、これが私が実装したコードです。私がそれをきれいにするのを手伝う提案を持っている人は誰でも、私はフィードバックを歓迎します。

class Hexagon():
"""Implements a class of hexagon from a hex map which is vertically tiled.
This hexagon is able to return a list of it's neighbors. It does not care 
if the neighbors are hexes which actually exist on the map or not, the map is
responsible for determining that."""

def __init__(self,grid_number):
    self.name = grid_number
    self.x = int(grid_number[0:2])
    self.y = int(grid_number[2:4])

def neighbors(self):
    ret_list = []
    if self.x % 2 == 0:
        temp_list = [[self.x,self.y-1],
              [self.x-1,self.y],  [self.x+1,self.y],
              [self.x-1,self.y+1],[self.x+1,self.y+1],
                    [self.x,self.y+1]]
        for i in temp_list:
            ret_list.append(format(i[0],'02d') + format(i[1],'02d'))

    elif self.x % 2 == 1:
        temp_list = [[self.x,self.y-1],
              [self.x-1,self.y-1],[self.x+1,self.y-1],
              [self.x-1,self.y],[self.x+1,self.y],
                    [self.x,self.y+1]]
        for i in temp_list:
            ret_list.append(format(i[0],'02d') + format(i[1],'02d'))

    return ret_list

def main():
    hex1 = Hexagon('0201')
    hex2 = Hexagon('0302')
    if hex1.neighbors() == ['0200','0101','0301','0102','0302','0202']:
        print("Works for even columns.")
    else:
        print("Failed for even columns.")
        print(hex1.neighbors())

    if hex2.neighbors() == ['0301','0201','0401','0202','0402','0303']:
        print("Works for odd columns.")
    else:
        print("Failed for odd columns.")
        print(hex2.neighbors())

if __name__ == '__main__':
    main()
于 2011-07-17T04:10:55.627 に答える