From f75a5a68d5b46220a07dd01d042fe548365f35a2 Mon Sep 17 00:00:00 2001 From: matixezor Date: Tue, 18 May 2021 00:01:37 +0200 Subject: [PATCH] remove code duplication, add a_star flag to bfs --- src/search_algoritms/BFS.py | 32 ++++++++------------------------ 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/src/search_algoritms/BFS.py b/src/search_algoritms/BFS.py index c40aa7b..ed5f48b 100644 --- a/src/search_algoritms/BFS.py +++ b/src/search_algoritms/BFS.py @@ -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 \ No newline at end of file