Gra-SI/bfs.py

130 lines
4.1 KiB
Python

from config import *
import queue
class Bfs():
def __init__(self,game):
self.game = game
self.wall_cells = []
self.enemy_cells = []
def heuristic(a,b):
return abs(a[0]-b[0])+abs(a[1]-b[1])
def bfs(self,goal_cell):
print("x: ", self.game.agent.rect.x, "y: ", self.game.agent.rect.y)
visited = set()
q = queue.Queue()
start_position = self.get_cell_number(self.game.agent.rect.x,self.game.agent.rect.y)
q.put(start_position)
parent = {}
while not q.empty(): # główna pętla
current_pos = q.get()
visited.add(current_pos)
if current_pos == goal_cell:
print("Osiągnięto cel, konstruowanie drogi")
path = []
while current_pos != start_position:
path.append(current_pos)
current_pos = parent[current_pos]
path.append(start_position)
return path[::-1]
for successor in self.succesors(current_pos):
if successor not in visited:
q.put(successor)
parent[successor] = current_pos
print("Nieznaleziono drogi")
return None
def get_cell_number(self,x, y): #zamienia koordynaty na numer kratki
cell_number = None
cell_number =(x // TILE_SIZE) + (ROWS * (( y// TILE_SIZE)))
return cell_number
def get_possible_moves(self,cell_number):
children = []
up = self.get_up_cell(cell_number)
if up is not None and up not in self.wall_cells and up not in self.enemy_cells:
children.append(up)
right = self.get_right_cell(cell_number)
if right is not None and right not in self.wall_cells and up not in self.enemy_cells:
children.append(right)
down = self.get_down_cell(cell_number)
if down is not None and down not in self.wall_cells and up not in self.enemy_cells:
children.append(down)
left = self.get_left_cell(cell_number)
if left is not None and left not in self.wall_cells and up not in self.enemy_cells:
children.append(left)
return children
def get_up_cell(self,cell_number):
cell_row_number = cell_number // ROWS
if (cell_row_number - 1 < 0):
return None
else:
return (cell_number - ROWS)
def get_right_cell(self,cell_number):
cell_column_number = cell_number % ROWS
if (cell_column_number + 1 >= ROWS):
return None
else:
return (cell_number + 1)
def get_down_cell(self,cell_number):
cell_row_number = cell_number // ROWS
if (cell_row_number + 1 >= ROWS):
return None
else:
return (cell_number + ROWS)
def get_left_cell(self,cell_number):
cell_column_number = cell_number % ROWS
if (cell_column_number - 1 < 0):
return None
else:
return (cell_number - 1)
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):
return True
if(from_cell - 1 == to_cell):
return True
if(from_cell - ROWS == to_cell):
return True
if(from_cell + ROWS == to_cell):
return True
return False
def get_coordinates(self,cell_to_move): #zamienia numer kratki na koordynaty
cell_row_number = cell_to_move // ROWS
cell_column_number = cell_to_move % ROWS
y = cell_row_number * TILE_SIZE
x = cell_column_number * TILE_SIZE
return x, y
def succesors(self,current_pos):
possible_moves = self.get_possible_moves(current_pos)
valid_moves = []
for move in possible_moves:
if self.verify_move(current_pos,move) == True:
valid_moves.append(move)
return valid_moves