From 1de3c78fd399de2d84acd0d3d7c9bd2241eac26e Mon Sep 17 00:00:00 2001 From: matixezor Date: Sat, 24 Apr 2021 20:01:45 +0200 Subject: [PATCH] fix bfs --- src/BFS.py | 45 +++++++++++++++++++++------------------------ src/const.py | 21 --------------------- src/main.py | 1 - src/node.py | 11 ++++++++--- 4 files changed, 29 insertions(+), 49 deletions(-) diff --git a/src/BFS.py b/src/BFS.py index 08117cb..20825cf 100644 --- a/src/BFS.py +++ b/src/BFS.py @@ -1,39 +1,37 @@ import queue from typing import List +import pygame as pg + from node import Node from tile import Tile -from const import DIRECTIONS_X, DIRECTIONS_Y, DIRECTIONS_SIDE, ACTIONS -def successor(x: int, y: int, direction: int): +def successor(field: List[List[Tile]], x: int, y: int, direction: int): """ kody klawiszy: :a -> 97 :d -> 100 :w -> 119 - 1 direction -> prawo - neighbour side 1 -> po prawej 1-1 = 0 -> W - neighbour side 2 -> na gorze 2-1 = 1 -> D + W - neighbour side 3 -> po lewej 3-1 = 2 -> D + D + W - neighbour side 0 -> na dole 0-1 = -1 -> A + W - zwraca listę krotek, które zawierają x, y, kierunek i akcje dla sąsiada """ neighbours = [] - for i in range(4): - neighbour_x = x + DIRECTIONS_X[i] - neighbour_y = y + DIRECTIONS_Y[i] - neighbour_side = DIRECTIONS_SIDE[i] + coord = 'x' if direction in (1, 3) else 'y' + shift = -1 if direction in (0, 3) else 1 - if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9: - neighbours.append((neighbour_x, neighbour_y, neighbour_side, ACTIONS[neighbour_side-direction])) + 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 = [(start_x, start_y)] - node_queue = queue.SimpleQueue() + explored = [] + node_queue = queue.Queue() node_queue.put(Node(start_x, start_y, start_direction)) while not node_queue.empty(): @@ -44,17 +42,16 @@ def breadth_first_search(field: List[List[Tile]], start_x: int, start_y: int, st actions = [] while node.parent: path.append((node.x, node.y)) - actions.append(node.actions) + actions.append(node.action) node = node.parent - return path[::-1], sum(actions[::-1], []) + return path[::-1], actions[::-1] - for x, y, direction, actions in successor(node.x, node.y, node.direction): - if field[y][x].number in (2, 3): + 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 - elif (x, y) in explored: - continue - explored.append((x, y)) - neighbour_node = Node(x, y, direction, node, actions) node_queue.put(neighbour_node) return False, False diff --git a/src/const.py b/src/const.py index c19ac2d..12af6fa 100644 --- a/src/const.py +++ b/src/const.py @@ -61,24 +61,3 @@ DEFAULT_FIELD = [ [5, 2, 0, 1, 3, 1, 2, 1, 0, 0], [2, 5, 1, 4, 5, 1, 0, 5, 4, 0], ] - -# -1 -> prawo, ruch -# 0 -> ruch -# 1 -> lewo, ruch -# -2 -> prawo, prawo, ruch -# 2 -> lewo, lewo, ruch -# -3 -> lewo, ruch -# 3 -> prawo, ruch -ACTIONS = { - -1: [pg.K_a, pg.K_w], - 0: [pg.K_w], - 1: [pg.K_d, pg.K_w], - 2: [pg.K_d, pg.K_d, pg.K_w], - -2: [pg.K_d, pg.K_d, pg.K_w], - -3: [pg.K_d, pg.K_w], - 3: [pg.K_a, pg.K_w] -} - -DIRECTIONS_X = (0, -1, 0, 1) -DIRECTIONS_Y = (-1, 0, 1, 0) -DIRECTIONS_SIDE = (0, 3, 2, 1) diff --git a/src/main.py b/src/main.py index c677f0f..217f9a9 100644 --- a/src/main.py +++ b/src/main.py @@ -44,7 +44,6 @@ def main(): agent = Agent() game_ui = GameUi(agent, env) game_ui.update() - factory = TilesFactory() running = True diff --git a/src/node.py b/src/node.py index 0c7569e..621a9f7 100644 --- a/src/node.py +++ b/src/node.py @@ -1,12 +1,17 @@ from __future__ import annotations + from pygame import event -from typing import List class Node: - def __init__(self, x: int, y: int, direction: int, parent: Node = None, actions: List[event] = None): + def __init__(self, x: int, y: int, direction: int, parent: Node = None, action: event = None): self.x = x self.y = y self.direction = direction self.parent = parent - self.actions = actions + self.action = action + + def __eq__(self, other): + if isinstance(other, Node): + return self.x == other.x and self.y == other.y and self.direction == other.direction + return False