genetic_alg #34

Merged
s464965 merged 18 commits from genetic_alg into master 2022-06-06 15:19:58 +02:00
2 changed files with 16 additions and 22 deletions
Showing only changes of commit e89f564dd6 - Show all commits

View File

@ -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

View File

@ -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__':