Merge pull request 'finish_bfs' (#21) from finish_bfs into master

Reviewed-on: #21
This commit is contained in:
Paweł Felcyn 2023-04-23 16:45:17 +02:00
commit eb7857323c
3 changed files with 23 additions and 12 deletions

6
bfs.py
View File

@ -83,7 +83,13 @@ def get_next_cell(state: AgentState) -> Tuple[int, int]:
def is_state_success(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool: def is_state_success(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool:
next_cell = get_next_cell(state) next_cell = get_next_cell(state)
try:
return grid[next_cell] == GridCellType.GARBAGE_CAN return grid[next_cell] == GridCellType.GARBAGE_CAN
except:
return False
def is_state_valid(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool: def is_state_valid(state: AgentState, grid: Dict[Tuple[int, int], GridCellType]) -> bool:
try:
return grid[state.position] == GridCellType.STREET_HORIZONTAL or grid[state.position] == GridCellType.STREET_VERTICAL return grid[state.position] == GridCellType.STREET_HORIZONTAL or grid[state.position] == GridCellType.STREET_VERTICAL
except:
return False

12
main.py
View File

@ -4,7 +4,7 @@ from gameContext import GameContext
from startup import startup from startup import startup
from PIL import Image from PIL import Image
from agentActionType import AgentActionType from agentActionType import AgentActionType
from movement import move_dust_car from movement import collect_garbage
pygame.init() pygame.init()
@ -18,15 +18,7 @@ game_context.dust_car_pil = dust_car_pil
game_context.dust_car_pygame = pygame.image.frombuffer(dust_car_pil.tobytes(), dust_car_pil.size, 'RGB') game_context.dust_car_pygame = pygame.image.frombuffer(dust_car_pil.tobytes(), dust_car_pil.size, 'RGB')
game_context.canvas = canvas game_context.canvas = canvas
startup(game_context) startup(game_context)
collect_garbage(game_context)
# test = [AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD,
# AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD,
# AgentActionType.MOVE_FORWARD, AgentActionType.TURN_RIGHT, AgentActionType.MOVE_FORWARD,
# AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD,
# AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD, AgentActionType.MOVE_FORWARD,
# AgentActionType.TURN_LEFT, AgentActionType.MOVE_FORWARD]
# move_dust_car(test, game_context)
exit = False exit = False

View File

@ -7,6 +7,19 @@ from gridCellType import GridCellType
from gameContext import GameContext from gameContext import GameContext
from agentOrientation import AgentOrientation from agentOrientation import AgentOrientation
import pygame import pygame
from bfs import find_path_to_nearest_can
from agentState import AgentState
def collect_garbage(game_context: GameContext) -> None:
while True:
start_agent_state = AgentState(game_context.dust_car.position, game_context.dust_car.orientation)
path = find_path_to_nearest_can(start_agent_state, game_context.grid)
if path == None or len(path) == 0:
break
move_dust_car(path, game_context)
next_position = calculate_next_position(game_context.dust_car)
game_context.grid[next_position] = GridCellType.VISITED_GARBAGE_CAN
pass
def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None: def move_dust_car(actions: list[AgentActionType], game_context: GameContext) -> None:
for action in actions: for action in actions: