a * wypisuje droge

This commit is contained in:
Paralyzecl 2023-05-05 14:56:42 +02:00
parent 0e183ca1cf
commit 635d448799
2 changed files with 124 additions and 8 deletions

16
bfs.py
View File

@ -10,10 +10,11 @@ class Bfs:
self.close_queue = []
self.wall_cells = []
self.enemy_cells = []
def heuristic(a,b):
return abs(a[0]-b[0])+abs(a[1]-b[1])
def bfs(self):
print("x: ", self.game.agent.x, "y: ", self.game.agent.y)
cost_road=0
self.open_queue.append(self.get_cell_number(self.game.agent.x,self.game.agent.y))
# tutaj dodaje się cel agenta
goal_cell = self.get_cell_number(self.game.flower.x,self.game.flower.y)
@ -33,25 +34,25 @@ class Bfs:
continue
print("Aktualna kratka: ", current_node_cell)
print("Cel znajduje się na kratce: ", goal_cell)
print("Cel znajduje sie na kratce: ", goal_cell)
if (current_node_cell == goal_cell):
self.close_queue.append(current_node_cell)
found_goal_cell = current_node_cell
print("Znaleziono cel, szukanie drogi z odwiedzonych węzłów, kolejka odwiedzonych:", self.close_queue)
print("Znaleziono cel, szukanie drogi z odwiedzonych wezlow, kolejka odwiedzonych:", self.close_queue)
processing = False
find_path = True
self.game.clock.tick(2)
else:
child_node_cells = self.get_child_nodes(current_node_cell)
self.close_queue.append(current_node_cell)
print("Sąsiedzi: ", child_node_cells)
print("Sasiedzi: ", 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)
print("Kolejka: ", self.open_queue, "\n")
else:
print("Brak nowych węzłów, kolejka: ",self. open_queue)
print("Brak nowych wezlow, kolejka: ",self. open_queue)
print("Odwiedzone : ", self.close_queue)
return self.close_queue
@ -152,6 +153,7 @@ class Bfs:
return False
def move_agent(self,path):
print("PATH:::::",path)
for cell_to_move in path:
x, y = self.get_coordinates(cell_to_move)
print("Ruch do kratki : ", cell_to_move, " z x: ", x, ", y: ", y, ", agent.x: ", self.game.agent.rect.x, ", agent.y: ", self.game.agent.rect.y)
@ -181,7 +183,7 @@ class Bfs:
self.game.update()
self.game.map()
print("Położenie agenta: agent.x: ", self.game.agent.rect.x, ", agent.y: ", self.game.agent.rect.y)
print("Polozenie agenta: agent.x: ", self.game.agent.rect.x, ", agent.y: ", self.game.agent.rect.y)
self.game.clock.tick(2)
def get_coordinates(self,cell_to_move): #zamienia numer kratki na koordynaty

114
main.py
View File

@ -6,6 +6,7 @@ from mobs import *
#from unknown_mob import * #unknown mob
import random
from bfs import *
from heapq import *
class Game:
@ -50,16 +51,27 @@ class Game:
self.bfs.enemy_cells.append(self.bfs.get_cell_number(self.sauron.x,self.sauron.y))
self.flower = Health_flower(self, 8,2)
#self.unknown_mob = Unknown_mob(self,8,8) #unknown mob
self.grass = Grass(self,0,2)
self.grass = Grass(self,1,2)
self.grass = Grass(self,0,3)
self.grass = Grass(self,1,3)
self.grass = Grass(self,0,4)
self.grass = Grass(self,1,4)
cost_cell_1000=[13,26,27,40]
for y in range(5):
self.rock = Rocks(self,3,y)
self.bfs.wall_cells.append(self.bfs.get_cell_number(self.rock.x,self.rock.y))
'''
for i in range(10):
x = random.randint(0,12)
y = random.randint(0,11)
self.grass = Grass(self,x,y)
self.grass_cells.append(self.bfs.get_cell_number(self.grass.x,self.grass.y))
for y in range(5,8):
self.little_rocks = Little_Rocks(self,4,y)
'''
@ -113,6 +125,108 @@ class Game:
# BFS ALGORITHM
cols, rows = 13,12
def get_circle(x,y):
return (x * TILE_SIZE + TILE_SIZE//2, y* TILE_SIZE + TILE_SIZE//2), TILE_SIZE//4
def get_rect(x,y):
return x*TILE_SIZE +1, y* TILE_SIZE +1, TILE_SIZE -2, TILE_SIZE -2
'''
def get_next_nodes(x,y):
check_next_node = lambda x, y:True if 0<= x < cols and 0<=y < rows else False
ways =[-1,0],[0,-1],[1,0],[0,1]
return [(grid[y + dy][x + dx], (x + dx, y + dy)) for dx, dy in ways if check_next_node(x + dx, y + dy)]
'''
def get_neighbours(x, y):
check_neighbour = lambda x, y: True if 0 <= x < cols and 0 <= y < rows else False
ways = [-1, 0], [0, -1], [1, 0], [0, 1]
return [(grid[y + dy][x + dx], (x + dx, y + dy)) for dx, dy in ways if check_neighbour(x + dx, y + dy)]
def heuristic(a, b):
return abs(a[0] - b[0]) + abs(a[1] - b[1])
def dijkstra(start, goal, graph):
queue = []
heappush(queue, (0, start))
cost_visited = {start: 0}
visited = {start: None}
while queue:
cur_cost, cur_node = heappop(queue)
if cur_node == goal:
break
neighbours = graph[cur_node]
for neighbour in neighbours:
neigh_cost, neigh_node = neighbour
new_cost = cost_visited[cur_node] + neigh_cost
if neigh_node not in cost_visited or new_cost < cost_visited[neigh_node]:
priority = new_cost + heuristic(neigh_node, goal)
heappush(queue, (priority, neigh_node))
cost_visited[neigh_node] = new_cost
visited[neigh_node] = cur_node
return visited
grid =['2229222222222',
'2229222222222',
'9929222222222',
'9929222222222',
'9929222222222',
'2222222222222',
'2222222222222',
'2222222222222',
'2222222222222',
'2222222222222',
'2222222222222',
'2222222222222'
]
grid = [[int(char) for char in string ] for string in grid]
graph ={}
for y, row in enumerate(grid):
for x, col in enumerate(row):
graph[(x, y)] = graph.get((x, y), []) + get_neighbours(x, y)
print("graph 2 0",graph[(2,0)])
start = (1,1)
goal =(0,5)
queue =[]
heappush(queue, (0,start))
cost_visited = {start:0}
visited = {start: None}
goall=1
while goall==1:
if queue:
visited=dijkstra(start,goal,graph)
goall=0
path=[]
path_head, path_segment = goal, goal
while path_segment:
print("path_segment: ",path_segment)
path_segment =visited[path_segment]
path.append(path_segment)
print("path_head",path_head)
path.pop(len(path)-1)
path.reverse()
path_true=[]
bfss =Bfs(Game)
for i in path:
z=str(i)
print("Z:",z)
x=z[1]
y=z[4]
x=int(x)*64
y=int(y)*64
a=bfss.get_cell_number(x,y)
path_true.append(a)
print("path:",path)
print("path_true:",path_true)
bfss.move_agent(path_true)
g = Game()
g.new()