From 61d7ab2fe3f59c8b1ac7d40845513c522cdb7d17 Mon Sep 17 00:00:00 2001 From: Weranda Date: Wed, 3 May 2023 13:29:17 +0200 Subject: [PATCH] =?UTF-8?q?bfs=20dzia=C5=82aj=C4=85cy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- __pycache__/agent.cpython-310.pyc | Bin 5655 -> 5655 bytes __pycache__/bfs.cpython-310.pyc | Bin 0 -> 4695 bytes __pycache__/main.cpython-310.pyc | Bin 0 -> 3071 bytes agent.py | 3 - main.py | 144 +++++++++++++++--------------- 5 files changed, 72 insertions(+), 75 deletions(-) create mode 100644 __pycache__/bfs.cpython-310.pyc create mode 100644 __pycache__/main.cpython-310.pyc diff --git a/__pycache__/agent.cpython-310.pyc b/__pycache__/agent.cpython-310.pyc index cf340ad5bc68127b2ccec70f49846bfdf9ce538b..9f226b8d946e8ea581d014c545ddd33c57c158dc 100644 GIT binary patch delta 22 ccmbQPGhK%_pO=@50SG2K1f|T`$jc=L06X*rC;$Ke delta 22 ccmbQPGhK%_pO=@50SG=T2BzHH$jc=L06&-orvLx| diff --git a/__pycache__/bfs.cpython-310.pyc b/__pycache__/bfs.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..db4bae0c2d167c235f243f782a911dfcc1aa149a GIT binary patch literal 4695 zcmb7I&2Jn@74NF<>FMeD@YtEyjyIsr5o)idJ{WfkaAeZBYU z)vH(aK59DUvTNY`@V$rPE3X>HUubapGckAyQsN2VwV=UL1Vy~gH?lpFTCV@_W9D{dJVu30!R+%tXxnP2SWUQLP z6c$=A7B#w2MNwNHMZCm-!cUs_N)VoVc%Q1LpwDN^CSES zd&G|z;Sck4U`Koaoej9i4NPGVtkjghOPsC3p?hT9Ha;v4a%pa>2uNw0%eRyAfx(R3 zH$|B)&lO1LyQy%qyji=IlV;sX-wv0H51q@;ACy)xAs` z1A948) z&9SVd{WP1G=ApHJ(+Nd|)Wv$58&S~drB?h$NO>{knT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0 : - current_node_cell = self.open_queue.pop(0) # remove and get first element of open_queue + current_node_cell = self.open_queue.pop(0) - # check if this cell allready processed if(current_node_cell in self.close_queue): continue - print("current cell number is: ", current_node_cell) - print("goal cell/s is/are at: ", goal_cell) + print("Aktualna kratka: ", current_node_cell) + print("Cel znajduje się na kratce: ", goal_cell) - # cheking for goal if (current_node_cell == goal_cell): - # add the goal cell to close queue too, this is helpfull to find the path self.close_queue.append(current_node_cell) found_goal_cell = current_node_cell - print("goal found, now find the path from close_queue, close_queue is:", self.close_queue) + print("Znaleziono cel, szukanie drogi z odwiedzonych węzłów, kolejka odwiedzonych:", self.close_queue) processing = False find_path = True - self.clock.tick(2) # stay some time here without closing the windoew + self.clock.tick(2) else: - # goal not found, continue processing - child_node_cells = self.get_child_nodes(current_node_cell) # find possible child cells nodes to process - self.close_queue.append(current_node_cell) # putting the processed node to closed queue - - # add child nodes to open queue only if they are already not in both open_queue and close_queue - print("childe nodes: ", child_node_cells) + child_node_cells = self.get_child_nodes(current_node_cell) + self.close_queue.append(current_node_cell) + print("Sąsiedzi: ", child_node_cells) for child_node in child_node_cells: if child_node not in self.open_queue and child_node not in self.close_queue: - self.open_queue.append(child_node) # add children to END OF THE OPEN QUEUE (BFS) - print("open queue: ", self.open_queue, "open_queue length: ", len(self.open_queue), "\n") + self.open_queue.append(child_node) + print("Kolejka: ", self.open_queue, "\n") else: - print("no nodes to processe, open_queue length: ", len(self.open_queue), ", open queue: ",self. open_queue) - print("closed queue (processed nodes) : ", self.close_queue) + print("Brak nowych węzłów, kolejka: ",self. open_queue) + print("Odwiedzone : ", self.close_queue) return self.close_queue dead_end_nodes = [] while find_path: - path.append(self.close_queue[0]) # put the agent starting cell to path. + path.append(self.close_queue[0]) for i in range(len(self.close_queue) -1): from_cell = path[-1] @@ -147,22 +144,20 @@ class Game: if to_cell in dead_end_nodes: continue - if self.verify_to_cell_is_navigatable_from_from_cell(from_cell, to_cell): + if self.verify_move(from_cell, to_cell): path.append(to_cell) if path[-1] == found_goal_cell: find_path = False else: - # a dead end has occured, start finding path avoiding this dead end cell dead_end_nodes.append(path[-1]) - path = [] # to start again + path = [] - print("path: ", path) + print("Droga: ", path) self.move_agent(path) - - def get_cell_number(self,x, y): + def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki cell_number = None cell_number =(x // TILE_SIZE) + (NUM_ROWS * (( y// TILE_SIZE))) return cell_number @@ -187,83 +182,88 @@ class Game: return children + def get_up_cell(self,cell_number): - cell_row_number = cell_number // NUM_ROWS # current row number of agent - if (cell_row_number - 1 < 0): # above /up row number of agent + cell_row_number = cell_number // NUM_ROWS + if (cell_row_number - 1 < 0): return None else: return (cell_number - NUM_ROWS) def get_right_cell(self,cell_number): - cell_column_number = cell_number % NUM_ROWS # current column number of agent + cell_column_number = cell_number % NUM_ROWS if (cell_column_number + 1 >= NUM_ROWS): - # current cell is at the right edge, so no rigth child / right cell available return None else: - return (cell_number + 1) # else return next cell number + return (cell_number + 1) def get_down_cell(self,cell_number): - cell_row_number = cell_number // NUM_ROWS # current row number of agent - if (cell_row_number + 1 >= NUM_ROWS): # down / next row number of agent + cell_row_number = cell_number // NUM_ROWS + if (cell_row_number + 1 >= NUM_ROWS): return None else: return (cell_number + NUM_ROWS) def get_left_cell(self,cell_number): - cell_column_number = cell_number % NUM_ROWS # current column number of agent + cell_column_number = cell_number % NUM_ROWS if (cell_column_number - 1 < 0): - # current cell is at the left edge, so no left child / right cell available return None else: - return (cell_number - 1) # else return previous cell number + return (cell_number - 1) - def verify_to_cell_is_navigatable_from_from_cell(self,from_cell, to_cell): - if (to_cell in self.wall_cells or to_cell in self.enemy_cells): # if to_cell is a wall cell, return False + def verify_move(self,from_cell, to_cell): #sprawdzenie czy ruch jest poprawny czyt. czy następna kratka to przeszkoda lub mob + if (to_cell in self.wall_cells or to_cell in self.enemy_cells): return False - if(from_cell + 1 == to_cell): # check to_cell is the right cell + if(from_cell + 1 == to_cell): return True - if(from_cell - 1 == to_cell): # check to_cell is the left cell + if(from_cell - 1 == to_cell): return True - if(from_cell - NUM_ROWS == to_cell): # check to_cell is the top / up cell + if(from_cell - NUM_ROWS == to_cell): return True - if(from_cell + NUM_ROWS == to_cell): # check to_cell is the down / bottom cell + if(from_cell + NUM_ROWS == to_cell): return True - return False # Else not navigatable, return False - - - # trzeba poprawić poruszanie się agenta, sam bfs działa dobrze raczej + return False + def move_agent(self,path): for cell_to_move in path: - x, y = self.get_top_left_cordinates_given_cell_number(cell_to_move) - print("moving to cell : ", cell_to_move, " of cordinates x: ", x, ", y: ", y, ", line_width: ", TILE_SIZE) - if x > self.agent.x and y == self.agent.y: - self.agent.direction = 0 - if x == self.agent.x and y > self.agent.y: - self.agent.direction = 1 - if x < self.agent.x and y == self.agent.y: - self.agent.direction = 2 - if x == self.agent.x and y < self.agent.y: - self.agent.direction = 3 - if self.agent.direction==0: - self.agent.x += TILE_SIZE - if self.agent.direction==1: - self.agent.y += TILE_SIZE - if self.agent.direction==2: - self.agent.x -= TILE_SIZE - if self.agent.direction==3: - self.agent.y -= TILE_SIZE + x, y = self.get_coordinates(cell_to_move) + print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y) + if(self.get_cell_number(self.agent.x,self.agent.y)!=cell_to_move): + if x > self.agent.rect.x: + self.agent.direction = 0 + elif y > self.agent.rect.y: + self.agent.direction = 1 + elif x < self.agent.rect.x: + self.agent.direction = 2 + elif y < self.agent.rect.y: + self.agent.direction = 3 + if self.agent.direction==0: + print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) + self.agent.x_change += TILE_SIZE + elif self.agent.direction==1: + print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) + self.agent.y_change += TILE_SIZE + elif self.agent.direction==2: + print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) + self.agent.x_change -= TILE_SIZE + elif self.agent.direction==3: + print("DIRECTION: "+self.agent.AGENT_IMAGES[self.agent.direction]) + self.agent.y_change -= TILE_SIZE - print("moved agent attributes: agent.x: ", self.agent.x, ", agent.y: ", self.agent.y) + self.update() + self.map() + + print("Położenie agenta: agent.x: ", self.agent.rect.x, ", agent.y: ", self.agent.rect.y) self.clock.tick(2) - def get_top_left_cordinates_given_cell_number(self,cell_to_move): - cell_row_number = cell_to_move // NUM_ROWS # cell row number - cell_column_number = cell_to_move % NUM_ROWS # cell column number + def get_coordinates(self,cell_to_move): #zamienia numer kratki na koordynaty + cell_row_number = cell_to_move // NUM_ROWS + cell_column_number = cell_to_move % NUM_ROWS y = cell_row_number * TILE_SIZE x = cell_column_number * TILE_SIZE