implement successor and bfs

This commit is contained in:
matixezor 2021-04-10 12:12:06 +02:00
parent a1938e5683
commit be6481745a

50
src/BFS.py Normal file
View File

@ -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