Project Classes
Random Number Generation
- class universe.rng.RNG[source]
Bases:
objectA class to handle random number generation.
- choice(seq: list) any[source]
Choose a random element from a sequence.
- Parameters:
seq (list) – The sequence to choose from.
- Returns:
A random element from the sequence.
- Return type:
any
- randint(a: int, b: int) int[source]
Generate a random integer between a and b.
- Parameters:
a (int) – The lower bound.
b (int) – The upper bound.
- Returns:
A random integer.
- Return type:
int
- random() float[source]
Generate a random float between 0 and 1.
- Returns:
A random float.
- Return type:
float
- random_instance = None
- seed = 0
Universe
- class universe.universe.Universe[source]
Bases:
objectClass representing the universe.
The universe is the environment in which the simulation takes place.
- Variables:
rng – The random number generator.
boundary – The boundary of the universe.
ants – A dictionary of ants.
objects – A dictionary of objects.
nests – A list of nests.
- MAX_ANTS = 500
- MAX_OBJECTS = 500
- ants_count = 0
- objects_count = 0
Updates
- class universe.update.Update(_type: UpdateType, ant: Ant | None = None, target: Any | None = None, state: Any | None = None)[source]
Bases:
objectClass representing an update in the simulation.
- target: Any | None = None
- to_dict() Dict[str, Any][source]
Convert the update to a dictionary.
- Returns:
The dictionary representation of the update.
- type: UpdateType = -1
- class universe.update.UpdateType(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumEnum class for update types.
- ANT_ATTACK = 13
- ANT_DEATH = 12
- ANT_MOVE = 11
- ANT_PROMOTE = 14
- ANT_SPAWN = 10
- ERROR_INVALID_ROUNDS = 41
- ERROR_INVALID_TPS = 40
- ERROR_SIMULATION_NOT_RUNNING = 42
- NEST_SPAWN = 20
- OBJECT_DESPAWN = 31
- OBJECT_SPAWN = 30
- SIMULATION_CURRENT_ROUND = 9
- SIMULATION_END = 1
- SIMULATION_PAUSE = 2
- SIMULATION_RESUME = 3
- SIMULATION_SET_BOUNDARIES = 6
- SIMULATION_SET_ROUNDS = 8
- SIMULATION_SET_SEED = 7
- SIMULATION_SET_TPS = 5
- SIMULATION_START = 0
- SIMULATION_TPS = 4
- UNKNOWN = -1
Ant Base Class
- class universe.ants.ant.Ant(position: Position)[source]
Bases:
objectClass representing an ant in the universe.
- Variables:
id – The ID of the ant.
role – The role of the ant.
health – The health of the ant.
food – The food of the ant.
damage – The damage of the ant.
speed – The speed of the ant.
position – The position of the ant.
alive – Whether the ant is alive.
- NEXT_ID = 0
- alive = True
- async attack(other: Ant, update_callback: Callable)[source]
Attack another ant.
- Parameters:
other (Ant) – The ant to attack.
update_callback (Callable) – The callback function to update the state.
- available_directions(boundary: Boundary) List[Direction][source]
Return the available directions for the ant to move.
- damage = 10
- async die(update_callback: Callable)[source]
Kill the ant.
- Parameters:
update_callback (Callable) – The callback function to update the state.
- food = 60
- health = 50
- is_alive() bool[source]
Return whether the ant is alive.
- Returns:
True if the ant is alive, False otherwise.
- Return type:
bool
- abstract async move(universe: Universe, update_callback: Callable)[source]
Move the ant in the universe.
This method should be implemented by the subclasses.
- Parameters:
universe (Universe) – The universe.
update_callback (Callable) – The callback function to update the state.
- async process(universe: Universe, update_callback: Callable)[source]
Process the ant.
- Parameters:
universe (Universe) – The universe.
update_callback (Callable) – The callback function to update the state.
- async set_role(role: Role, update_callback: Callable)[source]
Set the role of the ant.
- Parameters:
role (Role) – The role to set.
update_callback (Callable) – The callback function to update the state.
- async spawn_ants(universe: Universe, max_count: int, update_callback: Callable)[source]
Spawn new ants for the queen.
- Parameters:
universe (Universe) – The universe.
max_count (int) – The maximum number of ants to spawn.
update_callback (Callable) – The callback function to update the state.
- speed = 3
Black Ant
Red Ant
- class universe.ants.red_ant.RedAnt(position: Position)[source]
Bases:
AntRedAnt is a subclass of Ant with specific attributes. It has a health of 40, damage of 15, and speed of 4.
- damage: int = 15
- health: int = 40
- async move(universe: Universe, update_callback: Callable) None[source]
Move the red ant in the universe.
- Parameters:
universe (Universe) – The universe.
update_callback (Callable) – The callback function to update the state.
- speed: int = 4
Area
- class universe.map.area.Area(position_1: Position, position_2: Position)[source]
Bases:
object- direction_from_position(position: Position) Direction | None[source]
Determine the direction from a position to the area.
Boundary
- class universe.map.boundary.Boundary[source]
Bases:
AreaClass representing the boundary of the universe.
- Variables:
width – The width of the boundary.
- contains(position) bool[source]
Check if the given position is within the boundary.
- Parameters:
position (Position) – The position to check.
- Returns:
True if the position is within the boundary, False otherwise.
- Return type:
bool
- height: int = 200
- set_boundary_by_size(size: int) None[source]
Set the boundary by size.
The x and y coordinates are set to negative half of the size, and the width and height are set to the size.
- Parameters:
size (int) – The size to set the boundary.
- set_boundary_by_width_height(width: int, height: int) None[source]
Set the boundary by width and height. The x and y coordinates are set to zero.
- Parameters:
width (int) – The width of the boundary.
height (int) – The height of the boundary.
- size() int[source]
Get the size of the boundary.
- Returns:
The size of the boundary.
- Return type:
int
- width: int = 200
Nest
- class universe.map.nest.Nest(area: Area)[source]
Bases:
objectClass representing a nest in the universe.
- Variables:
area – The area of the nest.
queen – The queen ant of the nest.
- ants_type() Type[Ant][source]
Get the type of the queen ant.
- Returns:
The type of the queen ant.
- Return type:
Type[Ant]
- static generate_random_nest_area(universe: Universe, size_from: int = 10, size_to: int = 20, min_distance: int = 40, min_distance_from: Area | None = None) Area[source]
Generate a random nest area.
- Parameters:
universe (Universe) – The universe to generate the nest area in.
size_from (int) – The minimum size of the nest area.
size_to (int) – The maximum size of the nest area.
min_distance (int) – The minimum distance from the given area.
min_distance_from (Optional[Area]) – The area to keep distance from.
- Returns:
The generated nest area.
- Return type:
Object
- class universe.map.object.Object(position: Position, object_type: ObjectType)[source]
Bases:
objectClass representing an object in the universe.
- Variables:
position – The position of the object.
object_type – The type of the object.
usages_left – The number of usages left for the object.
- async interact(boundary: Boundary, ant: Ant, update_callback: Callable)[source]
Interact with an ant.
- to_dict() dict[source]
Convert the object to a dictionary.
- Returns:
The dictionary representation of the object.
- Return type:
dict
- usages_left: int = 3
Position
- class universe.map.position.Direction(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]
Bases:
EnumEnum class for directions.
- EAST = 90
- NORTH = 0
- SOUTH = 180
- WEST = 270
- class universe.map.position.Position(x: int, y: int, direction: Direction = Direction.NORTH)[source]
Bases:
object- calculate_new_position(boundary: Boundary, direction: Direction, distance: int = 1) Position[source]
Calculate a new position based on a direction and distance.
- can_move(boundary: Boundary, direction: Direction, distance: int = 1) bool[source]
Check if the position can move in a direction.
- chebyshev_distance(other: Position) int[source]
Calculate the Chebyshev distance to another position.
- euclidean_distance(other: Position) float[source]
Calculate the Euclidean distance to another position.
- get_neighbors(distance: int = 1) list[Position][source]
Get the neighbors of the position.
- Parameters:
distance (int) – The distance to get the neighbors from, defaults to 1.
- Returns:
The neighbors of the position.
- Return type:
list[Position]
- manhattan_distance(other: Position) int[source]
Calculate the Manhattan distance to another position.