diff --git a/src/entity.cpp b/src/entity.cpp index a04bb21..e493caf 100644 --- a/src/entity.cpp +++ b/src/entity.cpp @@ -4,12 +4,12 @@ #include namespace envlibcpp { - Entity::Entity(std::string entityName) { - id = rand() % 100 + 1; + Entity::Entity(int identifier, std::string entityName) { + id = identifier; name = entityName; environmentId = -1; gridId = -1; - locationId = -1; + locationId = "N/S"; } int Entity::getId() { @@ -28,7 +28,7 @@ namespace envlibcpp { return gridId; } - int Entity::getLocationId() { + std::string Entity::getLocationId() { return locationId; } @@ -44,7 +44,11 @@ namespace envlibcpp { gridId = id; } - void Entity::setLocationId(int id) { + void Entity::setLocationId(std::string id) { locationId = id; } + + void Entity::resetLocationId() { + setLocationId("N/S"); + } } \ No newline at end of file diff --git a/src/environment.cpp b/src/environment.cpp index 8d0656b..26dedd2 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -3,10 +3,14 @@ #include namespace envlibcpp { - Environment::Environment(std::string envName, int size) { - id = rand() % 100 + 1; + Environment::Environment(int identifier, std::string envName, int size) { + id = identifier; name = envName; - grid = Grid(size); + grid = new Grid(id, size); + } + + Environment::~Environment() { + free(grid); } int Environment::getId() { @@ -17,7 +21,7 @@ namespace envlibcpp { return name; } - Grid& Environment::getGrid() { + Grid* Environment::getGrid() { return grid; } @@ -27,25 +31,25 @@ namespace envlibcpp { void Environment::addEntity(Entity& entity) { entity.setEnvironmentId(id); - grid.addEntity(entity); + grid->addEntity(entity); } void Environment::addEntityToLocation(Entity& entity, Location& location) { entity.setEnvironmentId(id); - grid.addEntityToLocation(entity, location); + grid->addEntityToLocation(entity, location); } void Environment::removeEntity(Entity& entity) { entity.setEnvironmentId(-1); - grid.removeEntity(entity); + grid->removeEntity(entity); } bool Environment::isEntityPresent(Entity& entity) { - return grid.isEntityPresent(entity); + return grid->isEntityPresent(entity); } int Environment::getNumEntities() { - return grid.getNumEntities(); + return grid->getNumEntities(); } void Environment::printInfo() { @@ -53,8 +57,8 @@ namespace envlibcpp { std::cout << "Num entities: " << getNumEntities() << std::endl; } - envlibcpp::Entity& Environment::getFirstEntity() { - for (envlibcpp::Location& location : getGrid().getLocations()) { + Entity& Environment::getFirstEntity() { + for (Location& location : getGrid()->getLocations()) { if (location.getNumEntities() > 0) { return location.getEntities()[0]; } @@ -62,9 +66,9 @@ namespace envlibcpp { throw new std::exception(); } - envlibcpp::Entity& Environment::getEntity(int entityId) { - for (envlibcpp::Location& location : getGrid().getLocations()) { - for (envlibcpp::Entity& entity : location.getEntities()) { + Entity& Environment::getEntity(int entityId) { + for (Location& location : getGrid()->getLocations()) { + for (Entity& entity : location.getEntities()) { if (entity.getId() == entityId) { return entity; } diff --git a/src/grid.cpp b/src/grid.cpp index 6d0692f..d86c171 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -3,12 +3,8 @@ #include namespace envlibcpp { - Grid::Grid() { - Grid(4); - } - - Grid::Grid(int gridSize) { - id = rand() % 100 + 1; + Grid::Grid(int identifier, int gridSize) { + id = identifier; size = gridSize; generateLocations(); } @@ -84,7 +80,7 @@ namespace envlibcpp { for (Location& location : locations) { if (location.isEntityPresent(entity)) { location.removeEntity(entity); - entity.setLocationId(-1); + entity.resetLocationId(); } } entity.setGridId(-1); @@ -99,7 +95,7 @@ namespace envlibcpp { return false; } - Location& Grid::getLocation(int locationId) { + Location& Grid::getLocation(std::string locationId) { for (Location& location : locations) { if (location.getId() == locationId) { return location; @@ -116,7 +112,8 @@ namespace envlibcpp { void Grid::generateLocations() { for (int y = 0; y < getSize(); y++) { for (int x = 0; x < getSize(); x++) { - Location location(x, y); + std::string identifier = "" + std::to_string(getId()) + "-" + std::to_string(x) + "-" + std::to_string(y); + Location location(identifier, x, y); locations.push_back(location); } } diff --git a/src/header/entity.h b/src/header/entity.h index 9fcf280..b3c441e 100644 --- a/src/header/entity.h +++ b/src/header/entity.h @@ -9,23 +9,24 @@ namespace envlibcpp { // @since August 26th, 2022 class Entity { public: - Entity(std::string entityName); + Entity(int id, std::string entityName); int getId(); std::string getName(); int getEnvironmentId(); int getGridId(); - int getLocationId(); + std::string getLocationId(); void setName(std::string newName); void setEnvironmentId(int id); void setGridId(int id); - void setLocationId(int id); - envlibcpp::Entity& getEntity(int entityId); + void setLocationId(std::string id); + void resetLocationId(); + Entity& getEntity(int entityId); private: int id; std::string name; int environmentId; int gridId; - int locationId; + std::string locationId; }; } diff --git a/src/header/environment.h b/src/header/environment.h index c9fcbb3..202e374 100644 --- a/src/header/environment.h +++ b/src/header/environment.h @@ -11,10 +11,11 @@ namespace envlibcpp { // @since August 26th, 2022 class Environment { public: - Environment(std::string envName, int size); + Environment(int id, std::string envName, int size); + ~Environment(); int getId(); std::string getName(); - Grid& getGrid(); + Grid* getGrid(); void setName(std::string newName); void addEntity(Entity& entity); void addEntityToLocation(Entity& entity, Location& location); @@ -22,12 +23,12 @@ namespace envlibcpp { bool isEntityPresent(Entity& entity); int getNumEntities(); void printInfo(); - envlibcpp::Entity& getFirstEntity(); - envlibcpp::Entity& getEntity(int entityId); + Entity& getFirstEntity(); + Entity& getEntity(int entityId); private: int id; std::string name; - Grid grid; + Grid* grid; }; } diff --git a/src/header/grid.h b/src/header/grid.h index 23e44fe..d74ad38 100644 --- a/src/header/grid.h +++ b/src/header/grid.h @@ -12,8 +12,8 @@ namespace envlibcpp { // @since August 26th, 2022 class Grid { public: - Grid(); - Grid(int gridSize); + Grid() = delete; + Grid(int id, int gridSize); int getId(); std::vector& getLocations(); Location& getFirstLocation(); @@ -28,7 +28,7 @@ namespace envlibcpp { void addEntityToLocation(Entity& entity, Location& location); void removeEntity(Entity& entity); bool isEntityPresent(Entity& entity); - Location& getLocation(int locationId); + Location& getLocation(std::string locationId); Location& getRandomLocation(); private: int id; diff --git a/src/header/location.h b/src/header/location.h index 8080aa5..2607450 100644 --- a/src/header/location.h +++ b/src/header/location.h @@ -12,8 +12,8 @@ namespace envlibcpp { // @since August 26th, 2022 class Location { public: - Location(int xpos, int ypos); - int getId(); + Location(std::string id, int xpos, int ypos); + std::string getId(); int getX(); int getY(); int getNumEntities(); @@ -22,7 +22,7 @@ namespace envlibcpp { bool isEntityPresent(Entity& entity); std::vector& getEntities(); private: - int id; + std::string id; int x; int y; std::vector entities; diff --git a/src/location.cpp b/src/location.cpp index a38b65f..7d5ef53 100644 --- a/src/location.cpp +++ b/src/location.cpp @@ -3,13 +3,13 @@ #include namespace envlibcpp { - Location::Location(int xpos, int ypos) { - id = rand() % 100 + 1; + Location::Location(std::string identifier, int xpos, int ypos) { + id = identifier; x = xpos; y = ypos; } - int Location::getId() { + std::string Location::getId() { return id; } @@ -41,7 +41,7 @@ namespace envlibcpp { if (index != -1) { entities.erase(entities.begin() + index); } - entity.setLocationId(-1); + entity.resetLocationId(); } bool Location::isEntityPresent(Entity& entity) { diff --git a/src/tests.cpp b/src/tests.cpp index 5fdb41c..573c707 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -19,8 +19,8 @@ void testTemplate() { void testPlacingEntityInLocation() { std::cout << "---" << std::endl; std::cout << "Test 1 - Placing entity in location" << std::endl; - Entity entity("Daniel"); - Location location(0, 0); + Entity entity(0, "Daniel"); + Location location("my location id", 0, 0); location.addEntity(entity); assert(location.isEntityPresent(entity) == true); assert(entity.getLocationId() == location.getId()); @@ -30,12 +30,12 @@ void testPlacingEntityInLocation() { void testRemovingEntityFromLocation() { std::cout << "---" << std::endl; std::cout << "Test 2 - Removing entity from location" << std::endl; - Entity entity("Daniel"); - Location location(0, 0); + Entity entity(0, "Daniel"); + Location location("location-0-0", 0, 0); location.addEntity(entity); location.removeEntity(entity); assert(!location.isEntityPresent(entity)); - assert(entity.getLocationId() == -1); + assert(entity.getLocationId() == "N/S"); std::cout << "Success" << std::endl; } @@ -43,8 +43,8 @@ void testGeneratingGrid() { std::cout << "---" << std::endl; std::cout << "Test 3 - Generating grid" << std::endl; int size = 4; - Environment environment("test environment", 4); - int numLocations = environment.getGrid().getLocations().size(); + Environment environment(0, "test environment", 4); + int numLocations = environment.getGrid()->getLocations().size(); int expectedNumLocations = size*size; assert(numLocations == expectedNumLocations); std::cout << "Success" << std::endl; @@ -53,7 +53,7 @@ void testGeneratingGrid() { void testRetrievingLocation() { std::cout << "---" << std::endl; std::cout << "Test 4 - Retrieving location" << std::endl; - Grid grid(5); + Grid grid(0, 5); Location firstLocation = grid.getFirstLocation(); assert(firstLocation.getX() == 0 && firstLocation.getY() == 0); std::cout << "Success" << std::endl; @@ -62,9 +62,9 @@ void testRetrievingLocation() { void testRetrievingLocationAfterModification() { std::cout << "---" << std::endl; std::cout << "Test 5 - Retrieving location after modification" << std::endl; - Grid grid(5); + Grid grid(0, 5); Location firstLocation = grid.getFirstLocation(); - Entity entity = Entity("test"); + Entity entity = Entity(0, "test"); firstLocation = grid.getFirstLocation(); firstLocation.addEntity(entity); assert(firstLocation.getX() == 0 && firstLocation.getY() == 0); @@ -75,8 +75,8 @@ void testRetrievingLocationAfterModification() { void testPlacingEntityInGrid() { std::cout << "---" << std::endl; std::cout << "Test 6 - Placing entity in grid" << std::endl; - Entity entity("Daniel"); - Grid grid(4); + Entity entity(0, "Daniel"); + Grid grid(0, 4); grid.addEntity(entity); assert(grid.isEntityPresent(entity)); assert(entity.getGridId() == grid.getId()); @@ -86,8 +86,8 @@ void testPlacingEntityInGrid() { void testRemovingEntityFromGrid() { std::cout << "---" << std::endl; std::cout << "Test 7 - Removing entity from grid" << std::endl; - Entity entity("Daniel"); - Grid grid(4); + Entity entity(0, "Daniel"); + Grid grid(0, 4); grid.addEntity(entity); grid.removeEntity(entity); assert(!grid.isEntityPresent(entity)); @@ -98,8 +98,8 @@ void testRemovingEntityFromGrid() { void testPlacingEntityInEnvironment() { std::cout << "---" << std::endl; std::cout << "Test 8 - Placing entity in environment" << std::endl; - Entity entity("Daniel"); - Environment environment("Earth", 2); + Entity entity(0, "Daniel"); + Environment environment(0, "Earth", 2); environment.addEntity(entity); assert(environment.isEntityPresent(entity)); assert(entity.getEnvironmentId() == environment.getId()); @@ -109,8 +109,8 @@ void testPlacingEntityInEnvironment() { void testRemovingEntityFromEnvironment() { std::cout << "---" << std::endl; std::cout << "Test 9 - Removing entity from environment" << std::endl; - Entity entity("Daniel"); - Environment environment("Earth", 2); + Entity entity(0, "Daniel"); + Environment environment(0, "Earth", 2); environment.addEntity(entity); environment.removeEntity(entity); assert(!environment.isEntityPresent(entity)); @@ -121,10 +121,10 @@ void testRemovingEntityFromEnvironment() { void testPlacingMultipleEntities() { std::cout << "---" << std::endl; std::cout << "Test 10 - Placing multiple entities" << std::endl; - Entity entity1("Bob"); - Entity entity2("Phil"); - Entity entity3("Clarisse"); - Environment environment("Earth", 6); + Entity entity1(0, "Bob"); + Entity entity2(1, "Phil"); + Entity entity3(2, "Clarisse"); + Environment environment(0, "Earth", 6); environment.addEntity(entity1); environment.addEntity(entity2); environment.addEntity(entity3); diff --git a/tests b/tests index 67f87b3..4084c01 100644 Binary files a/tests and b/tests differ