import time import pygame import random import displayControler as dCon import Slot import Osprzet 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' DIRECTION_WEST = 'W' DIRECTION_EAST = 'E' def __init__(self,slot,screen, osprzet,clock): self.tractor_images = { Tractor.DIRECTION_NORTH: pygame.transform.scale(pygame.image.load('images/traktorN.png'), (dCon.CUBE_SIZE, dCon.CUBE_SIZE)), Tractor.DIRECTION_SOUTH: pygame.transform.scale(pygame.image.load('images/traktorS.png'), (dCon.CUBE_SIZE, dCon.CUBE_SIZE)), Tractor.DIRECTION_WEST: pygame.transform.scale(pygame.image.load('images/traktorW.png'), (dCon.CUBE_SIZE, dCon.CUBE_SIZE)), Tractor.DIRECTION_EAST: pygame.transform.scale(pygame.image.load('images/traktor.png'), (dCon.CUBE_SIZE, dCon.CUBE_SIZE)) } self.direction = Tractor.DIRECTION_EAST # początkowy kierunek wschód self.current_tractor_image = self.tractor_images[self.direction] self.screen=screen self.slot=slot self.osprzet = osprzet self.clock=clock self.slot_hydrate_dict={} def draw_tractor(self): self.screen.blit(self.current_tractor_image, (self.slot.x_axis * dCon.CUBE_SIZE, self.slot.y_axis * dCon.CUBE_SIZE)) pygame.display.update() def turn_left(self): # zmiana kierunku w lewo direction_map = { Tractor.DIRECTION_EAST: Tractor.DIRECTION_NORTH, Tractor.DIRECTION_NORTH: Tractor.DIRECTION_WEST, Tractor.DIRECTION_WEST: Tractor.DIRECTION_SOUTH, Tractor.DIRECTION_SOUTH: Tractor.DIRECTION_EAST } self.direction = direction_map[self.direction] self.current_tractor_image = self.tractor_images[self.direction] self.draw_tractor() def turn_right(self): # zmiana kierunku w prawo direction_map = { Tractor.DIRECTION_EAST: Tractor.DIRECTION_SOUTH, Tractor.DIRECTION_SOUTH: Tractor.DIRECTION_WEST, Tractor.DIRECTION_WEST: Tractor.DIRECTION_NORTH, Tractor.DIRECTION_NORTH: Tractor.DIRECTION_EAST } self.direction = direction_map[self.direction] self.current_tractor_image = self.tractor_images[self.direction] self.draw_tractor() def move_forward(self, pole): next_slot_coordinates = None if self.direction == Tractor.DIRECTION_EAST: next_slot_coordinates = (self.slot.x_axis + 1, self.slot.y_axis) self.current_tractor_image = self.tractor_images[self.direction] elif self.direction == Tractor.DIRECTION_WEST: next_slot_coordinates = (self.slot.x_axis - 1, self.slot.y_axis) self.current_tractor_image = self.tractor_images[self.direction] elif self.direction == Tractor.DIRECTION_SOUTH: next_slot_coordinates = (self.slot.x_axis, self.slot.y_axis + 1) self.current_tractor_image = self.tractor_images[self.direction] elif self.direction == Tractor.DIRECTION_NORTH: next_slot_coordinates = (self.slot.x_axis, self.slot.y_axis - 1) self.current_tractor_image = self.tractor_images[self.direction] # sprawdzenie czy następny slot jest dobry self.do_move_if_valid(pole,next_slot_coordinates) def do_move_if_valid(self,pole, next_slot_coordinates): if next_slot_coordinates and pole.is_valid_move(next_slot_coordinates): next_slot = pole.get_slot_from_cord(next_slot_coordinates) self.slot.redraw_image() self.slot = next_slot self.draw_tractor() return True else: return False def random_move(self, pole): self.clock.tick(2) # losowanie skrętu turn_direction = random.choice([self.turn_left, self.turn_right]) turn_direction() self.clock.tick(5) # wykonanie ruchu do przodu z uwzględnieniem aktualnej orientacji self.move_forward(pole) def reset_pos(self,pole): self.do_move_if_valid(pole,(0,0)) def initial_move(self,pole): for y in range (0,dCon.NUM_Y): if(y%2==0): for x in range(0,dCon.NUM_X): self.snake_move(pole,x,y) else: for x in range(dCon.NUM_X,-1,-1): self.snake_move(pole,x,y) 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: hydrateIndex = 0 else: hydrateIndex = 1 self.slot_hydrate_dict[(x,y)]= hydrateIndex #Budowanie slownika slotow z poziomem nawodnienia dla traktorka self.clock.tick(10) 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': self.move_forward(pole) if move == 'right': self.turn_right() if move == 'left': self.turn_left() for a in actions: a() self.clock.tick(3) #to tak zrobiłam już na później, może się przyda def change_osprzet(self, new_osprzet): self.osprzet = new_osprzet def print_osprzet_info(self): print("ID:", self.osprzet.id) print("Marka:", self.osprzet.marka) print("Model:", self.osprzet.model) if self.osprzet.akcje: print("Akcje:") for akcja in self.osprzet.akcje: print("- Typ:", akcja.typ) else: print("Brak akcji przypisanych do tego sprzętu.") def irrigateSlot(self): self.slot.irrigatePlant()