diff --git a/src/grid.cpp b/src/grid.cpp index ce77d55..4787c8e 100644 --- a/src/grid.cpp +++ b/src/grid.cpp @@ -68,7 +68,7 @@ namespace envlibcpp { void Grid::addEntity(Entity &entity) { entity.setGridId(id); - Location &firstLocation = getFirstLocation(); + Location &firstLocation = getRandomLocation(); firstLocation.addEntity(entity); } @@ -106,6 +106,11 @@ namespace envlibcpp { } throw std::exception(); } + + Location& Grid::getRandomLocation() { + int index = rand() % getLocations().size(); + return getLocations()[index]; + } void Grid::generateLocations() { for (int y = 0; y < getSize(); y++) { diff --git a/src/header/grid.h b/src/header/grid.h index ee10c28..c369862 100644 --- a/src/header/grid.h +++ b/src/header/grid.h @@ -29,6 +29,7 @@ namespace envlibcpp { void removeEntity(Entity &entity); bool isEntityPresent(Entity &entity); Location& getLocation(int locationId); + Location& getRandomLocation(); private: int id; int size;