From 34a22a054aabce945e040918ac5c3e3229c4e72f Mon Sep 17 00:00:00 2001 From: enne2 Date: Fri, 16 Feb 2024 00:51:51 +0100 Subject: [PATCH] Refactor knight.py and main.py --- Units/__pycache__/knight.cpython-311.pyc | Bin 6421 -> 4023 bytes Units/knight.py | 70 +++++------------------ main.py | 54 +++++++++++++++-- solver.py | 1 + 4 files changed, 64 insertions(+), 61 deletions(-) diff --git a/Units/__pycache__/knight.cpython-311.pyc b/Units/__pycache__/knight.cpython-311.pyc index 68c4f914c3b1f9964cbce119128df4f0c81c188a..b4b4b0854d97f330c0e8206bf6d2c8e6332223f6 100644 GIT binary patch delta 1169 zcmZuwT}TvB6ux)X!_HB0F{0LDc)4PRatsUsrP@?*Fb-v?>@ zK(6&-m@8q8as``LYoV*$`>Cq31aXb&+6JG0HFrkmOYTQRh848c=siRf;Jl5l0r%XO zA3_hF@E*H%T}dlxoE9#M{6bkqo4^e$;BUevJM53*?d*pCC^P|bSws)wGImHm8KoiM zc_lV9WXDOqqSsDY7X}hZJDIZLHr&AZ1f>R~&>)9eo-9IA#6wMHmL2p_9YXHqoMWwl z8qX$9UttytJlmV~E{9uYy5`P0;g*+@(|Tedd~#8*%X;r_%NgOh+Sf*O$%wu<;4~jw zG};}bJ*zDn!|Zr@Rat0@6Kc##x7D0kpOqXv#FopecL14-zBb#I%(j;u3!SH(&Ynfn za!kuj$!!Vq(OA%p;4zYb7j_h68`mkhH zvd!76>NpO{fjJ!3p)QvL7u;One9+Ru<&U(Oa+E+hm=yzB1PA`-T8vKA6-p+V_b`9itm1eER#}`oW)tbKpJssZyjpuOZ z92jQ?$k+$Xh(|5{e!FH8?vtZh9Tyb-NXEfUVMM>FTi2$)!Ad|lZMavb9wD~Og2&(z z{%JU4&+9)R62}ZWyi_Or%W!%qZ@{WWHAjfIn({{8$dgM1cZw&stg5AUlAcs8`{(Y^qi}=G=!f92;%rO&=1Bc1QO} zgZ&D>F*FXX^&R8=e{n(9xQMLv@nn|HwH$I`^v45dh!2P|xZ!?V%P8kjeG^1Nt{kO3!t&Dtogh9U7{kY! z zu!5gmrIv6#?YBd+azcfup6cS>I}+u8SF;yPjH#xW^o$YX8U&zsHptJi(vphC`SH^{ z){%Z(n^(rHa^BwxaGfQpWM=^evtY2(R}YKte)iVNfSzjqZX4 zd#~OXBXcnvL?p93qv~5ts#ZHPa9MC~`f z5geu;feXS%rc25#je<}g<-R~csiPkXVPVIn#(Mwg*VO3pjmjw zJW?oIX4%{n6VK+N=^2)jUsRk+LEzZ=%o1DHM;8{@G*dRPu^2m(%W^u|N-l8mbgoRv zhi6imEOs%aQY!0m@p+b$i<9&1Lq*d}X1;9T@F*YCrr0zm|Gscm`A*AP4C8Cy&I@GS z4+>>ElVYljrea)XekvE`@RqXkqy_Wla%Ia5n@UYB#k27o%W!@flTNeC-+^+{vXAnT zY$&KJ@k6xzL#KeQ7l`L7oJS9dR=Yol;%9E~MJe-inV(C*o5 z^==PU2u(weBG!i_XUooqUY>yWk}#AKLn(x%V;(u?q0*&h*F#JXG|VYbbKoRY;UpFdLoqQFLs&Xykz*Dr zeb3oeA?SuA_=;-W42hHvQ9elda6sPn^$4O`MXFVqY_FC)14YlkGaJ172D&>fdS;Mk zM%f~TFYc_C!UM(dz%w0seH>1_RX`Jr7-mtJg;cu~>exw^Lf4C->$^ELa2JlhiN@1n zD1$;7NChNk7+xGKItL-{!?6l}!GkNBzY)YOJm8r;d_|xA8MWQ`)28hvc&=x^i*zrd zdm-H`1;dc`OWw{3VQdU3qOke8)Y@3%g=)ei4S3#PAZrl3GU8 zGNhIzuTRcFn?yH1v_7VGnu_!#NM9;QzTi$%$#=Er!xjETFC4jpMxvr`8u_L-bq{UN zzor`D8NWyc5EYPffGhsSR{wfrV@Sdml2`J&_n&ae!SJFOUPM?@ONd&6N+= old_distance_from_target or new_distance_from_start < old_distance_from_start: - print(f"{time.time()} - No progress") - new_neighbors_list = [] - for neighbor in neighbors_list: - neighbor_neighbors_list = self.engine.find_neighbors(neighbor) - for neighbor_neighbor in neighbor_neighbors_list: - # consider only the neighbors of the neighbors that are walkable - if not self.engine.battlefield[neighbor_neighbor[1]][neighbor_neighbor[0]].walkable: - if neighbor_neighbor not in self.visited: - # if the neighbor of the neighbor is not walkable and has not been visited, add it to the list of visited cells - new_neighbors_list.append(neighbor) - break - # scegli uhn vicino casualmente come destinaziione - - self.destination = new_neighbors_list[random.randint(0,len(new_neighbors_list)-1)] - - if self.destination: - self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="purple")) - self.partial_move = 0 - if self.position != self.target: - self.direction = self.engine.get_direction(self.position,self.destination) - else: - print("No destination") - self.visited = [self.position] - self.destination = self.position - #self.target = self.position - self.engine.effects.append(OrderClick(self.engine.iso_transform(*self.destination),color="brown")) - + self.destination = self.engine.get_closest_neighbor(neighbors_list,self.target) + self.partial_move = 0 else: self.visited = [self.target] self.position = self.target @@ -101,4 +55,8 @@ class Knight: self.state = "Idle" self.partial_move = 1 gif = self.animation.get(f'Knight_{self.state}_dir{self.direction}.gif') - return gif, screen_x, screen_y \ No newline at end of file + return gif, screen_x, screen_y + + def move_to(self, target): + self.target = target + self.state = "Walk" diff --git a/main.py b/main.py index 48ac36c..3fa6f13 100644 --- a/main.py +++ b/main.py @@ -7,6 +7,7 @@ import json from Units.knight import Knight from Effects.order_click import OrderClick from tkinter import Menu +from collections import deque @@ -141,7 +142,40 @@ class IsometricGame: x1, y1 = position1 x2, y2 = position2 return abs(((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5) - + def get_distance_components(self, position1, position2): + x1, y1 = position1 + x2, y2 = position2 + return abs(x2 - x1), abs(y2 - y1) + #function to check if a cell is near a wall + def near_wall(self, position): + x, y = position + neighbors = self.find_neighbors(position) + for neighbor in neighbors: + if not self.battlefield[neighbor[1]][neighbor[0]].walkable: + return True + def min_steps_to_target(self, start, target): + grid = [[0 for _ in range(self.width)] for _ in range(self.height)] + rows, cols = len(grid), len(grid[0]) + dx = [0, 1, 0, -1] + dy = [1, 0, -1, 0] + queue = deque([start]) + visited = set([start]) + + steps = 0 + while queue: + for _ in range(len(queue)): + x, y = queue.popleft() + if (x, y) == target: + return steps + + for i in range(4): + nx, ny = x + dx[i], y + dy[i] + if 0 <= nx < rows and 0 <= ny < cols and (nx, ny) not in visited: + queue.append((nx, ny)) + visited.add((nx, ny)) + steps += 1 + + return -1 def draw_battlefield(self): self.canvas.delete("cell") # Pulisce le celle precedenti prima di ridisegnare self.canvas.delete("cell-label") @@ -235,6 +269,7 @@ class IsometricGame: # calculate the clicked cell's coordinates cell_x, cell_y = self.inv_iso_transform(event.x, event.y) self.battlefield[cell_y][cell_x].walkable ^= True + self.battlefield[cell_y][cell_x].tile = self.tiles["landscapeTiles_067"] if self.battlefield[cell_y][cell_x].walkable else self.tiles["landscapeTiles_066"] self.draw_battlefield() self.draw_units() @@ -260,10 +295,8 @@ class IsometricGame: return if 0 <= cell_x < self.width and 0 <= cell_y < self.height: - self.knight.starting_position = self.knight.position - self.knight.target = (cell_x,cell_y) - self.knight.state = "Walk" - + self.knight.move_to((cell_x,cell_y)) + def run(self): self.draw_battlefield() self.update() @@ -288,6 +321,17 @@ class Cell: if __name__ == "__main__": with open('maze.json', 'r') as file: data = json.load(file) + # Creare una matrice 10x10 piena di False + room = [[True for _ in range(10)] for _ in range(10)] + + # Cambiare i valori interni della matrice in True + for i in range(1, 9): + for j in range(1, 8): + room[i][j] = False + + # Creare un'apertura di un'unità verso l'esterno + room[0][4] = True # Cambia questo valore per spostare l'apertura + data =room # data is a boolean matrix, find dmensions width = len(data[0]) height = len(data) diff --git a/solver.py b/solver.py index 37c1f1e..421ab77 100644 --- a/solver.py +++ b/solver.py @@ -70,5 +70,6 @@ class MazeSolver: self.window.mainloop() # Create and run the maze solver + #Breadth-First Search, BFS solver = MazeSolver('maze.json') solver.run() \ No newline at end of file