From f4ec5b586986d33a7ff96e6dbe0bc228623159af Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Mon, 11 Apr 2022 19:18:03 +0200 Subject: [PATCH] feat: models --- algorithms/a_star.py | 25 +++++++++++++++++++++++++ algorithms/bfs.py | 2 +- common/constants.py | 1 + logic/spawner.py | 10 +++++----- 4 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 algorithms/a_star.py diff --git a/algorithms/a_star.py b/algorithms/a_star.py new file mode 100644 index 0000000..d3e3077 --- /dev/null +++ b/algorithms/a_star.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +from dataclasses import dataclass, field +from typing import Tuple, Optional + +from common.constants import Direction + + +@dataclass +class State: + position: Tuple[int, int] + direction: Direction + + +@dataclass +class Node: + state: State + parent: Optional[Node] + action: str + cost: int = field(init=False) + depth: int = field(init=False) + + def __post_init__(self) -> None: + self.cost = 0 if not self.parent else self.parent.cost + 1 + self.depth = self.cost diff --git a/algorithms/bfs.py b/algorithms/bfs.py index e900faa..b2ed3d3 100644 --- a/algorithms/bfs.py +++ b/algorithms/bfs.py @@ -114,7 +114,7 @@ def go(row, column, direction): def is_valid_move(map, target_row, target_column): - if 0 <= target_row < ROWS and 0 <= target_column < COLUMNS and map[target_row][target_column] == ' ': + if 0 <= target_row < ROWS and 0 <= target_column < COLUMNS and map[target_row][target_column] in ['g', 's', ' ']: return True return False diff --git a/common/constants.py b/common/constants.py index 37dd0ae..692c32d 100644 --- a/common/constants.py +++ b/common/constants.py @@ -29,6 +29,7 @@ CASTLE_SPAWN_FIRST_COL = 9 NBR_OF_WATER = 16 NBR_OF_TREES = 20 NBR_OF_MONSTERS = 2 +NBR_OF_SANDS = 15 TILES = [ 'grass1.png', diff --git a/logic/spawner.py b/logic/spawner.py index 67431e4..1e91795 100644 --- a/logic/spawner.py +++ b/logic/spawner.py @@ -8,7 +8,7 @@ class Spawner: self.map = map def __is_free_field(self, field): - return field == ' ' + return field in ['g', 's', ' '] def spawn_in_area(self, objects: list, spawn_area_pos_row=0, spawn_area_pos_column=0, spawn_area_width=0, spawn_area_height=0, size=1): @@ -17,17 +17,17 @@ class Spawner: while spawned_objects_count != len(objects): x = random.randint(0, spawn_area_height) + spawn_area_pos_row y = random.randint(0, spawn_area_width) + spawn_area_pos_column - if x < ROWS-1 and y < COLUMNS-1 and self.__is_free_field(self.map[x][y]): + if x < ROWS - 1 and y < COLUMNS - 1 and self.__is_free_field(self.map[x][y]): for i in range(size): for j in range(size): - self.map[x-i][y-j] = objects[spawned_objects_count] + self.map[x - i][y - j] = objects[spawned_objects_count] spawned_objects_count += 1 def spawn_where_possible(self, objects: list): spawned_objects_count = 0 while spawned_objects_count != len(objects): - x = random.randint(0, ROWS-1) - y = random.randint(0, COLUMNS-1) + x = random.randint(0, ROWS - 1) + y = random.randint(0, COLUMNS - 1) if self.__is_free_field(self.map[x][y]): self.map[x][y] = objects[spawned_objects_count] spawned_objects_count += 1