Genome class responsible for keeping grid and lists of positions

This commit is contained in:
korzepadawid 2022-05-30 23:27:31 +02:00
parent c06624d79c
commit 7a9d685701

View File

@ -1,5 +1,6 @@
from dataclasses import dataclass from dataclasses import dataclass
from random import randrange from random import randrange
from typing import List
import numpy as np import numpy as np
import numpy.typing as npt import numpy.typing as npt
@ -37,6 +38,32 @@ class Position:
col: int col: int
class Genome:
grid: npt.NDArray
knights_red: List[Position]
knights_blue: List[Position]
def __init__(self):
self.grid = np.zeros((ROWS, COLUMNS), dtype=int)
self.knights_red = spawn_objects_in_given_area(
grid=self.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
)
self.knights_blue = spawn_objects_in_given_area(
grid=self.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
)
def is_empty(grid: npt.NDArray, position: Position) -> bool: def is_empty(grid: npt.NDArray, position: Position) -> bool:
return grid[position.row, position.col] in [GRASS, SAND] return grid[position.row, position.col] in [GRASS, SAND]
@ -52,55 +79,31 @@ def spawn_objects_in_given_area(grid: npt.NDArray, object_alias: KNIGHT_RED | KN
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 = 1,
height: int = 1) -> None: height: int = 1) -> 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")
if objects_count > 0: objects_remaining = int(objects_count)
positions = []
while objects_remaining > 0:
row = randrange(spawn_position_start.row, spawn_position_start.row + height) row = randrange(spawn_position_start.row, spawn_position_start.row + height)
col = randrange(spawn_position_start.col, spawn_position_start.col + width) col = randrange(spawn_position_start.col, spawn_position_start.col + width)
position = Position(row=row, col=col) position = Position(row=row, col=col)
objects_remaining = int(objects_count)
if is_empty(grid=grid, position=position): if is_empty(grid=grid, position=position):
grid[position.row, position.col] = object_alias grid[position.row, position.col] = object_alias
positions.append(position)
objects_remaining -= 1 objects_remaining -= 1
spawn_objects_in_given_area( return positions
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: def main() -> None:
example_genome = genome() example_genome = Genome()
print(example_genome) print(example_genome.knights_red)
print(example_genome.knights_blue)
print(example_genome.grid)
if __name__ == '__main__': if __name__ == '__main__':