From 6c271999881351b47a265b76b23afb6bbc0e266e Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Mon, 11 Apr 2022 23:53:50 +0200 Subject: [PATCH] feat: result function --- algorithms/a_star.py | 46 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/algorithms/a_star.py b/algorithms/a_star.py index 46da7e6..524eca5 100644 --- a/algorithms/a_star.py +++ b/algorithms/a_star.py @@ -11,6 +11,10 @@ RIGHT = 'RIGHT' UP = 'UP' DOWN = 'DOWN' +TURN_LEFT = 'TURN_LEFT' +TURN_RIGHT = 'TURN_RIGHT' +FORWARD = 'FORWARD' + directions = { LEFT: (0, -1), RIGHT: (0, 1), @@ -18,10 +22,6 @@ directions = { DOWN: (1, 0) } -TURN_LEFT = 'TURN_LEFT' -TURN_RIGHT = 'TURN_RIGHT' -FORWARD = 'FORWARD' - @dataclass class State: @@ -93,8 +93,42 @@ def remove_forward(possible_actions: List[str]) -> None: possible_actions.remove(FORWARD) -def result(state: State, action: str, grid: List[List[str]]) -> State: - pass +def result(state: State, action: str) -> State: + new_state = State(state.position, state.direction) + + if state.direction == UP: + if action == TURN_LEFT: + new_state.direction = LEFT + elif action == TURN_RIGHT: + new_state.direction = RIGHT + elif action == FORWARD: + new_state.position = next_position(state.position, UP) + + elif state.direction == DOWN: + if action == TURN_LEFT: + new_state.direction = RIGHT + elif action == TURN_RIGHT: + new_state.direction = LEFT + elif action == FORWARD: + new_state.position = next_position(state.position, DOWN) + + elif state.direction == LEFT: + if action == TURN_LEFT: + new_state.direction = DOWN + elif action == TURN_RIGHT: + new_state.direction = UP + elif action == FORWARD: + new_state.position = next_position(state.position, LEFT) + + elif state.direction == RIGHT: + if action == TURN_LEFT: + new_state.direction = UP + elif action == TURN_RIGHT: + new_state.direction = DOWN + elif action == FORWARD: + new_state.position = next_position(state.position, RIGHT) + + return new_state def goal_test(state: State, goal_list: List[Tuple[int, int]]) -> bool: