1

I'm trying to compute the manhattan distance for an 8-puzzle with A*. I realize that I'm doing something wrong with how I compare the current positions with the goal, but I can't figure exactly what I'm doing wrong (I'm pretty new to python). Please help.

 def manhattan_distance(self, goal):
  dist = 0
  x = 0
  y = 0
  goal = [(0, 0), (1, 0), (2, 0),
          (0, 1), (1, 1), (2, 1),
          (0, 2), (1, 2), (2, 2)]
      for row in range(len(self.board)):
        for col in range(len(self.board)):
           val = self.board[row][col]
           if val != 0:
             for x, y in goal:
               dist += abs(row - x) + abs(col - y)
  return dist

I'm not getting any error message, it just keeps searching through nodes.

  • Your distance doesn’t use `val` beyond checking if the space is empty. But you haven’t shown anything that could loop forever. – Davis Herring Sep 22 '19 at 13:58
  • 1
    You are using the same range for `row` and `col` loops. If the board is always square, then this is not a problem. – bfris Sep 22 '19 at 15:09

0 Answers0