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
|
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 = []
|
explored = []
|
||||||
node_queue = queue.Queue()
|
node_queue = queue.Queue()
|
||||||
node_queue.put(Node(start_x, start_y, start_direction))
|
node_queue.put(Node(start_x, start_y, start_direction))
|
||||||
@ -15,29 +21,7 @@ def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, st
|
|||||||
node = node_queue.get()
|
node = node_queue.get()
|
||||||
|
|
||||||
if field[node.y][node.x].mine:
|
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):
|
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
explored.append(node)
|
||||||
for x, y, direction, action in successor(field, node.x, node.y, node.direction):
|
for x, y, direction, action in successor(field, node.x, node.y, node.direction):
|
||||||
|
Loading…
Reference in New Issue
Block a user