From 63229b2c6094a9e4134c5aedf5adbfbf75379e13 Mon Sep 17 00:00:00 2001 From: Mateusz Czajka Date: Sun, 14 Apr 2024 21:39:04 +0200 Subject: [PATCH] Made BFS1 and BFS3 algorithm in BFS.py file. --- App.py | 17 ++- BFS.py | 254 ++++++++++++++++++++++++++++++++++++++++++++ Tractor.py | 95 ++--------------- displayControler.py | 2 +- 4 files changed, 276 insertions(+), 92 deletions(-) create mode 100644 BFS.py diff --git a/App.py b/App.py index be9f2d5..6a994a7 100644 --- a/App.py +++ b/App.py @@ -7,6 +7,7 @@ import displayControler as dCon import Image import Osprzet import Ui +import BFS pygame.init() @@ -37,10 +38,20 @@ def init_demo(): #Demo purpose ui.render_text_to_console(string_to_print="Przejazd inicjalizujacy- traktor sprawdza poziom nawodnienia") traktor.initial_move(pole) traktor.reset_pos(pole) - bfsRoot = Tractor.BFS({"x": 0, "y": 0, "direction": "E", "hydradeIndex": traktor.slot_hydrate_dict}) - bfsRoot.reverse() + + bfsRoot1 = BFS.BFS1({"x": 0, "y": 0, "direction": "E", "hydradeIndex": traktor.slot_hydrate_dict}) #displayControler: NUM_X: 6, NUM_Y: 3 (klasyczne) + #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) + #bfsRoot3 = BFS.BFS2({'x': 0, 'y': 0, 'direction': "E"}) #displayControler: NUM_X: 20, NUM_Y: 12 (skarb) + + bfsRoot1.reverse() + #bfsRoot2.reverse() + #bfsRoot3.reverse() # ui.render_text_to_console(string_to_print="traktor porusza się ścieżką bfs") - traktor.move_by_root(bfsRoot, pole, [traktor.irrigateSlot]) + + traktor.move_by_root(bfsRoot1, pole, [traktor.irrigateSlot]) + #traktor.move_by_root(bfsRoot2, pole, [traktor.irrigateSlot]) + #traktor.move_by_root(bfsRoot3, pole, [traktor.irrigateSlot]) + start_flag=False # demo_move() old_info=get_info(old_info) diff --git a/BFS.py b/BFS.py new file mode 100644 index 0000000..23cb7f3 --- /dev/null +++ b/BFS.py @@ -0,0 +1,254 @@ +import random +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) + + + + +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) + + + + +""" +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/Tractor.py b/Tractor.py index 8cbee1f..74c8645 100644 --- a/Tractor.py +++ b/Tractor.py @@ -8,90 +8,6 @@ import Node -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) - - - - - class Tractor: DIRECTION_NORTH = 'N' DIRECTION_SOUTH = 'S' @@ -198,7 +114,9 @@ class Tractor: def snake_move(self,pole,x,y): next_slot_coordinates=(x,y) if(self.do_move_if_valid(pole,next_slot_coordinates)): - if pole.get_slot_from_cord((x,y)).get_hydrate_stats() < 60: + if x == 0 and y == 0: + hydrateIndex = -1 + elif pole.get_slot_from_cord((x,y)).get_hydrate_stats() < 60: hydrateIndex = 0 else: hydrateIndex = 1 @@ -207,14 +125,15 @@ class Tractor: for event in pygame.event.get(): if event.type == pygame.QUIT: quit() + def move_by_root(self, root, pole, actions = None): for move in root: self.slot.redraw_image() - if move == 'forward': + if move[1] == 'forward': self.move_forward(pole) - if move == 'right': + if move[1] == 'right': self.turn_right() - if move == 'left': + if move[1] == 'left': self.turn_left() for a in actions: a() diff --git a/displayControler.py b/displayControler.py index cad2f16..f5cb408 100644 --- a/displayControler.py +++ b/displayControler.py @@ -1,5 +1,5 @@ CUBE_SIZE = 64 -NUM_X = 5 +NUM_X = 6 NUM_Y = 3 #returns true if tractor can move to specified slot