From be6481745adb9b33aaceed58901f1cc3e247babc Mon Sep 17 00:00:00 2001 From: matixezor Date: Sat, 10 Apr 2021 12:12:06 +0200 Subject: [PATCH] implement successor and bfs --- src/BFS.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/BFS.py diff --git a/src/BFS.py b/src/BFS.py new file mode 100644 index 0000000..82de383 --- /dev/null +++ b/src/BFS.py @@ -0,0 +1,50 @@ +import queue +from typing import Tuple, List + +from Node import Node +from tile import Tile +from const import DIRECTIONS_X, DIRECTIONS_Y + + +def successor(x: int, y: int): + neighbours = [] + for i in range(4): + neighbour_x = x + DIRECTIONS_X[i] + neighbour_y = y + DIRECTIONS_Y[i] + + if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9: + neighbours.append((neighbour_x, neighbour_y)) + + return neighbours + + +def breadth_first_search(field: List[Tile], start_x: int, start_y: int, goal: Tuple[int]): + # TODO + # 1. Implementacja akcji (zapisywanie tego co robot musi zrobić by przejść z jednego node do drugiego) + # 2. Wypisanie drogi razem z akcjami + explored = [(start_x, start_y)] + node_queue = queue.SimpleQueue() + node_queue.put(Node(start_x, start_y)) + + while not node_queue.empty(): + node = node_queue.get() + + if (node.x, node.y) == goal: + path = [] + print(explored) + while node.parent: + path.append((node.x, node.y)) + node = node.parent + print(path[::-1]) + return True + + for x, y in successor(node.x, node.y): + if field[y][x].number in (2, 3): + continue + elif (x, y) in explored: + continue + explored.append((x, y)) + neighbour_node = Node(x, y, node) + node_queue.put(neighbour_node) + + return False