change project structure, add a* template
This commit is contained in:
parent
1de3c78fd3
commit
f777afba56
57
src/BFS.py
57
src/BFS.py
@ -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
|
@ -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
|
||||
|
||||
|
||||
|
28
src/search_algoritms/BFS.py
Normal file
28
src/search_algoritms/BFS.py
Normal file
@ -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
|
0
src/search_algoritms/__init__.py
Normal file
0
src/search_algoritms/__init__.py
Normal file
34
src/search_algoritms/a_star.py
Normal file
34
src/search_algoritms/a_star.py
Normal file
@ -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
|
38
src/search_algoritms/helpers.py
Normal file
38
src/search_algoritms/helpers.py
Normal file
@ -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]
|
Loading…
Reference in New Issue
Block a user