diff --git a/App.py b/App.py index 8029d49..93c18ba 100644 --- a/App.py +++ b/App.py @@ -6,19 +6,26 @@ import time import displayControler as dCon import Image import Osprzet +import Ui +import BFS pygame.init() -screen = pygame.display.set_mode((dCon.getScreenWidth(), dCon.getScreenHeihgt())) - +show_console=True +screen = pygame.display.set_mode((dCon.getScreenWidth(show_console), dCon.getScreenHeihgt())) +FPS=5 +clock=pygame.time.Clock() image_loader=Image.Image() image_loader.load_images() pole=Pole.Pole(screen,image_loader) pole.draw_grid() #musi byc tutaj wywołane ponieważ inicjalizuje sloty do slownika - +ui=Ui.Ui(screen) #Tractor creation +bfs1_flag=True +bfs2_flag=False #Change this lines to show different bfs implementation +bfs3_flag=False traktor_slot = pole.get_slot_from_cord((0, 0)) -traktor = Tractor.Tractor(traktor_slot, screen, Osprzet.plug) +traktor = Tractor.Tractor(traktor_slot, screen, Osprzet.opryskiwacz,clock,bfs2_flag) def init_demo(): #Demo purpose @@ -27,9 +34,43 @@ def init_demo(): #Demo purpose time.sleep(2) pole.randomize_colors() traktor.draw_tractor() + start_flag=True while True: - time.sleep(0.5) - demo_move() + clock.tick(FPS) + if(start_flag): + ui.render_text_to_console(string_to_print="Przejazd inicjalizujacy- traktor sprawdza poziom nawodnienia") + traktor.initial_move(pole) + traktor.reset_pos(pole) + clock.tick(20) + ui.clear_console() + clock.tick(20) + ui.render_text_to_console("Obliczanie sciezki przy uzyciu BFS") + for event in pygame.event.get(): + if event.type == pygame.QUIT: + quit() + if(bfs1_flag): + bfsRoot1 = BFS.BFS1({"x": 0, "y": 0, "direction": "E", "hydradeIndex": traktor.slot_hydrate_dict}) + #displayControler: NUM_X: 6, NUM_Y: 3 (klasyczne) CHANGE THIS BY HAND + bfsRoot1.reverse() + print_to_console("Traktor porusza sie obliczona sciezka BFS") + traktor.move_by_root(bfsRoot1, pole, [traktor.irrigateSlot]) + if(bfs2_flag): + bfsRoot2 = [[{'x': 2, 'y': 2, 'direction': 'N', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 1, (1, 2): 1, (2, 2): 1, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 2, 'y': 2, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 1, (1, 2): 1, (2, 2): 1, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'left'], [{'x': 1, 'y': 2, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 1, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 0, 'y': 2, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 1, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 0, 'y': 2, 'direction': 'N', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 1, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'right'], [{'x': 0, 'y': 3, 'direction': 'N', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 0, 'y': 3, 'direction': 'W', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'right'], [{'x': 1, 'y': 3, 'direction': 'W', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 1, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 2, 'y': 3, 'direction': 'W', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 1, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 3, 'y': 3, 'direction': 'W', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 1, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 4, 'y': 3, 'direction': 'W', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 4, 'y': 3, 'direction': 'S', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 1, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'right'], [{'x': 4, 'y': 2, 'direction': 'S', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 1, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 4, 'y': 1, 'direction': 'S', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 1, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 0, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 4, 'y': 0, 'direction': 'S', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 0, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 0, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 4, 'y': 0, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 1, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 0, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 0, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'right'], [{'x': 3, 'y': 0, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 1, (4, 0): 0, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 0, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 0, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 2, 'y': 0, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 1, (3, 0): 0, (4, 0): 0, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 0, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 0, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 1, 'y': 0, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 1, (2, 0): 0, (3, 0): 0, (4, 0): 0, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 0, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 0, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward'], [{'x': 0, 'y': 0, 'direction': 'E', 'hydradeIndex': {(0, 0): -1, (1, 0): 0, (2, 0): 0, (3, 0): 0, (4, 0): 0, (5, 0): 1, (6, 0): 1, (7, 0): 1, (8, 0): 1, (9, 0): 1, (0, 1): 1, (1, 1): 1, (2, 1): 0, (3, 1): 1, (4, 1): 0, (5, 1): 1, (6, 1): 1, (7, 1): 1, (8, 1): 1, (9, 1): 1, (0, 2): 0, (1, 2): 1, (2, 2): 0, (3, 2): 1, (4, 2): 0, (5, 2): 1, (6, 2): 1, (7, 2): 1, (8, 2): 1, (9, 2): 1, (0, 3): 1, (1, 3): 0, (2, 3): 0, (3, 3): 0, (4, 3): 0, (5, 3): 1, (6, 3): 1, (7, 3): 1, (8, 3): 1, (9, 3): 1, (0, 4): 1, (1, 4): 1, (2, 4): 1, (3, 4): 1, (4, 4): 1, (5, 4): 1, (6, 4): 1, (7, 4): 1, (8, 4): 1, (9, 4): 1}}, 'forward']] + #displayControler: NUM_X: 10, NUM_Y: 5 (klasyczne - duże) CHANGE THIS IN DCON !!! + bfsRoot2.reverse() + print_to_console("Traktor porusza sie obliczona sciezka BFS") + traktor.move_by_root(bfsRoot2, pole, [traktor.irrigateSlot]) + if(bfs3_flag): + bfsRoot3 = BFS.BFS2({'x': 0, 'y': 0, 'direction': "E"}) + #displayControler: NUM_X: 20, NUM_Y: 12 (skarb) CHANGE THIS IN DCON BY HAND!!!!!!!! + bfsRoot3.reverse() + print_to_console("Traktor porusza sie obliczona sciezka BFS") + traktor.move_by_root(bfsRoot3, pole, [traktor.irrigateSlot]) + + + + start_flag=False + # demo_move() old_info=get_info(old_info) for event in pygame.event.get(): if event.type == pygame.QUIT: @@ -41,6 +82,11 @@ def init(demo): init_demo() #TODO: Implement +def print_to_console(text): + clock.tick(5) + ui.clear_console() + clock.tick(5) + ui.render_text_to_console(text) def demo_move(): current_slot = traktor.slot @@ -49,11 +95,14 @@ def demo_move(): traktor.random_move(pole) def get_info(old_info): - (x,y)=pygame.mouse.get_pos() - new_info=pole.check_collision(x,y) - if(old_info!=new_info): - print(new_info) - return new_info + try: + (x,y)=pygame.mouse.get_pos() + new_info=pole.check_collision(x,y) + if(old_info!=new_info): + print(new_info) + return new_info + except: + pass diff --git a/BFS.py b/BFS.py new file mode 100644 index 0000000..366bcac --- /dev/null +++ b/BFS.py @@ -0,0 +1,262 @@ +import random + +import pygame +import Node +from displayControler import NUM_X, NUM_Y + + +def goalTest1(hIndex): + for i in list(hIndex.values()): + if i == 0: + return False + return True + + +def succ1(state): + resp = [] + hIndex = state["hydradeIndex"].copy() + if state["direction"] == "N": + if state["y"] > 0: + if hIndex[state["x"], state["y"]-1] == 0: + hIndex[state["x"], state["y"] - 1] = 1 + resp.append(["forward", {'x': state["x"], 'y': state["y"]-1, 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "E", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "W", 'hydradeIndex': state["hydradeIndex"].copy()}]) + elif state["direction"] == "S": + if state["y"] < NUM_Y-1: + if hIndex[state["x"], state["y"]+1] == 0: + hIndex[state["x"], state["y"] + 1] = 1 + resp.append(["forward", {'x': state["x"], 'y': state["y"]+1, 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "W", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "E", 'hydradeIndex': state["hydradeIndex"].copy()}]) + elif state["direction"] == "E": + if state["x"] < NUM_X-1: + if hIndex[state["x"]+1, state["y"]] == 0: + hIndex[state["x"] + 1, state["y"]] = 1 + resp.append(["forward", {'x': state["x"]+1, 'y': state["y"], 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "S", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "N", 'hydradeIndex': state["hydradeIndex"].copy()}]) + else: #state["zwrot"] == "W" + if state["x"] > 0: + if hIndex[state["x"]-1, state["y"]] == 0: + hIndex[state["x"] - 1, state["y"]] = 1 + resp.append(["forward", {'x': state["x"]-1, 'y': state["y"], 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "N", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "S", 'hydradeIndex': state["hydradeIndex"].copy()}]) + + return resp + + +def check1(tab, state): + for i in tab: + if i.state == state: + return False + return True + + +def BFS1(istate): + + + fringe = [] + explored = [] + + x = Node.Node(istate) + fringe.append(x) + + while True: + + + if fringe == []: + return False + + elem = fringe.pop(0) + + if goalTest1(elem.state["hydradeIndex"]): + x = elem + tab = [] + while x.parent != None: + tab.append([x.parent, x.action]) + x = x.parent + return tab + explored.append(elem) + + for resp in succ1(elem.state): + if check1(fringe, resp[1]) and check1(explored, resp[1]): + x = Node.Node(resp[1]) + x.parent = elem + x.action = resp[0] + fringe.append(x) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + quit() + + + + +def goalTest2(state, goalTreassure): + if state["x"] == goalTreassure[0] and state["y"] == goalTreassure[1]: + return True + return False + + +def succ2(state): + resp = [] + if state["direction"] == "N": + if state["y"] > 0: + resp.append(["forward", {'x': state["x"], 'y': state["y"]-1, 'direction': state["direction"]}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "E"}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "W"}]) + elif state["direction"] == "S": + if state["y"] < NUM_Y: + resp.append(["forward", {'x': state["x"], 'y': state["y"]+1, 'direction': state["direction"]}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "W"}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "E"}]) + elif state["direction"] == "E": + if state["x"] < NUM_X: + resp.append(["forward", {'x': state["x"]+1, 'y': state["y"], 'direction': state["direction"]}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "S"}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "N"}]) + else: #state["zwrot"] == "W" + if state["x"] > 0: + resp.append(["forward", {'x': state["x"]-1, 'y': state["y"], 'direction': state["direction"]}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "N"}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "S"}]) + + return resp + + +def check2(tab, state): + for i in tab: + if i.state == state: + return False + return True + + +def BFS2(istate): + goalTreassuere = (random.randint(0,NUM_X-1), random.randint(0,NUM_Y-1)) + print(goalTreassuere) + fringe = [] + explored = [] + + x = Node.Node(istate) + fringe.append(x) + + while True: + + if fringe == []: + return False + + elem = fringe.pop(0) + + if goalTest2(elem.state, goalTreassuere): + x = elem + tab = [] + while x.parent != None: + tab.append([x.parent, x.action]) + x = x.parent + return tab + + explored.append(elem) + + for resp in succ2(elem.state): + if check2(fringe, resp[1]) and check2(explored, resp[1]): + x = Node.Node(resp[1]) + x.parent = elem + x.action = resp[0] + fringe.append(x) + for event in pygame.event.get(): + if event.type == pygame.QUIT: + quit() + + + + +""" +def goalTest(hIndex): + for i in list(hIndex.values()): + if i == 0: + return False + return True + + +def succ(state): + resp = [] + hIndex = state["hydradeIndex"].copy() + + if state["direction"] == "N": + if state["y"] > 0: + if hIndex[state["x"], state["y"]-1] == 0: + hIndex[state["x"], state["y"] - 1] = 1 + resp.append(["forward", {'x': state["x"], 'y': state["y"]-1, 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "E", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "W", 'hydradeIndex': state["hydradeIndex"].copy()}]) + elif state["direction"] == "S": + if state["y"] < dCon.NUM_Y-1: + if hIndex[state["x"], state["y"]+1] == 0: + hIndex[state["x"], state["y"] + 1] = 1 + resp.append(["forward", {'x': state["x"], 'y': state["y"]+1, 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "W", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "E", 'hydradeIndex': state["hydradeIndex"].copy()}]) + elif state["direction"] == "E": + if state["x"] < dCon.NUM_X-1: + if hIndex[state["x"]+1, state["y"]] == 0: + hIndex[state["x"] + 1, state["y"]] = 1 + resp.append(["forward", {'x': state["x"]+1, 'y': state["y"], 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "S", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "N", 'hydradeIndex': state["hydradeIndex"].copy()}]) + else: #state["direction"] == "W" + if state["x"] > 0: + if hIndex[state["x"]-1, state["y"]] == 0: + hIndex[state["x"] - 1, state["y"]] = 1 + resp.append(["forward", {'x': state["x"]-1, 'y': state["y"], 'direction': state["direction"], 'hydradeIndex': hIndex}]) + resp.append(["right", {'x': state["x"], 'y': state["y"], 'direction': "N", 'hydradeIndex': state["hydradeIndex"].copy()}]) + resp.append(["left", {'x': state["x"], 'y': state["y"], 'direction': "S", 'hydradeIndex': state["hydradeIndex"].copy()}]) + + return resp + + +def check(tab, state): + for i in tab: + if i.state == state: + return False + return True + + +def BFS(istate): + fringe = [] + explored = [] + + x = Node.Node(istate) + fringe.append(x) + + while True: + if fringe == []: + return False + + elem = fringe.pop(0) + + if goalTest(elem.state["hydradeIndex"]): + x = elem + tab = [] + while x.parent != None: + tab.append(x.action) + x = x.parent + return tab + + explored.append(elem) + + for resp in succ(elem.state): + if check(fringe, resp[1]) and check(explored, resp[1]): + x = Node.Node(resp[1]) + x.parent = elem + x.action = resp[0] + fringe.append(x) +""" + + + + + + + + diff --git a/Image.py b/Image.py index 4bd8136..a924bd0 100644 --- a/Image.py +++ b/Image.py @@ -6,6 +6,7 @@ class Image: def __init__(self): self.plants_image_dict={} self.tractor_image=None + self.garage_image=None def load_images(self): files_plants={0:"borowka", 1:"kukurydza", @@ -19,6 +20,8 @@ class Image: self.plants_image_dict[files_plants[index]]=plant_image tractor_image=pygame.image.load("images/traktor.png") tractor_image=pygame.transform.scale(tractor_image,(dCon.CUBE_SIZE,dCon.CUBE_SIZE)) + garage=pygame.image.load("images/garage.png") + self.garage_image=pygame.transform.scale(garage,(dCon.CUBE_SIZE,dCon.CUBE_SIZE)) def return_random_plant(self): x=random.randint(0,5) keys=list(self.plants_image_dict.keys()) @@ -26,4 +29,7 @@ class Image: return (plant,self.plants_image_dict[plant]) def return_plant(self,plant_name): - return (plant_name,self.plants_image_dict[plant_name]) \ No newline at end of file + return (plant_name,self.plants_image_dict[plant_name]) + + def return_garage(self): + return self.garage_image \ No newline at end of file diff --git a/Node.py b/Node.py new file mode 100644 index 0000000..cc88608 --- /dev/null +++ b/Node.py @@ -0,0 +1,8 @@ +class Node: + state = None #[{stan}] + parent = None #[Node] + action = None #[Forward/Right/Left] + + def __init__(self, state): + self.state = state + diff --git a/Pole.py b/Pole.py index 017848e..dc7d82e 100644 --- a/Pole.py +++ b/Pole.py @@ -33,13 +33,18 @@ class Pole: slot_dict=self.get_slot_dict() for coordinates in slot_dict: slot_dict[coordinates].draw() + garage=self.slot_dict[(0,0)] + garage.set_garage_image() def randomize_colors(self): pygame.display.update() time.sleep(3) self.ui.render_text("Randomizing Crops") for coordinates in self.slot_dict: - self.slot_dict[coordinates].set_random_plant() + if(coordinates==(0,0)): + continue + else: + self.slot_dict[coordinates].set_random_plant() def change_color_of_slot(self,coordinates,color): #Coordinates must be tuple (x,y) (left top slot has cord (0,0) ), color has to be from defined in Colors.py or custom in RGB value (R,G,B) self.get_slot_from_cord(coordinates).color_change(color) @@ -55,5 +60,8 @@ class Pole: def check_collision(self,mouse_x,mouse_y): mouse_x=math.floor(mouse_x/dCon.CUBE_SIZE) mouse_y=math.floor(mouse_y/dCon.CUBE_SIZE) - collided=self.get_slot_from_cord((mouse_x,mouse_y)) - return collided.print_status() \ No newline at end of file + if(mouse_x=7): + self.font_size=int(30) + self.line=30 + self.first_line=30 + def render_text(self,string_to_print): font=pygame.font.Font(self.font,self.font_size) text=font.render(string_to_print,True,Colors.BLACK,Colors.WHITE) textRect=text.get_rect() - textRect.center=(dCon.getScreenWidth() // 2,dCon.getScreenHeihgt() // 2) - self.screen.blit(text,textRect) \ No newline at end of file + textRect.center=(dCon.getGameWidth() // 2,dCon.getScreenHeihgt() // 2) + self.screen.blit(text,textRect) + + def render_text_to_console(self,string_to_print): + font=pygame.font.Font(self.font,self.font_size) + self.break_string_to_console(string_to_print) + for string in self.to_print: + text=font.render(string,True,Colors.BLACK,Colors.WHITE) + textRect=text.get_rect() + textRect.center=(dCon.getGameWidth()+350/2,self.line) + textRect.scale_by(x=350,y=100) + self.screen.blit(text,textRect) + self.line=self.line+self.first_line + def clear_console(self): + self.line=self.first_line + pygame.draw.rect(self.screen,(0,0,0) , pygame.Rect(dCon.returnConsoleCoordinate(), 0, dCon.getConsoleWidth(), dCon.getScreenHeihgt()), 0) + + def break_string_to_console(self,string_to_print): + self.to_print=string_to_print.split(" ") diff --git a/displayControler.py b/displayControler.py index ea90442..3d774d8 100644 --- a/displayControler.py +++ b/displayControler.py @@ -1,6 +1,6 @@ CUBE_SIZE = 64 -NUM_X = 20 -NUM_Y = 12 +NUM_X = 6 +NUM_Y = 3 #returns true if tractor can move to specified slot def isValidMove(x, y): @@ -10,8 +10,28 @@ def isValidMove(x, y): return False return True -def getScreenWidth(): + +def getGameWidth(): return NUM_X * CUBE_SIZE +def returnConsoleCoordinate(): + return NUM_X * CUBE_SIZE + + + + def getScreenHeihgt(): - return NUM_Y * CUBE_SIZE \ No newline at end of file + return NUM_Y * CUBE_SIZE + + +def getScreenWidth(show_console): + screen_width=getGameWidth() + if(show_console): + screen_width=screen_width+350 + return screen_width + +def getConsoleWidth(): + return 350 + +def getConsoleWidthCenter(): + return getScreenWidth()+getConsoleWidth()/2 \ No newline at end of file diff --git a/images/garage.png b/images/garage.png new file mode 100644 index 0000000..f9ca1c0 Binary files /dev/null and b/images/garage.png differ diff --git a/images/traktorN.png b/images/traktorN.png new file mode 100644 index 0000000..940e09a Binary files /dev/null and b/images/traktorN.png differ diff --git a/images/traktorS.png b/images/traktorS.png new file mode 100644 index 0000000..0020c3d Binary files /dev/null and b/images/traktorS.png differ diff --git a/images/traktorW.png b/images/traktorW.png new file mode 100644 index 0000000..4120a16 Binary files /dev/null and b/images/traktorW.png differ