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 regal
|
||||||
import paczka
|
import paczka
|
||||||
from wozek import Wozek
|
from wozek import Wozek
|
||||||
import wyszukiwanie
|
from wyszukiwanie import wyszukiwanie_bfs, Stan, SearchSpace
|
||||||
|
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
screen = pygame.display.set_mode((980, 980))
|
screen = pygame.display.set_mode((980, 980))
|
||||||
@ -23,43 +24,32 @@ def main():
|
|||||||
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if event.type == pygame.MOUSEBUTTONDOWN:
|
grid = SearchSpace()
|
||||||
# lewy przycisk myszy
|
# create start and goal states
|
||||||
if event.button == 1:
|
start_state = Stan(x=0, y=0, kierunek=0) # for example
|
||||||
x = pygame.mouse.get_pos()[0]
|
goal_state = Stan(x=0, y=1, kierunek=0) # for example
|
||||||
y = pygame.mouse.get_pos()[1]
|
|
||||||
|
|
||||||
docelowy_stan = wyszukiwanie.Stan(x, y, 0)
|
# perform BFS search
|
||||||
# print(f'{docelowy_stan.x} + {docelowy_stan.y}')
|
path = wyszukiwanie_bfs(start_state, goal_state, grid)
|
||||||
wyszukiwanie.wyszukiwanie_bfs(wozek.obecnyStan, docelowy_stan)
|
|
||||||
|
|
||||||
if event.type == pygame.KEYDOWN:
|
# print the path, if found
|
||||||
if event.key == pygame.K_DOWN:
|
if path:
|
||||||
wozek.y_change = 1
|
for p in path:
|
||||||
if event.key == pygame.K_UP:
|
print(p.x, p.y)
|
||||||
wozek.y_change = -1
|
print("Path found:", path)
|
||||||
if event.key == pygame.K_RIGHT:
|
else:
|
||||||
wozek.x_change = 1
|
print("No path found.")
|
||||||
if event.key == pygame.K_LEFT:
|
|
||||||
wozek.x_change = -1
|
|
||||||
|
|
||||||
if event.type == pygame.KEYUP:
|
for state in path:
|
||||||
if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
|
pygame.time.delay(200)
|
||||||
wozek.y_change = 0
|
wozek.x = state.x
|
||||||
if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT:
|
wozek.y = state.y
|
||||||
wozek.x_change = 0
|
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
|
# Drawing
|
||||||
screen.fill((51,51,51)) # removes object trail
|
screen.fill((51,51,51)) # removes object trail
|
||||||
@ -94,3 +84,4 @@ def main():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
5
wozek.py
5
wozek.py
@ -1,7 +1,5 @@
|
|||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# screen nie działa
|
# screen nie działa
|
||||||
|
|
||||||
class Wozek():
|
class Wozek():
|
||||||
@ -51,7 +49,8 @@ class Wozek():
|
|||||||
|
|
||||||
def __zainicjuj_stan_poczatkowy(self):
|
def __zainicjuj_stan_poczatkowy(self):
|
||||||
from wyszukiwanie import Stan
|
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):
|
# def ustaw_wozek_w_kierunku(self, kierunek):
|
||||||
# TODO
|
# 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:
|
class Stan:
|
||||||
def __init__(self, x, y, kierunek):
|
def __init__(self, x, y, kierunek):
|
||||||
self.x = x
|
self.x = x
|
||||||
self.y = y
|
self.y = y
|
||||||
self.kierunek = kierunek
|
self.kierunek = kierunek
|
||||||
|
|
||||||
|
|
||||||
class Wezel:
|
class Wezel:
|
||||||
def __init__(self, stan):
|
def __init__(self, stan):
|
||||||
self.stan = stan
|
self.stan = stan
|
||||||
|
|
||||||
|
def poprzednik(wezel, search_space):
|
||||||
def poprzednik(wezel):
|
|
||||||
# gora -> prawo -> dol -> lewo | obrot w prawo
|
# gora -> prawo -> dol -> lewo | obrot w prawo
|
||||||
# gora -> lewo -> dol -> prawo | obrot w lewo
|
# gora -> lewo -> dol -> prawo | obrot w lewo
|
||||||
# 0 gora 1 prawo 2 dol 3 lewo
|
# 0 gora 1 prawo 2 dol 3 lewo
|
||||||
@ -37,23 +45,23 @@ def poprzednik(wezel):
|
|||||||
|
|
||||||
return wezly
|
return wezly
|
||||||
|
|
||||||
|
def wyszukiwanie_bfs(stan_poczatkowy, stan_docelowy, search_space):
|
||||||
def wyszukiwanie_bfs(stan_poczatkowy, stan_docelowy):
|
|
||||||
pierwszy_wezel = Wezel(stan_poczatkowy)
|
pierwszy_wezel = Wezel(stan_poczatkowy)
|
||||||
fringe = [pierwszy_wezel]
|
fringe = [pierwszy_wezel]
|
||||||
|
|
||||||
odwiedzone = list()
|
odwiedzone = set()
|
||||||
|
|
||||||
while fringe:
|
while fringe:
|
||||||
# kolejka
|
|
||||||
wezel = fringe.pop(0)
|
wezel = fringe.pop(0)
|
||||||
|
|
||||||
if stan_docelowy.x == wezel.stan.x and stan_docelowy.y == wezel.stan.y:
|
if stan_docelowy.x == wezel.stan.x and stan_docelowy.y == wezel.stan.y:
|
||||||
return odwiedzone
|
return odwiedzone
|
||||||
|
|
||||||
odwiedzone.append(wezel)
|
odwiedzone.add(wezel.stan)
|
||||||
|
|
||||||
for stan in poprzednik(wezel):
|
for stan in poprzednik(wezel, search_space):
|
||||||
if wezel not in fringe and stan not in odwiedzone:
|
if stan not in odwiedzone:
|
||||||
nowy_wezel = Wezel(stan)
|
nowy_wezel = Wezel(stan)
|
||||||
fringe.append(nowy_wezel)
|
fringe.append(nowy_wezel)
|
||||||
|
odwiedzone.add(stan)
|
||||||
|
return odwiedzone
|
||||||
|
Loading…
Reference in New Issue
Block a user