genetic_alg #34
107
algorithms/genetic/map_generator.py
Normal file
107
algorithms/genetic/map_generator.py
Normal file
@ -0,0 +1,107 @@
|
||||
from dataclasses import dataclass
|
||||
from random import randrange
|
||||
|
||||
import numpy as np
|
||||
import numpy.typing as npt
|
||||
|
||||
# map config
|
||||
KNIGHTS_PER_TEAM_COUNT = 4
|
||||
SAND_COUNT = 21
|
||||
WATER_COUNT = 21
|
||||
TREE_COUNT = 37
|
||||
MONSTERS_COUNT = 2
|
||||
CASTLES_COUNT = 1
|
||||
ROWS = 19
|
||||
COLUMNS = 24
|
||||
KNIGHTS_SPAWN_WIDTH = 4
|
||||
KNIGHTS_SPAWN_HEIGHT = 7
|
||||
LEFT_KNIGHTS_SPAWN_FIRST_ROW = 6
|
||||
LEFT_KNIGHTS_SPAWN_FIRST_COL = 0
|
||||
RIGHT_KNIGHTS_SPAWN_FIRST_ROW = 6
|
||||
RIGHT_KNIGHTS_SPAWN_FIRST_COL = 20
|
||||
|
||||
# aliases
|
||||
GRASS = 0
|
||||
SAND = 1
|
||||
WATER = 2
|
||||
TREE = 3
|
||||
MONSTER = 4
|
||||
CASTLE = 5
|
||||
KNIGHT_RED = 6
|
||||
KNIGHT_BLUE = 7
|
||||
|
||||
|
||||
@dataclass
|
||||
class Position:
|
||||
row: int
|
||||
col: int
|
||||
|
||||
|
||||
def is_empty(grid: npt.NDArray, position: Position) -> bool:
|
||||
return grid[position.row, position.col] in [GRASS, SAND]
|
||||
|
||||
|
||||
def is_invalid_area(spawn_position_start, height, width) -> bool:
|
||||
return spawn_position_start.row + height - 1 < 0 or \
|
||||
spawn_position_start.row + height - 1 >= ROWS or \
|
||||
spawn_position_start.col + width - 1 < 0 or \
|
||||
spawn_position_start.col + width - 1 >= COLUMNS
|
||||
|
||||
|
||||
def spawn_objects_in_given_area(grid: npt.NDArray, object_alias: KNIGHT_RED | KNIGHT_BLUE | CASTLE | MONSTER,
|
||||
objects_count: int = 1,
|
||||
spawn_position_start: Position = Position(row=0, col=0),
|
||||
width: int = 1,
|
||||
height: int = 1) -> None:
|
||||
if is_invalid_area(spawn_position_start, height, width):
|
||||
raise ValueError("Invalid spawn area")
|
||||
|
||||
if objects_count > 0:
|
||||
row = randrange(spawn_position_start.row, spawn_position_start.row + height)
|
||||
col = randrange(spawn_position_start.col, spawn_position_start.col + width)
|
||||
position = Position(row=row, col=col)
|
||||
objects_remaining = int(objects_count)
|
||||
|
||||
if is_empty(grid=grid, position=position):
|
||||
grid[position.row, position.col] = object_alias
|
||||
objects_remaining -= 1
|
||||
|
||||
spawn_objects_in_given_area(
|
||||
grid=grid,
|
||||
object_alias=object_alias,
|
||||
objects_count=objects_remaining,
|
||||
spawn_position_start=spawn_position_start,
|
||||
width=width,
|
||||
height=height
|
||||
)
|
||||
|
||||
|
||||
def genome() -> npt.NDArray:
|
||||
grid = np.zeros((ROWS, COLUMNS), dtype=int)
|
||||
spawn_objects_in_given_area(
|
||||
grid=grid,
|
||||
object_alias=KNIGHT_RED,
|
||||
objects_count=KNIGHTS_PER_TEAM_COUNT,
|
||||
spawn_position_start=Position(row=LEFT_KNIGHTS_SPAWN_FIRST_ROW, col=LEFT_KNIGHTS_SPAWN_FIRST_COL),
|
||||
width=KNIGHTS_SPAWN_WIDTH,
|
||||
height=KNIGHTS_SPAWN_HEIGHT
|
||||
)
|
||||
|
||||
spawn_objects_in_given_area(
|
||||
grid=grid,
|
||||
object_alias=KNIGHT_BLUE,
|
||||
objects_count=KNIGHTS_PER_TEAM_COUNT,
|
||||
spawn_position_start=Position(row=RIGHT_KNIGHTS_SPAWN_FIRST_ROW, col=RIGHT_KNIGHTS_SPAWN_FIRST_COL),
|
||||
width=KNIGHTS_SPAWN_WIDTH,
|
||||
height=KNIGHTS_SPAWN_HEIGHT
|
||||
)
|
||||
return grid
|
||||
|
||||
|
||||
def main() -> None:
|
||||
example_genome = genome()
|
||||
print(example_genome)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
BIN
requirements.txt
BIN
requirements.txt
Binary file not shown.
Loading…
Reference in New Issue
Block a user