about summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-10-09 17:20:57 +0700
committerNguyễn Gia Phong <vn.mcsinyx@gmail.com>2019-10-09 17:20:57 +0700
commitb70c00eb8df3c0c8eea19e7d4aeafdeb7b30bef3 (patch)
tree53b03b2f9ad860bf4ffe7eba22de63ab6e91f6e9
parentbb3d4158ca78ac53c68f1c5bca26fcef9e4010e5 (diff)
downloadbrutalmaze-b70c00eb8df3c0c8eea19e7d4aeafdeb7b30bef3.tar.gz
Unnest the queue used for path finding
-rw-r--r--brutalmaze/maze.py21
1 files changed, 6 insertions, 15 deletions
diff --git a/brutalmaze/maze.py b/brutalmaze/maze.py
index e140753..f04b7da 100644
--- a/brutalmaze/maze.py
+++ b/brutalmaze/maze.py
@@ -460,31 +460,22 @@ class Maze:
             self.stepx = self.stepy = 0
             return True
 
-        # Forest Fire algorithm with step count
-        queue = [[(self.destx, self.desty)]]
-        visited, count, distance = set(), 1, 0
-        while count:
-            if not queue[distance]: distance += 1
-            x, y = queue[distance].pop()
-            count -= 1
+        # Forest Fire algorithm
+        queue, visited = deque([(self.destx, self.desty)]), set()
+        while queue:
+            x, y = queue.pop()
             if (x, y) not in visited:
                 visited.add((x, y))
             else:
                 continue
-
             dx, dy = MIDDLE - x, MIDDLE - y
             if dx**2 + dy**2 <= 2:
+                # Succeeded on finding a path
                 self.stepx, self.stepy = dx, dy
                 return False
             for i, j in around(x, y):
                 if self.map[i][j] == EMPTY and check(i, j):
-                    try:
-                        queue[distance + 1].append((i, j))
-                    except IndexError:
-                        queue.append([(i, j)])
-                    finally:
-                        count += 1
-
+                    queue.appendleft((i, j))
         # Failed to find way to move to target
         self.stepx = self.stepy = 0
         return True