Merge branch 'losowanie_celu' into astar_temp

This commit is contained in:
jakzar 2024-04-28 11:21:09 +02:00
commit eb17e42686
6 changed files with 57 additions and 18 deletions

View File

@ -13,6 +13,14 @@ from Pole import stoneList
from queue import PriorityQueue 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): def heuristic(state, goal):
# Oblicz odległość Manhattanowską między aktualnym stanem a celem # Oblicz odległość Manhattanowską między aktualnym stanem a celem
manhattan_distance = abs(state['x'] - goal[0]) + abs(state['y'] - goal[1]) 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 return 0
def A_star(istate, pole): def A_star(istate, pole, goalTreasure):
#goalTreasure = (5, 5) # goalTreasure = (random.randint(0,NUM_X-1), random.randint(0,NUM_Y-1))
while True: # #jeśli chcemy używać random musimy wykreslić sloty z kamieniami, ponieważ tez mogą się wylosować i wtedy traktor w ogóle nie rusza
goalTreasure = (random.randint(0, NUM_X - 1), random.randint(0, NUM_Y - 1)) # Współrzędne celu #lub zrobić to jakoś inaczej, np. funkcja szukająca najmniej nawodnionej rośliny
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 fringe = PriorityQueue() # Kolejka priorytetowa dla wierzchołków do rozpatrzenia
explored = [] # Lista odwiedzonych stanów explored = [] # Lista odwiedzonych stanów
obrot = 1 obrot = 1
@ -164,14 +177,16 @@ def heuristic2(state, goal):
return manhattan_distance 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)) # 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 # #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 #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 # przeniesione wyżej do funkcji getRandomGoalTreasure, wykorzystywana jest w App.py
if goalTreasure not in stoneList: # while True:
break # 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 fringe = PriorityQueue() # Kolejka priorytetowa dla wierzchołków do rozpatrzenia
explored = [] # Lista odwiedzonych stanów explored = [] # Lista odwiedzonych stanów

11
App.py
View File

@ -9,6 +9,7 @@ import Osprzet
import Ui import Ui
import BFS import BFS
import AStar import AStar
import random
bfs1_flag=False bfs1_flag=False
@ -27,7 +28,8 @@ FPS=5
clock=pygame.time.Clock() clock=pygame.time.Clock()
image_loader=Image.Image() image_loader=Image.Image()
image_loader.load_images() 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 pole.draw_grid() #musi byc tutaj wywołane ponieważ inicjalizuje sloty do slownika
ui=Ui.Ui(screen) ui=Ui.Ui(screen)
#Tractor creation #Tractor creation
@ -77,7 +79,7 @@ def init_demo(): #Demo purpose
print_to_console("Traktor porusza sie obliczona sciezka BFS") print_to_console("Traktor porusza sie obliczona sciezka BFS")
traktor.move_by_root(bfsRoot3, pole, [traktor.irrigateSlot]) traktor.move_by_root(bfsRoot3, pole, [traktor.irrigateSlot])
if (Astar): 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: if aStarRoot:
#print("Pełna ścieżka agenta:") #print("Pełna ścieżka agenta:")
aStarRoot.reverse() aStarRoot.reverse()
@ -90,7 +92,8 @@ def init_demo(): #Demo purpose
else: else:
print_to_console("Nie można znaleźć ścieżki A*") # Wyświetl komunikat, jeśli nie znaleziono ścieżki print_to_console("Nie można znaleźć ścieżki A*") # Wyświetl komunikat, jeśli nie znaleziono ścieżki
if (Astar2): 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: if aStarRoot2:
aStarRoot2.reverse() aStarRoot2.reverse()
print_to_console("Traktor porusza się obliczoną ścieżką A*") print_to_console("Traktor porusza się obliczoną ścieżką A*")
@ -136,3 +139,5 @@ def get_info(old_info):

View File

@ -8,8 +8,10 @@ class Image:
self.tractor_image=None self.tractor_image=None
self.garage_image=None self.garage_image=None
self.stone_image=None self.stone_image=None
self.gasStation_image=None
def load_images(self): def load_images(self):
files_plants={0:"borowka", files_plants={
0:"borowka",
1:"kukurydza", 1:"kukurydza",
2:"pszenica", 2:"pszenica",
3:"slonecznik", 3:"slonecznik",
@ -30,6 +32,8 @@ class Image:
self.garage_image=pygame.transform.scale(garage,(dCon.CUBE_SIZE,dCon.CUBE_SIZE)) self.garage_image=pygame.transform.scale(garage,(dCon.CUBE_SIZE,dCon.CUBE_SIZE))
stone=pygame.image.load("images/stone.png") stone=pygame.image.load("images/stone.png")
self.stone_image=pygame.transform.scale(stone,(dCon.CUBE_SIZE,dCon.CUBE_SIZE)) 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): def return_random_plant(self):
x=random.randint(0,7) x=random.randint(0,7)
@ -45,3 +49,6 @@ class Image:
def return_stone(self): def return_stone(self):
return self.stone_image return self.stone_image
def return_gasStation(self):
return self.gasStation_image

13
Pole.py
View File

@ -5,16 +5,18 @@ import pygame
import time import time
import Ui import Ui
import math 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)] 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 stoneFlag = False
class Pole: class Pole:
def __init__(self,screen,image_loader): def __init__(self,screen,image_loader, gasStation = (-1, -1)):
self.screen=screen 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.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.ui=Ui.Ui(screen)
self.image_loader=image_loader self.image_loader=image_loader
self.gasStation=gasStation
def get_slot_from_cord(self,coordinates): def get_slot_from_cord(self,coordinates):
(x_axis,y_axis)=coordinates (x_axis,y_axis)=coordinates
@ -42,13 +44,16 @@ class Pole:
for i in stoneList: for i in stoneList:
st=self.slot_dict[i] st=self.slot_dict[i]
st.set_stone_image() st.set_stone_image()
if self.gasStation[0] != -1:
st=self.slot_dict[self.gasStation]
st.set_gasStation_image()
def randomize_colors(self): def randomize_colors(self):
pygame.display.update() pygame.display.update()
time.sleep(3) time.sleep(3)
#self.ui.render_text("Randomizing Crops") #self.ui.render_text("Randomizing Crops")
for coordinates in self.slot_dict: 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 continue
else: else:
self.slot_dict[coordinates].set_random_plant() self.slot_dict[coordinates].set_random_plant()
@ -71,4 +76,6 @@ class Pole:
if(mouse_y<dCon.NUM_Y): if(mouse_y<dCon.NUM_Y):
collided=self.get_slot_from_cord((mouse_x,mouse_y)) collided=self.get_slot_from_cord((mouse_x,mouse_y))
return collided.print_status() return collided.print_status()
return "" return ""

View File

@ -49,6 +49,11 @@ class Slot:
self.plant_image=self.image_loader.return_stone() self.plant_image=self.image_loader.return_stone()
self.screen.blit(self.plant_image, (self.x_axis * dCon.CUBE_SIZE, self.y_axis * dCon.CUBE_SIZE)) self.screen.blit(self.plant_image, (self.x_axis * dCon.CUBE_SIZE, self.y_axis * dCon.CUBE_SIZE))
pygame.draw.rect(self.screen, Colors.BLACK, self.field, BORDER_THICKNESS) pygame.draw.rect(self.screen, Colors.BLACK, self.field, BORDER_THICKNESS)
def set_gasStation_image(self):
self.plant_image=self.image_loader.return_gasStation()
self.screen.blit(self.plant_image, (self.x_axis * dCon.CUBE_SIZE, self.y_axis * dCon.CUBE_SIZE))
pygame.draw.rect(self.screen, Colors.BLACK, self.field, BORDER_THICKNESS)
def random_plant(self): #Probably will not be used later only for demo purpouse def random_plant(self): #Probably will not be used later only for demo purpouse
return self.image_loader.return_random_plant() return self.image_loader.return_random_plant()

BIN
images/gasStation.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 82 KiB