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 random import randrange
from typing import List from typing import List
@ -13,6 +12,10 @@ class Genome:
grid: npt.NDArray grid: npt.NDArray
knights_red: List[Position] knights_red: List[Position]
knights_blue: List[Position] knights_blue: List[Position]
waters: List[Position]
trees: List[Position]
sands: List[Position]
monsters: List[Position]
def __init__(self): def __init__(self):
self.grid = np.zeros((ROWS, COLUMNS), dtype=int) self.grid = np.zeros((ROWS, COLUMNS), dtype=int)
@ -45,10 +48,14 @@ class Genome:
height=2 height=2
) )
spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("WATER"), objects_count=WATER_COUNT) self.waters = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("WATER"),
spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("TREE"), objects_count=TREE_COUNT) objects_count=WATER_COUNT)
spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("SAND"), objects_count=SAND_COUNT) self.trees = spawn_objects_in_given_area(grid=self.grid, object_alias=MAP_ALIASES.get("TREE"),
spawn_where_possible(grid=self.grid, object_alias=MAP_ALIASES.get("MONSTER"), objects_count=MONSTERS_COUNT) 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: 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, 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, objects_count: int = 1,
spawn_position_start: Position = Position(row=0, col=0), spawn_position_start: Position = Position(row=0, col=0),
width: int = 1, width: int = COLUMNS,
height: int = 1) -> List[Position]: height: int = ROWS) -> List[Position]:
if is_invalid_area(spawn_position_start, height, width): if is_invalid_area(spawn_position_start, height, width):
raise ValueError("Invalid spawn area") raise ValueError("Invalid spawn area")
@ -85,16 +92,3 @@ def spawn_objects_in_given_area(grid: npt.NDArray,
objects_remaining -= 1 objects_remaining -= 1
return positions 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_red)
print(example_genome.knights_blue) print(example_genome.knights_blue)
print(example_genome.grid) print(example_genome.grid)
export_map(example_genome.grid) # export_map(example_genome.grid) fixme: FileNotFoundError
if __name__ == '__main__': if __name__ == '__main__':