modified the bfs search - prints path
This commit is contained in:
parent
ea49df80bd
commit
2a3e6b006a
BIN
__pycache__/etykieta.cpython-311.pyc
Normal file
BIN
__pycache__/etykieta.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/main.cpython-311.pyc
Normal file
BIN
__pycache__/main.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/paczka.cpython-311.pyc
Normal file
BIN
__pycache__/paczka.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/regal.cpython-311.pyc
Normal file
BIN
__pycache__/regal.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wozek.cpython-311.pyc
Normal file
BIN
__pycache__/wozek.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wyszukiwanie.cpython-311.pyc
Normal file
BIN
__pycache__/wyszukiwanie.cpython-311.pyc
Normal file
Binary file not shown.
BIN
__pycache__/wyszukiwanie1.cpython-311.pyc
Normal file
BIN
__pycache__/wyszukiwanie1.cpython-311.pyc
Normal file
Binary file not shown.
59
main.py
59
main.py
@ -3,7 +3,8 @@ import pygame
|
||||
import regal
|
||||
import paczka
|
||||
from wozek import Wozek
|
||||
import wyszukiwanie
|
||||
from wyszukiwanie import wyszukiwanie_bfs, Stan, SearchSpace
|
||||
|
||||
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((980, 980))
|
||||
@ -23,43 +24,32 @@ def main():
|
||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||
sys.exit(0)
|
||||
|
||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
||||
# lewy przycisk myszy
|
||||
if event.button == 1:
|
||||
x = pygame.mouse.get_pos()[0]
|
||||
y = pygame.mouse.get_pos()[1]
|
||||
grid = SearchSpace()
|
||||
# create start and goal states
|
||||
start_state = Stan(x=0, y=0, kierunek=0) # for example
|
||||
goal_state = Stan(x=0, y=1, kierunek=0) # for example
|
||||
|
||||
docelowy_stan = wyszukiwanie.Stan(x, y, 0)
|
||||
# print(f'{docelowy_stan.x} + {docelowy_stan.y}')
|
||||
wyszukiwanie.wyszukiwanie_bfs(wozek.obecnyStan, docelowy_stan)
|
||||
# perform BFS search
|
||||
path = wyszukiwanie_bfs(start_state, goal_state, grid)
|
||||
|
||||
if event.type == pygame.KEYDOWN:
|
||||
if event.key == pygame.K_DOWN:
|
||||
wozek.y_change = 1
|
||||
if event.key == pygame.K_UP:
|
||||
wozek.y_change = -1
|
||||
if event.key == pygame.K_RIGHT:
|
||||
wozek.x_change = 1
|
||||
if event.key == pygame.K_LEFT:
|
||||
wozek.x_change = -1
|
||||
# print the path, if found
|
||||
if path:
|
||||
for p in path:
|
||||
print(p.x, p.y)
|
||||
print("Path found:", path)
|
||||
else:
|
||||
print("No path found.")
|
||||
|
||||
if event.type == pygame.KEYUP:
|
||||
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
|
||||
wozek.y_change = 0
|
||||
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
|
||||
wozek.x_change = 0
|
||||
for state in path:
|
||||
pygame.time.delay(200)
|
||||
wozek.x = state.x
|
||||
wozek.y = state.y
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
sys.exit(0)
|
||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||
sys.exit(0)
|
||||
|
||||
wozek.x += wozek.x_change
|
||||
wozek.y += wozek.y_change
|
||||
|
||||
if wozek.x <= 0:
|
||||
wozek.x = 0
|
||||
elif wozek.x >= 916:
|
||||
wozek.x = 916
|
||||
if wozek.y <= 0:
|
||||
wozek.y = 0
|
||||
elif wozek.x >= 916:
|
||||
wozek.x = 916
|
||||
|
||||
# Drawing
|
||||
screen.fill((51,51,51)) # removes object trail
|
||||
@ -94,3 +84,4 @@ def main():
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
5
wozek.py
5
wozek.py
@ -1,7 +1,5 @@
|
||||
import pygame
|
||||
|
||||
|
||||
|
||||
# screen nie działa
|
||||
|
||||
class Wozek():
|
||||
@ -51,7 +49,8 @@ class Wozek():
|
||||
|
||||
def __zainicjuj_stan_poczatkowy(self):
|
||||
from wyszukiwanie import Stan
|
||||
self.obecnyStan = Stan(55, 55, 3)
|
||||
# self.obecnyStan = Stan(55, 55, 3)
|
||||
self.obecnyStan = Stan(0, 0, 3)
|
||||
|
||||
# def ustaw_wozek_w_kierunku(self, kierunek):
|
||||
# TODO
|
||||
|
@ -1,16 +1,24 @@
|
||||
class SearchSpace:
|
||||
def __init__(self):
|
||||
self.grid = [['1']*10 for _ in range(10)] # create a 10x10 grid of cells
|
||||
for r, c in [(2,2), (2,3), (3,2), (3,3), (8,2), (8,3), (9,2), (9,3),
|
||||
(2,8), (2,9), (3,8), (3,9), (8,8), (8,9), (9,8), (9,9)]:
|
||||
self.grid[r][c] = 'X' # set the cells with a shelf to not passable
|
||||
|
||||
def is_passable(self, x, y):
|
||||
return 0 <= x < 10 and 0 <= y < 10 and self.grid[y][x] != 'X'
|
||||
|
||||
class Stan:
|
||||
def __init__(self, x, y, kierunek):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.kierunek = kierunek
|
||||
|
||||
|
||||
class Wezel:
|
||||
def __init__(self, stan):
|
||||
self.stan = stan
|
||||
|
||||
|
||||
def poprzednik(wezel):
|
||||
def poprzednik(wezel, search_space):
|
||||
# gora -> prawo -> dol -> lewo | obrot w prawo
|
||||
# gora -> lewo -> dol -> prawo | obrot w lewo
|
||||
# 0 gora 1 prawo 2 dol 3 lewo
|
||||
@ -37,23 +45,23 @@ def poprzednik(wezel):
|
||||
|
||||
return wezly
|
||||
|
||||
|
||||
def wyszukiwanie_bfs(stan_poczatkowy, stan_docelowy):
|
||||
def wyszukiwanie_bfs(stan_poczatkowy, stan_docelowy, search_space):
|
||||
pierwszy_wezel = Wezel(stan_poczatkowy)
|
||||
fringe = [pierwszy_wezel]
|
||||
|
||||
odwiedzone = list()
|
||||
odwiedzone = set()
|
||||
|
||||
while fringe:
|
||||
# kolejka
|
||||
wezel = fringe.pop(0)
|
||||
|
||||
if stan_docelowy.x == wezel.stan.x and stan_docelowy.y == wezel.stan.y:
|
||||
return odwiedzone
|
||||
|
||||
odwiedzone.append(wezel)
|
||||
odwiedzone.add(wezel.stan)
|
||||
|
||||
for stan in poprzednik(wezel):
|
||||
if wezel not in fringe and stan not in odwiedzone:
|
||||
for stan in poprzednik(wezel, search_space):
|
||||
if stan not in odwiedzone:
|
||||
nowy_wezel = Wezel(stan)
|
||||
fringe.append(nowy_wezel)
|
||||
odwiedzone.add(stan)
|
||||
return odwiedzone
|
||||
|
Loading…
Reference in New Issue
Block a user