2016-10-24 5 views
0

私が作っているこのプログラムの私の最終的な目標は、A Starアルゴリズムを使ってロボットがグリッド内で目標を見つけられるようにすることです。私はAステージにはまだいません。ロボットの移動方法を理解しようとしていますが、ロボットを現在の位置から次の位置に移動する方法がわかりません(北、南、東、 西)。「ロボット」をx yグリッドで動かす - python

注:まだ移動しているときは、グリッド境界や壁を考慮していません。もし誰かが私がそれに取り組む方法についての良い考えを持っているなら、あなたの知識を共有することを自由にしてください。

また、私はグリッドを印刷することができない、私は頻繁にpythonを使用しないと私は完全にグリッドを印刷する方法がわからない。ロボット、壁、トラップ、またはパスを表す特定の文字で、グリッドをどのように印刷しますか?

ありがとうございます。一般

class robot: 

    def __init__ (self, name, grid, position): 
     self.name = name 
     self.position = position 
     self.grid = grid 

    def robot_position(position): 
     position = (x,y) 
     return robot.position 

    def robot_neighbours(self, position): 
     position = (x,y) 
     results = [(x+1, y), (x, y-1), (x-1, y), (x, y+1)] 
     return results 

    def move_north(): 
     north = (x,y+1) 
     robot.robot_position = north 
     return robot.robot_position 





class game_board: 

    def boundary(self, point): #defines the grids boundaries 
     point = (x,y) 
     return 0 <= x < self.width and 0 <=y < self.height 

    def node_neighbours(self, point): #defines the current nodes neighbours 
     point = (x,y) 
     results = [(x+1, y), (x, y-1), (x-1, y), (x, y+1)] 
     results = filter(self.grid_boundary, results) #filters out the boundary results 
     results = filter(self.can_pass_through, results) #filters out the coordinates that you can pass through from those you can't 
     return results 

    def can_pass_through(self, point): 
     return point not in self.walls 
     return point not in self.traps 

#constructsthe2Dgrid 
    def __init__(self, width, height): 
     self.width = width 
     self.height = height 
     self.name = " . " 
     self.walls = [] 
     self.traps = [] 

    def build_grid(grid): 
     for x in range(grid.width): 
      for y in range (grid.height): 
       print(x, y) 
     return "grid created" + width + " " + height 
+0

あり、あなたのクラスや関数を持ついくつかの問題だと、それはまた、あなたのコード内の他の問題に対処することなく、あなたの質問に対処するために、やや難しいようなものを書くでしょう。おそらく、この問題を最小の部品に分割し、各部品の機能を確認してから次の部品に移行することを検討する必要があります。また、より狭く正確な質問をすることで、人々があなたをより良く助けることができます。 – sytech

答えて

-1

、問題は、それが[]あるべきときに、リストの()を使用していることです。今はタプルを生成していますが、数学演算を行う場合はリストを使用するのが最善です。

def robot_neighbours(self, position): 
    position = [x, y] 
    results = [[x+1, y], [x, y-1], [x-1, y], [x, y+1]] 
    return results` 

希望助け、 Narusan

EDITは:タプルを使用して、それが主な違いは、リストを使用して、position[0]は有効な引数であるとされていますが、コード内のほんの一例

ない。 あなたの運動のために、あなたは

new_pos = [position[0]+1, position[1]] 
+0

ああ。タプルは1つの要素とみなされるので、x、yは分離しないでしょうか?それが意味をなさないならば。もちろんリストには別々の要素があります。 –

+0

この問題を参照してください。[link](http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples) – Narusan

+0

タプルはここで完全に受け入れられます。タプルは不変ですが、OPはこの関数の値を変更する必要はありません。私は 'robot_neighbors'の目的は、グリッド上でそのロボットを囲む位置を返すことだと思います。 'position'がタプルであっても、new_posをそうするのが最も確実です。同様に、OPでは、 'position'もタプルであっても、' results'を問題なくタプルにすることができます。 – sytech

関連する問題