Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#include <iostream>

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() {
Expand All @@ -28,7 +28,7 @@ namespace envlibcpp {
return gridId;
}

int Entity::getLocationId() {
std::string Entity::getLocationId() {
return locationId;
}

Expand All @@ -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");
}
}
32 changes: 18 additions & 14 deletions src/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
#include <iostream>

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() {
Expand All @@ -17,7 +21,7 @@ namespace envlibcpp {
return name;
}

Grid& Environment::getGrid() {
Grid* Environment::getGrid() {
return grid;
}

Expand All @@ -27,44 +31,44 @@ 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() {
std::cout << "=== " << getName() << " ===" << std::endl;
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];
}
}
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;
}
Expand Down
15 changes: 6 additions & 9 deletions src/grid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
#include <iostream>

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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/header/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
}

Expand Down
11 changes: 6 additions & 5 deletions src/header/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ 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);
void removeEntity(Entity& entity);
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;
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/header/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Location>& getLocations();
Location& getFirstLocation();
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/header/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -22,7 +22,7 @@ namespace envlibcpp {
bool isEntityPresent(Entity& entity);
std::vector<Entity>& getEntities();
private:
int id;
std::string id;
int x;
int y;
std::vector<Entity> entities;
Expand Down
8 changes: 4 additions & 4 deletions src/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#include <iostream>

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;
}

Expand Down Expand Up @@ -41,7 +41,7 @@ namespace envlibcpp {
if (index != -1) {
entities.erase(entities.begin() + index);
}
entity.setLocationId(-1);
entity.resetLocationId();
}

bool Location::isEntityPresent(Entity& entity) {
Expand Down
Loading