Machine_learning_2023/domain/world.py

80 lines
2.7 KiB
Python
Raw Normal View History

2023-05-18 23:18:07 +02:00
from decisionTree.evaluate import evaluate
from domain.entities.entity import Entity
class World:
def __init__(self, width: int, height: int) -> object:
2023-05-25 16:25:37 +02:00
self.costs = [[1 for j in range(height)] for i in range(width)]
2023-05-18 23:18:07 +02:00
self.width = width
self.height = height
self.dust = [[[] for j in range(height)] for i in range(width)]
2023-06-15 16:46:08 +02:00
self.dustList = []
2023-05-18 23:18:07 +02:00
self.obstacles = [[[] for j in range(height)] for i in range(width)]
2023-06-15 16:46:08 +02:00
self.entity = [[[] for j in range(height)] for i in range(width)]
2023-05-18 23:18:07 +02:00
self.vacuum = None
self.cat = None
self.doc_station = None
def add_entity(self, entity: Entity):
if entity.type == "DOC_STATION":
self.doc_station = entity
elif entity.type == "PEEL":
2023-05-18 23:18:07 +02:00
self.dust[entity.x][entity.y].append(entity)
2023-06-15 16:46:08 +02:00
self.dustList.append(Entity(entity.x, entity.y, "PEEL"))
2023-05-18 23:18:07 +02:00
elif entity.type == "EARRING":
self.dust[entity.x][entity.y].append(entity)
2023-06-15 16:46:08 +02:00
self.dustList.append(Entity(entity.x, entity.y, "EARRING"))
2023-05-18 23:18:07 +02:00
elif entity.type == "VACUUM":
self.vacuum = entity
elif entity.type == "CAT":
self.cat = entity
self.obstacles[entity.x][entity.y].append(entity)
else:
self.obstacles[entity.x][entity.y].append(entity)
2023-06-15 16:46:08 +02:00
self.entity[entity.x][entity.y].append(entity)
def is_entity_at(
self,
x: int,
y: int,
) -> bool:
if len(self.entity[x][y]) > 0:
return True
return False
def delete_entities_at_Of_type(self, x: int, y: int, type: str):
entities = self.entity[x][y]
for entity in entities:
if entity.type == type:
entities.remove(entity)
2023-05-18 23:18:07 +02:00
def is_obstacle_at(self, x: int, y: int) -> bool:
return bool(self.obstacles[x][y])
2023-06-15 16:46:08 +02:00
def garbage_at(self, x: int, y: int) -> list[Entity]:
2023-05-18 23:18:07 +02:00
if len(self.dust[x][y]) == 0:
2023-05-19 15:49:17 +02:00
return []
2023-05-25 16:25:37 +02:00
return [i for i in self.dust[x][y] if evaluate([i.properties])[0] == 1]
2023-05-18 23:18:07 +02:00
def is_docking_station_at(self, x: int, y: int) -> bool:
return bool(self.doc_station.x == x and self.doc_station.y == y)
def accepted_move(self, checking_x, checking_y):
if (
checking_x > self.width - 1
or checking_y > self.height - 1
or checking_x < 0
or checking_y < 0
):
return False
if self.is_obstacle_at(checking_x, checking_y):
return False
return True
2023-05-25 16:25:37 +02:00
2023-06-15 16:46:08 +02:00
def get_cost(self, x, y) -> float:
2023-05-25 16:25:37 +02:00
return self.costs[x][y]