remove code duplication, add a_star flag to bfs
This commit is contained in:
parent
cfe68b8e7a
commit
f75a5a68d5
@ -6,7 +6,13 @@ from src.tile import Tile
|
||||
from .helpers import successor, get_path_actions
|
||||
|
||||
|
||||
def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, start_direction: int):
|
||||
def breadth_first_search(
|
||||
field: List[List[Tile]],
|
||||
start_x: int,
|
||||
start_y: int,
|
||||
start_direction: int,
|
||||
a_star: bool = False
|
||||
):
|
||||
explored = []
|
||||
node_queue = queue.Queue()
|
||||
node_queue.put(Node(start_x, start_y, start_direction))
|
||||
@ -15,7 +21,7 @@ def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, st
|
||||
node = node_queue.get()
|
||||
|
||||
if field[node.y][node.x].mine:
|
||||
return get_path_actions(node)
|
||||
return get_path_actions(node) if not a_star else node.x, node.y
|
||||
|
||||
explored.append(node)
|
||||
for x, y, direction, action in successor(field, node.x, node.y, node.direction):
|
||||
@ -26,25 +32,3 @@ def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, st
|
||||
node_queue.put(neighbour_node)
|
||||
|
||||
return False, False
|
||||
|
||||
|
||||
def breadth_first_search_for_a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction: int):
|
||||
explored = []
|
||||
node_queue = queue.Queue()
|
||||
node_queue.put(Node(start_x, start_y, start_direction))
|
||||
|
||||
while not node_queue.empty():
|
||||
node = node_queue.get()
|
||||
|
||||
if field[node.y][node.x].mine:
|
||||
return node.x, node.y
|
||||
|
||||
explored.append(node)
|
||||
for x, y, direction, action in successor(field, node.x, node.y, node.direction):
|
||||
neighbour_node = Node(x, y, direction, node, action)
|
||||
node_queue_list = node_queue.queue
|
||||
if neighbour_node in explored or neighbour_node in node_queue_list:
|
||||
continue
|
||||
node_queue.put(neighbour_node)
|
||||
|
||||
return False, False
|
Loading…
Reference in New Issue
Block a user