diff --git a/AStar.py b/AStar.py index a01f507..63d87dc 100644 --- a/AStar.py +++ b/AStar.py @@ -13,6 +13,14 @@ from Pole import stoneList from queue import PriorityQueue +def getRandomGoalTreasure(): + while True: + goalTreasure = (random.randint(0, NUM_X - 1), random.randint(0, NUM_Y - 1)) # Współrzędne celu + if goalTreasure not in stoneList: + break + return goalTreasure + + def heuristic(state, goal): # Oblicz odległość Manhattanowską między aktualnym stanem a celem manhattan_distance = abs(state['x'] - goal[0]) + abs(state['y'] - goal[1]) @@ -37,12 +45,17 @@ def get_cost_for_plant(plant_name): return 0 -def A_star(istate, pole): - #goalTreasure = (5, 5) - while True: - goalTreasure = (random.randint(0, NUM_X - 1), random.randint(0, NUM_Y - 1)) # Współrzędne celu - if goalTreasure not in stoneList: - break +def A_star(istate, pole, goalTreasure): + # goalTreasure = (random.randint(0,NUM_X-1), random.randint(0,NUM_Y-1)) + # #jeśli chcemy używać random musimy wykreslić sloty z kamieniami, ponieważ tez mogą się wylosować i wtedy traktor w ogóle nie rusza + #lub zrobić to jakoś inaczej, np. funkcja szukająca najmniej nawodnionej rośliny + + # przeniesione wyżej do funkcji getRandomGoalTreasure, wykorzystywana jest w App.py + # while True: + # goalTreasure = (random.randint(0, NUM_X - 1), random.randint(0, NUM_Y - 1)) # Współrzędne celu + # if goalTreasure not in stoneList: + # break + fringe = PriorityQueue() # Kolejka priorytetowa dla wierzchołków do rozpatrzenia explored = [] # Lista odwiedzonych stanów obrot = 1 @@ -164,14 +177,16 @@ def heuristic2(state, goal): return manhattan_distance -def A_star2(istate, pole): +def A_star2(istate, pole, goalTreasure): # goalTreasure = (random.randint(0,NUM_X-1), random.randint(0,NUM_Y-1)) # #jeśli chcemy używać random musimy wykreslić sloty z kamieniami, ponieważ tez mogą się wylosować i wtedy traktor w ogóle nie rusza #lub zrobić to jakoś inaczej, np. funkcja szukająca najmniej nawodnionej rośliny - while True: - goalTreasure = (random.randint(0, NUM_X - 1), random.randint(0, NUM_Y - 1)) # Współrzędne celu - if goalTreasure not in stoneList: - break + + # przeniesione wyżej do funkcji getRandomGoalTreasure, wykorzystywana jest w App.py + # while True: + # goalTreasure = (random.randint(0, NUM_X - 1), random.randint(0, NUM_Y - 1)) # Współrzędne celu + # if goalTreasure not in stoneList: + # break fringe = PriorityQueue() # Kolejka priorytetowa dla wierzchołków do rozpatrzenia explored = [] # Lista odwiedzonych stanów diff --git a/App.py b/App.py index 9f9b664..506b06e 100644 --- a/App.py +++ b/App.py @@ -9,6 +9,7 @@ import Osprzet import Ui import BFS import AStar +import random bfs1_flag=False @@ -27,7 +28,8 @@ FPS=5 clock=pygame.time.Clock() image_loader=Image.Image() image_loader.load_images() -pole=Pole.Pole(screen,image_loader) +goalTreasure = AStar.getRandomGoalTreasure() # nie wiem czy to najlepsze miejsce, obecnie pole zawiera pole gasStation, które służy do renderowania odpowiedniego zdjęcia +pole=Pole.Pole(screen,image_loader, goalTreasure) pole.draw_grid() #musi byc tutaj wywołane ponieważ inicjalizuje sloty do slownika ui=Ui.Ui(screen) #Tractor creation @@ -77,7 +79,7 @@ def init_demo(): #Demo purpose print_to_console("Traktor porusza sie obliczona sciezka BFS") traktor.move_by_root(bfsRoot3, pole, [traktor.irrigateSlot]) if (Astar): - aStarRoot = AStar.A_star({'x': 0, 'y': 0, 'direction': "E"}, pole) + aStarRoot = AStar.A_star({'x': 0, 'y': 0, 'direction': "E"}, pole, goalTreasure) if aStarRoot: #print("Pełna ścieżka agenta:") aStarRoot.reverse() @@ -90,7 +92,8 @@ def init_demo(): #Demo purpose else: print_to_console("Nie można znaleźć ścieżki A*") # Wyświetl komunikat, jeśli nie znaleziono ścieżki if (Astar2): - aStarRoot2 = AStar.A_star2({'x': 0, 'y': 0, 'direction': "E"}, pole) + + aStarRoot2 = AStar.A_star2({'x': 0, 'y': 0, 'direction': "E"}, pole, goalTreasure) if aStarRoot2: aStarRoot2.reverse() print_to_console("Traktor porusza się obliczoną ścieżką A*") @@ -136,3 +139,5 @@ def get_info(old_info): + + diff --git a/Image.py b/Image.py index 3bd0a42..41a9cca 100644 --- a/Image.py +++ b/Image.py @@ -8,8 +8,10 @@ class Image: self.tractor_image=None self.garage_image=None self.stone_image=None + self.gasStation_image=None def load_images(self): - files_plants={0:"borowka", + files_plants={ + 0:"borowka", 1:"kukurydza", 2:"pszenica", 3:"slonecznik", @@ -30,6 +32,8 @@ class Image: self.garage_image=pygame.transform.scale(garage,(dCon.CUBE_SIZE,dCon.CUBE_SIZE)) stone=pygame.image.load("images/stone.png") self.stone_image=pygame.transform.scale(stone,(dCon.CUBE_SIZE,dCon.CUBE_SIZE)) + gasStation=pygame.image.load("images/gasStation.png") + self.gasStation_image=pygame.transform.scale(gasStation,(dCon.CUBE_SIZE,dCon.CUBE_SIZE)) def return_random_plant(self): x=random.randint(0,7) @@ -45,3 +49,6 @@ class Image: def return_stone(self): return self.stone_image + + def return_gasStation(self): + return self.gasStation_image diff --git a/Pole.py b/Pole.py index 34e534f..f0e5b8b 100644 --- a/Pole.py +++ b/Pole.py @@ -5,16 +5,18 @@ import pygame import time import Ui import math +import random stoneList = [(3,3), (3,4), (3,5), (3,6), (4,6), (5,6), (6,6), (7,6), (8,6), (9,6), (10,6), (11,6), (12,6), (13,6), (14,6), (15,6), (16,6), (16,7), (16,8), (16,9)] stoneFlag = False class Pole: - def __init__(self,screen,image_loader): + def __init__(self,screen,image_loader, gasStation = (-1, -1)): self.screen=screen self.slot_dict={} #Slot are stored in dictionary with key being a Tuple of x and y coordinates so top left slot key is (0,0) and value is slot object self.ui=Ui.Ui(screen) self.image_loader=image_loader + self.gasStation=gasStation def get_slot_from_cord(self,coordinates): (x_axis,y_axis)=coordinates @@ -42,13 +44,16 @@ class Pole: for i in stoneList: st=self.slot_dict[i] st.set_stone_image() + if self.gasStation[0] != -1: + st=self.slot_dict[self.gasStation] + st.set_gasStation_image() def randomize_colors(self): pygame.display.update() time.sleep(3) #self.ui.render_text("Randomizing Crops") for coordinates in self.slot_dict: - if(coordinates==(0,0) or coordinates in stoneList): + if(coordinates==(0,0) or coordinates in stoneList or coordinates == self.gasStation): continue else: self.slot_dict[coordinates].set_random_plant() @@ -71,4 +76,6 @@ class Pole: if(mouse_y