From 8b76d3635b2a74b03f0ccabc54ae670209098a87 Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Sun, 8 May 2022 19:26:09 +0200 Subject: [PATCH] finding neighbour tiles --- algorithms/a_star.py | 14 ++------------ common/constants.py | 5 +++++ common/helpers.py | 31 ++++++++++++++++++++++++++++++- logic/level.py | 7 ++++--- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/algorithms/a_star.py b/algorithms/a_star.py index 1b8db00..d615ad9 100644 --- a/algorithms/a_star.py +++ b/algorithms/a_star.py @@ -4,25 +4,15 @@ import heapq from dataclasses import dataclass, field from typing import Tuple, Optional, List -from common.constants import ROWS, COLUMNS +from common.constants import ROWS, COLUMNS, LEFT, RIGHT, UP, DOWN +from common.helpers import directions EMPTY_FIELDS = ['s', 'g', ' '] -LEFT = 'LEFT' -RIGHT = 'RIGHT' -UP = 'UP' -DOWN = 'DOWN' TURN_LEFT = 'TURN_LEFT' TURN_RIGHT = 'TURN_RIGHT' FORWARD = 'FORWARD' -directions = { - LEFT: (0, -1), - RIGHT: (0, 1), - UP: (-1, 0), - DOWN: (1, 0) -} - @dataclass class State: diff --git a/common/constants.py b/common/constants.py index caf995f..6d91768 100644 --- a/common/constants.py +++ b/common/constants.py @@ -67,3 +67,8 @@ ACTION = { BAR_ANIMATION_SPEED = 1 BAR_WIDTH_MULTIPLIER = 0.9 # (0;1> BAR_HEIGHT_MULTIPLIER = 0.1 + +LEFT = 'LEFT' +RIGHT = 'RIGHT' +UP = 'UP' +DOWN = 'DOWN' diff --git a/common/helpers.py b/common/helpers.py index dd13e31..eef852f 100644 --- a/common/helpers.py +++ b/common/helpers.py @@ -1,5 +1,16 @@ +from typing import Tuple, List + import pygame -from common.constants import GRID_CELL_PADDING, GRID_CELL_SIZE, COLUMNS, ROWS + +from common.constants import GRID_CELL_PADDING, GRID_CELL_SIZE +from common.constants import ROWS, COLUMNS, LEFT, RIGHT, UP, DOWN + +directions = { + LEFT: (0, -1), + RIGHT: (0, 1), + UP: (-1, 0), + DOWN: (1, 0) +} def draw_text(text, color, surface, x, y, text_size=30, is_bold=False): @@ -45,3 +56,21 @@ def castle_neighbors(map, castle_bottom_right_row, castle_bottom_right_col): continue neighbors.append((new_col, new_row)) return neighbors + + +def find_neighbours(col: int, row: int) -> List[Tuple[int, int]]: + dr = [-1, 1, 0, 0] + dc = [0, 0, -1, 1] + + neighbours = [] + + for i in range(4): + rr = row + dr[i] + cc = col + dc[i] + + if rr < 0 or cc < 0: continue + if rr >= ROWS or cc >= COLUMNS: continue + + neighbours.append((rr, cc)) + + return neighbours diff --git a/logic/level.py b/logic/level.py index 00d2044..e6da287 100644 --- a/logic/level.py +++ b/logic/level.py @@ -2,9 +2,9 @@ import random import pygame -from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD, UP, DOWN, LEFT, RIGHT +from algorithms.a_star import a_star, State, TURN_RIGHT, TURN_LEFT, FORWARD from common.constants import * -from common.helpers import castle_neighbors +from common.helpers import find_neighbours from logic.knights_queue import KnightsQueue from logic.spawner import Spawner from models.castle import Castle @@ -113,11 +113,12 @@ class Level: knight_pos_y = current_knight.position[1] castle_cords = (self.list_castles[0].position[0], self.list_castles[0].position[1]) - goal_list = castle_neighbors(self.map, castle_cords[0], castle_cords[1]) # list of castle neighbors + goal_list = find_neighbours(12, 8) state = State((knight_pos_y, knight_pos_x), current_knight.direction.name) action_list = a_star(state, self.map, goal_list) print(action_list) + print(goal_list) if len(action_list) == 0: return