feat new bfs, finding agent actions
This commit is contained in:
parent
c5105d426d
commit
d186c2de5c
47
app.py
47
app.py
@ -56,26 +56,43 @@ initBoard()
|
||||
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
|
||||
|
||||
#Wpisujemy miejsce w ktorym znajduje sie agent i miejsce docelowe do funkcji znajdujacej najkrotsza sciezke
|
||||
start_cell = cells[5][5]
|
||||
target_cell = cells[5][5]
|
||||
shortest_path = agent.bfs(start_cell, target_cell, cells)
|
||||
# start_cell = cells[5][5]
|
||||
# target_cell = cells[5][5]
|
||||
# shortest_path = agent.bfs(start_cell, target_cell, cells)
|
||||
|
||||
if shortest_path:
|
||||
print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path])
|
||||
else:
|
||||
print("Brak sciezki")
|
||||
# if shortest_path:
|
||||
# print("Sciezka: ", [(cell.X, cell.Y) for cell in shortest_path])
|
||||
# else:
|
||||
# print("Brak sciezki")
|
||||
|
||||
path = agent.convert_to_coordinates(shortest_path)
|
||||
# path = agent.convert_to_coordinates(shortest_path)
|
||||
|
||||
#osobny watek dla sciezki agenta zeby co *iles czasu* poruszal sie tam gdzie mowi path
|
||||
def watekDlaSciezkiAgenta():
|
||||
while True:
|
||||
agent.sciezkaAgenta(agent,path)
|
||||
time.sleep(1)
|
||||
# def watekDlaSciezkiAgenta():
|
||||
# while True:
|
||||
# agent.sciezkaAgenta(agent,path)
|
||||
# time.sleep(1)
|
||||
|
||||
# watek = threading.Thread(target=watekDlaSciezkiAgenta)
|
||||
# watek.daemon = True
|
||||
# watek.start()
|
||||
|
||||
#target_x, target_y = 9,6
|
||||
#target_x, target_y = 5,2
|
||||
#target_x, target_y = 7,2
|
||||
#target_x, target_y = 8,2
|
||||
target_x, target_y = 4,8
|
||||
#target_x, target_y = 0,9
|
||||
#target_x, target_y = 9,11
|
||||
#target_x, target_y = 4,11
|
||||
#target_x, target_y = 6,3
|
||||
#target_x, target_y = 11,11
|
||||
|
||||
#WYWOLUJEMY FUNKCJE SZUKAJACA LISTY AKCJI
|
||||
shortest_path = agent.bfs2(target_x, target_y)
|
||||
print("Najkrótsza ścieżka:", shortest_path)
|
||||
|
||||
|
||||
watek = threading.Thread(target=watekDlaSciezkiAgenta)
|
||||
watek.daemon = True
|
||||
watek.start()
|
||||
|
||||
running = True
|
||||
while running:
|
||||
|
@ -14,6 +14,9 @@ class Agent:
|
||||
self.score = baseScore
|
||||
self.multiplier = 1
|
||||
self.direction = 0
|
||||
self.directionPOM = 0
|
||||
self.xPOM = 5
|
||||
self.yPOM = 5
|
||||
|
||||
self.textures = [
|
||||
pygame.image.load("sprites/BartenderNew64.png").convert_alpha(),
|
||||
@ -93,7 +96,7 @@ class Agent:
|
||||
|
||||
if current in visited:
|
||||
continue
|
||||
|
||||
|
||||
visited.add(current)
|
||||
|
||||
for neighbor in self.get_neighbors(current, cells):
|
||||
@ -177,7 +180,84 @@ class Agent:
|
||||
self.current_cell = self.cells[self.current_cell.X+1][self.current_cell.Y]
|
||||
self.moved=True
|
||||
self.last_move_time=pygame.time.get_ticks()
|
||||
|
||||
|
||||
|
||||
|
||||
def get_possible_moves(self):
|
||||
possible_moves = []
|
||||
|
||||
if self.directionPOM == 0: # Patrzy w dół
|
||||
possible_moves.append((0, 'left'))
|
||||
possible_moves.append((0, 'right'))
|
||||
if self.yPOM < prefs.GRID_SIZE - 1 and not self.cells[self.xPOM][self.yPOM + 1].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
elif self.directionPOM == 1: # Patrzy w lewo
|
||||
possible_moves.append((1, 'left'))
|
||||
possible_moves.append((1, 'right'))
|
||||
if self.xPOM > 0 and not self.cells[self.xPOM - 1][self.yPOM].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
elif self.directionPOM == 2: # Patrzy w górę
|
||||
possible_moves.append((2, 'left'))
|
||||
possible_moves.append((2, 'right'))
|
||||
if self.yPOM > 0 and not self.cells[self.xPOM][self.yPOM - 1].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
elif self.directionPOM == 3: # Patrzy w prawo
|
||||
possible_moves.append((3, 'left'))
|
||||
possible_moves.append((3, 'right'))
|
||||
if self.xPOM < prefs.GRID_SIZE - 1 and not self.cells[self.xPOM + 1][self.yPOM].blocking_movement:
|
||||
possible_moves.append((self.directionPOM, 'forward'))
|
||||
|
||||
return possible_moves
|
||||
|
||||
|
||||
def bfs2(self, target_x, target_y):
|
||||
visited = set()
|
||||
self.directionPOM = self.direction
|
||||
start_state = (self.current_cell.X, self.current_cell.Y, self.directionPOM)
|
||||
#print(start_state)
|
||||
queue = deque([(start_state, [])])
|
||||
|
||||
while queue:
|
||||
(self.xPOM, self.yPOM, self.directionPOM), actions = queue.popleft()
|
||||
#print(x," ", y, " ", self.directionPOM, " ", actions)
|
||||
|
||||
if self.xPOM == target_x and self.yPOM == target_y:
|
||||
return actions
|
||||
|
||||
if (self.xPOM, self.yPOM, self.directionPOM) in visited:
|
||||
continue
|
||||
|
||||
#print(x," ",y," ",direction)
|
||||
visited.add((self.xPOM, self.yPOM, self.directionPOM))
|
||||
|
||||
possible_moves = self.get_possible_moves()
|
||||
#print(possible_moves)
|
||||
for new_direction, action in possible_moves:
|
||||
new_x, new_y = self.xPOM, self.yPOM
|
||||
new_actions = actions + [action]
|
||||
|
||||
if action == 'left':
|
||||
new_direction = (self.directionPOM + 1) % 4
|
||||
elif action == 'right':
|
||||
new_direction = (self.directionPOM - 1) % 4
|
||||
else: # forward
|
||||
if self.directionPOM == 0:
|
||||
new_y += 1
|
||||
elif self.directionPOM == 1:
|
||||
new_x -= 1
|
||||
elif self.directionPOM == 2:
|
||||
new_y -= 1
|
||||
else: # direction == 3
|
||||
new_x += 1
|
||||
|
||||
if 0 <= new_x < prefs.GRID_SIZE and 0 <= new_y < prefs.GRID_SIZE \
|
||||
and not self.cells[new_x][new_y].blocking_movement:
|
||||
new_state = (new_x, new_y, new_direction)
|
||||
queue.append((new_state, new_actions))
|
||||
#print(new_state, " ", new_actions)
|
||||
|
||||
return []
|
||||
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user