diff --git a/src/BFS.py b/src/BFS.py deleted file mode 100644 index 20825cf..0000000 --- a/src/BFS.py +++ /dev/null @@ -1,57 +0,0 @@ -import queue -from typing import List - -import pygame as pg - -from node import Node -from tile import Tile - - -def successor(field: List[List[Tile]], x: int, y: int, direction: int): - """ - kody klawiszy: - :a -> 97 - :d -> 100 - :w -> 119 - """ - neighbours = [] - coord = 'x' if direction in (1, 3) else 'y' - shift = -1 if direction in (0, 3) else 1 - - neighbours.append((x, y, (direction - 1) % 4, pg.K_a)) - neighbours.append((x, y, (direction + 1) % 4, pg.K_d)) - - if coord == 'x' and 0 <= x + shift <= 9 and field[y][x + shift].number not in (2, 3): - neighbours.append((x + shift, y, direction, pg.K_w)) - elif coord == 'y' and 0 <= y + shift <= 9 and field[y + shift][x].number not in (2, 3): - neighbours.append((x, y + shift, direction, pg.K_w)) - - return neighbours - - -def breadth_first_search(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: - path = [] - actions = [] - while node.parent: - path.append((node.x, node.y)) - actions.append(node.action) - node = node.parent - return path[::-1], actions[::-1] - - 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 = 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 diff --git a/src/main.py b/src/main.py index 217f9a9..c909d9f 100644 --- a/src/main.py +++ b/src/main.py @@ -4,7 +4,7 @@ from agent import Agent from game_ui import GameUi from const import ICON, IMAGES from environment import Environment -from BFS import breadth_first_search +from search_algoritms.BFS import breadth_first_search from tilesFactory import TilesFactory diff --git a/src/search_algoritms/BFS.py b/src/search_algoritms/BFS.py new file mode 100644 index 0000000..e899e09 --- /dev/null +++ b/src/search_algoritms/BFS.py @@ -0,0 +1,28 @@ +import queue +from typing import List + +from node import Node +from 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): + 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 get_path_actions(node) + + 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 diff --git a/src/search_algoritms/__init__.py b/src/search_algoritms/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/search_algoritms/a_star.py b/src/search_algoritms/a_star.py new file mode 100644 index 0000000..47c7e14 --- /dev/null +++ b/src/search_algoritms/a_star.py @@ -0,0 +1,34 @@ +from typing import List, Tuple +from operator import itemgetter + +from node import Node +from tile import Tile +from .helpers import successor, get_path_actions + + +def a_star(field: List[List[Tile]], start_x: int, start_y: int, start_direction: int, goal): + explored = [] + node_queue: List[Tuple[int, Node]] = [(1, Node(start_x, start_y, start_direction))] + # do kolejki dodawać krotke (priorytet, obiekt) + + while node_queue: + priority, node = node_queue.pop(0) + # przy get wyciąga element z najniższą wartością priorytetu + + if field[node.y][node.x] == goal: + return get_path_actions(node) + + 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) + neighbour_priority = calculate_priority(node, goal) + if neighbour_node not in explored and all(neighbour_node not in queue_tuple for queue_tuple in node_queue): + node_queue.append((neighbour_priority, neighbour_node)) + node_queue = sorted(node_queue, key=itemgetter(0)) + elif (index := [ + index for index, queue_tuple in enumerate(node_queue) if neighbour_priority > queue_tuple[0] + ][0]): + node_queue[index] = (neighbour_priority, neighbour_node) + node_queue = sorted(node_queue, key=itemgetter(0)) + + return False, False \ No newline at end of file diff --git a/src/search_algoritms/helpers.py b/src/search_algoritms/helpers.py new file mode 100644 index 0000000..1229b33 --- /dev/null +++ b/src/search_algoritms/helpers.py @@ -0,0 +1,38 @@ +from typing import List + +import pygame as pg + +from tile import Tile +from node import Node + + +def successor(field: List[List[Tile]], x: int, y: int, direction: int): + """ + kody klawiszy: + :a -> 97 + :d -> 100 + :w -> 119 + """ + neighbours = [] + coord = 'x' if direction in (1, 3) else 'y' + shift = -1 if direction in (0, 3) else 1 + + neighbours.append((x, y, (direction - 1) % 4, pg.K_a)) + neighbours.append((x, y, (direction + 1) % 4, pg.K_d)) + + if coord == 'x' and 0 <= x + shift <= 9 and field[y][x + shift].number not in (2, 3): + neighbours.append((x + shift, y, direction, pg.K_w)) + elif coord == 'y' and 0 <= y + shift <= 9 and field[y + shift][x].number not in (2, 3): + neighbours.append((x, y + shift, direction, pg.K_w)) + + return neighbours + + +def get_path_actions(node: Node): + path = [] + actions = [] + while node.parent: + path.append((node.x, node.y)) + actions.append(node.action) + node = node.parent + return path[::-1], actions[::-1]