From e89f564dd66cf6bf4121d5181df5ed0d795e70ba Mon Sep 17 00:00:00 2001 From: korzepadawid Date: Tue, 31 May 2022 15:50:42 +0200 Subject: [PATCH] saving sands, trees, monsters etc positions --- algorithms/genetic/genome.py | 36 ++++++++++++----------------- algorithms/genetic/map_generator.py | 2 +- 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/algorithms/genetic/genome.py b/algorithms/genetic/genome.py index 41c94ea..f060352 100644 --- a/algorithms/genetic/genome.py +++ b/algorithms/genetic/genome.py @@ -1,4 +1,3 @@ -import string from random import randrange from typing import List @@ -13,6 +12,10 @@ class Genome: grid: npt.NDArray knights_red: List[Position] knights_blue: List[Position] + waters: List[Position] + trees: List[Position] + sands: List[Position] + monsters: List[Position] def __init__(self): self.grid = np.zeros((ROWS, COLUMNS), dtype=int) @@ -45,10 +48,14 @@ class Genome: height=2 ) - spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("WATER"), objects_count=WATER_COUNT) - spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("TREE"), objects_count=TREE_COUNT) - spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("SAND"), objects_count=SAND_COUNT) - spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("MONSTER"), objects_count=MONSTERS_COUNT) + self.waters = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("WATER"), + objects_count=WATER_COUNT) + self.trees = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("TREE"), + objects_count=TREE_COUNT) + self.sands = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("SAND"), + objects_count=SAND_COUNT) + self.monsters = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("MONSTER"), + objects_count=MONSTERS_COUNT) def is_empty(grid: npt.NDArray, position: Position) -> bool: @@ -63,11 +70,11 @@ def is_invalid_area(spawn_position_start, height, width) -> bool: def spawn_objects_in_given_area(grid: npt.NDArray, - object_alias: MAP_ALIASES.get("KNIGHT_RED") | MAP_ALIASES.get("KNIGHT_BLUE") | MAP_ALIASES.get("CASTLE") | MAP_ALIASES.get("MONSTER"), + object_alias: str, objects_count: int = 1, spawn_position_start: Position = Position(row=0, col=0), - width: int = 1, - height: int = 1) -> List[Position]: + width: int = COLUMNS, + height: int = ROWS) -> List[Position]: if is_invalid_area(spawn_position_start, height, width): raise ValueError("Invalid spawn area") @@ -85,16 +92,3 @@ def spawn_objects_in_given_area(grid: npt.NDArray, objects_remaining -= 1 return positions - - -# temporary function to spawn castle, monsters, sand, trees -def spawn_where_possible(grid: npt.NDArray, object_alias: string, objects_count: int = 1): - objects_remaining = int(objects_count) - while objects_remaining > 0: - row = randrange(0, ROWS - 1) - col = randrange(0, COLUMNS - 1) - position = Position(row=row, col=col) - - if is_empty(grid, position): - grid[position.row, position.col] = object_alias - objects_remaining -= 1 diff --git a/algorithms/genetic/map_generator.py b/algorithms/genetic/map_generator.py index 8708e29..3148352 100644 --- a/algorithms/genetic/map_generator.py +++ b/algorithms/genetic/map_generator.py @@ -7,7 +7,7 @@ def main() -> None: print(example_genome.knights_red) print(example_genome.knights_blue) print(example_genome.grid) - export_map(example_genome.grid) + # export_map(example_genome.grid) fixme: FileNotFoundError if __name__ == '__main__':