fix bfs
This commit is contained in:
parent
8e9af8f0b3
commit
1de3c78fd3
45
src/BFS.py
45
src/BFS.py
@ -1,39 +1,37 @@
|
|||||||
import queue
|
import queue
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
import pygame as pg
|
||||||
|
|
||||||
from node import Node
|
from node import Node
|
||||||
from tile import Tile
|
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:
|
kody klawiszy:
|
||||||
:a -> 97
|
:a -> 97
|
||||||
:d -> 100
|
:d -> 100
|
||||||
:w -> 119
|
: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 = []
|
neighbours = []
|
||||||
for i in range(4):
|
coord = 'x' if direction in (1, 3) else 'y'
|
||||||
neighbour_x = x + DIRECTIONS_X[i]
|
shift = -1 if direction in (0, 3) else 1
|
||||||
neighbour_y = y + DIRECTIONS_Y[i]
|
|
||||||
neighbour_side = DIRECTIONS_SIDE[i]
|
|
||||||
|
|
||||||
if 0 <= neighbour_x <= 9 and 0 <= neighbour_y <= 9:
|
neighbours.append((x, y, (direction - 1) % 4, pg.K_a))
|
||||||
neighbours.append((neighbour_x, neighbour_y, neighbour_side, ACTIONS[neighbour_side-direction]))
|
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
|
return neighbours
|
||||||
|
|
||||||
|
|
||||||
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):
|
||||||
explored = [(start_x, start_y)]
|
explored = []
|
||||||
node_queue = queue.SimpleQueue()
|
node_queue = queue.Queue()
|
||||||
node_queue.put(Node(start_x, start_y, start_direction))
|
node_queue.put(Node(start_x, start_y, start_direction))
|
||||||
|
|
||||||
while not node_queue.empty():
|
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 = []
|
actions = []
|
||||||
while node.parent:
|
while node.parent:
|
||||||
path.append((node.x, node.y))
|
path.append((node.x, node.y))
|
||||||
actions.append(node.actions)
|
actions.append(node.action)
|
||||||
node = node.parent
|
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):
|
explored.append(node)
|
||||||
if field[y][x].number in (2, 3):
|
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
|
continue
|
||||||
elif (x, y) in explored:
|
|
||||||
continue
|
|
||||||
explored.append((x, y))
|
|
||||||
neighbour_node = Node(x, y, direction, node, actions)
|
|
||||||
node_queue.put(neighbour_node)
|
node_queue.put(neighbour_node)
|
||||||
|
|
||||||
return False, False
|
return False, False
|
||||||
|
21
src/const.py
21
src/const.py
@ -61,24 +61,3 @@ DEFAULT_FIELD = [
|
|||||||
[5, 2, 0, 1, 3, 1, 2, 1, 0, 0],
|
[5, 2, 0, 1, 3, 1, 2, 1, 0, 0],
|
||||||
[2, 5, 1, 4, 5, 1, 0, 5, 4, 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)
|
|
||||||
|
@ -44,7 +44,6 @@ def main():
|
|||||||
agent = Agent()
|
agent = Agent()
|
||||||
game_ui = GameUi(agent, env)
|
game_ui = GameUi(agent, env)
|
||||||
game_ui.update()
|
game_ui.update()
|
||||||
|
|
||||||
factory = TilesFactory()
|
factory = TilesFactory()
|
||||||
|
|
||||||
running = True
|
running = True
|
||||||
|
11
src/node.py
11
src/node.py
@ -1,12 +1,17 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from pygame import event
|
from pygame import event
|
||||||
from typing import List
|
|
||||||
|
|
||||||
|
|
||||||
class Node:
|
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.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.direction = direction
|
self.direction = direction
|
||||||
self.parent = parent
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user