From 7131d1f905c1680990c9347fa08ec3da9228e7d4 Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Tue, 12 Apr 2022 19:12:41 +0200 Subject: [PATCH] feat: movement --- algorithms/a_star.py | 13 +++++++------ logic/level.py | 43 ++++++++++++++++++++----------------------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/algorithms/a_star.py b/algorithms/a_star.py index 5d45dd3..67f0924 100644 --- a/algorithms/a_star.py +++ b/algorithms/a_star.py @@ -29,6 +29,9 @@ class State: position: Tuple[int, int] direction: str + def __eq__(self, other: State) -> bool: + return other.position == self.position and self.direction == other.direction + def __lt__(self, state): return self.position < state.position @@ -44,16 +47,13 @@ class Node: cost: int = field(init=False) depth: int = field(init=False) - def __lt__(self, node): + def __lt__(self, node) -> None: return self.state < node.state def __post_init__(self) -> None: self.cost = 0 if not self.parent else self.parent.cost + 1 self.depth = self.cost - def __eq__(self, other: Node) -> bool: - return self.state == other.state - def __hash__(self) -> int: return hash(self.state) @@ -158,11 +158,12 @@ def f(current_node: Node, goal: Tuple[int, int]) -> int: def get_path_from_start(node: Node) -> List[str]: - path = [node] + path = [node.action] while node.parent is not None: node = node.parent - path.append(node.action) + if node.action: + path.append(node.action) path.reverse() return path diff --git a/logic/level.py b/logic/level.py index 1485f6f..e657bc3 100644 --- a/logic/level.py +++ b/logic/level.py @@ -2,7 +2,7 @@ import random import pygame -from algorithms.a_star import a_star, State +from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD, UP, DOWN, LEFT, RIGHT from common.constants import * from common.helpers import castle_neighbors from logic.knights_queue import KnightsQueue @@ -112,32 +112,29 @@ class Level: state = State((knight_pos_y, knight_pos_x), current_knight.direction.name) action_list = a_star(state, self.map, goal_list) - - for action in action_list: - print(action) - print() + print(action_list) if len(action_list) == 0: return - # next_action = action_list.pop(0) - # if next_action == ACTION.get("rotate_left"): - # current_knight.rotate_left() - # elif next_action == ACTION.get("rotate_right"): - # current_knight.rotate_right() - # elif next_action == ACTION.get("go"): - # current_knight.step_forward() - # self.map[knight_pos_y][knight_pos_x] = ' ' - # - # # update knight on map - # if current_knight.direction.name == 'UP': - # self.map[knight_pos_y - 1][knight_pos_x] = current_knight - # elif current_knight.direction.name == 'RIGHT': - # self.map[knight_pos_y][knight_pos_x + 1] = current_knight - # elif current_knight.direction.name == 'DOWN': - # self.map[knight_pos_y + 1][knight_pos_x] = current_knight - # elif current_knight.direction.name == 'LEFT': - # self.map[knight_pos_y][knight_pos_x - 1] = current_knight + next_action = action_list.pop(0) + if next_action == TURN_LEFT: + current_knight.rotate_left() + elif next_action == TURN_RIGHT: + current_knight.rotate_right() + elif next_action == FORWARD: + current_knight.step_forward() + self.map[knight_pos_y][knight_pos_x] = ' ' + + # update knight on map + if current_knight.direction.name == UP: + self.map[knight_pos_y - 1][knight_pos_x] = current_knight + elif current_knight.direction.name == RIGHT: + self.map[knight_pos_y][knight_pos_x + 1] = current_knight + elif current_knight.direction.name == DOWN: + self.map[knight_pos_y + 1][knight_pos_x] = current_knight + elif current_knight.direction.name == LEFT: + self.map[knight_pos_y][knight_pos_x - 1] = current_knight def update(self): bg_width = (GRID_CELL_PADDING + GRID_CELL_SIZE) * COLUMNS + BORDER_WIDTH