3
3
from queue import Queue
4
4
5
5
6
+ # Chebyshev distance
7
+ def chebyshev (node1 : object , node2 : object ):
8
+ """
9
+ Calculate the Chebyshev distance between two nodes.
10
+
11
+ Parameters:
12
+ node1 (Node): The first node.
13
+ node2 (Node): The second node.
14
+
15
+ Returns:
16
+ int: The Chebyshev distance between the two nodes.
17
+ """
18
+ x1 , y1 = node1 .get_pos ()
19
+ x2 , y2 = node2 .get_pos ()
20
+
21
+ return max (abs (x1 - x2 ), abs (y1 - y2 ))
22
+
23
+
6
24
# Manhattan distance
7
25
def manhattan (node1 : object , node2 : object ):
8
26
"""
@@ -53,6 +71,8 @@ def heuristic(type: str, node1: object, node2: object):
53
71
float: The heuristic distance between the two nodes.
54
72
"""
55
73
match type :
74
+ case "chebyshev" :
75
+ return chebyshev (node1 , node2 )
56
76
case "euclidean" :
57
77
return euclidean (node1 , node2 )
58
78
case "manhattan" :
@@ -66,7 +86,7 @@ def reconstruct_path(came_from: object, current: object, draw: object):
66
86
Reconstructs the path from the start node to the end node in a maze.
67
87
68
88
Parameters:
69
- came_from (Dict[Node, Node]): A dictionary containing the nodes traversed
89
+ came_from (Dict[Node, Node]): A dictionary containing the nodes traversed
70
90
during the pathfinding algorithm.
71
91
current (Node): The end node of the path.
72
92
draw (function): A function for drawing the maze.
@@ -109,7 +129,7 @@ def get_unvisited_nodes(start: object):
109
129
110
130
def check (events : list , run : bool ):
111
131
"""
112
- Check for and handle user events such as quitting the program or
132
+ Check for and handle user events such as quitting the program or
113
133
pausing the algorithm.
114
134
115
135
Parameters:
@@ -124,9 +144,10 @@ def check(events: list, run: bool):
124
144
pygame .quit ()
125
145
if event .type == pygame .KEYDOWN and event .key == pygame .K_s :
126
146
run = False
127
-
147
+
128
148
return run
129
149
150
+
130
151
def markup (draw : object , current : object ):
131
152
"""
132
153
Mark the current node as visited and redraw the grid.
@@ -144,4 +165,4 @@ def markup(draw: object, current: object):
144
165
draw ()
145
166
146
167
if not current .is_start () and not current .is_end ():
147
- current .check ()
168
+ current .check ()
0 commit comments