diff --git a/README.md b/README.md index 9c6baf1..206b2e1 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ A `Grid` stores its locations by value in a `std::vector`. `Grid::getL `y` increases downward: `moveEntityUp` moves an entity to `y - 1`, `moveEntityDown` to `y + 1`, `moveEntityLeft` to `x - 1`, and `moveEntityRight` to `x + 1`. The grid does not wrap. A move that would leave the grid returns `false` and leaves the entity where it was; a successful move returns `true`. `moveEntityToRandomAdjacentLocation` picks one of the same four directions with `rand()`, so it also returns `false` whenever the chosen direction points off the grid. The library never calls `srand()`; seeding is left to the caller. +A directional move starts from the location named by the entity's location id (looked up with `Grid::getLocation`), not from the location that actually stores the entity. The two only differ if the location id has been changed directly, for example with `Entity::setLocationId`; the move then steps from the named location, takes the entity out of every location that holds it, and places it at the destination. + ## Entity Identity Entities are matched by `Entity::getId()`, not by address. `Location::isEntityPresent`, `Location::removeEntity`, and `Environment::getEntity` all compare ids, so two distinct `Entity` objects that share an id are treated as the same entity. Removing through such a twin takes the stored entity out of its location but resets only the twin's ids, so the stored entity keeps ids describing a placement it no longer has. Entity ids should therefore be unique within an environment. @@ -79,7 +81,7 @@ Lookups that find nothing throw `std::runtime_error`: - `Environment::getEntity`, when no entity has the given id. The message lists the ids that were found. - `Grid::getLocation` and `Grid::getLocationByCoordinates`, when no location matches. The message names the id or coordinates requested and the grid's id. -`Environment::moveEntityToNewLocation` looks up both the entity and the destination before moving anything, so an unknown entity id or location id throws and leaves the entity in place. The directional move methods catch the off-grid case and return `false`, but still throw if `entityId` does not name an entity in the environment. +`Environment::moveEntityToNewLocation` looks up both the entity and the destination before moving anything, so an unknown entity id or location id throws and leaves the entity in place. The directional move methods catch the off-grid case and return `false`, but still throw if `entityId` does not name an entity in the environment, or if that entity's location id does not name a location in the grid. In either case the entity is left in place. `addEntityToLocation` does not throw when no grid location has the given location's id. The entity is then placed nowhere, and its grid id, environment id, and location id are left unchanged. `Grid::addEntityToLocation` returns whether it placed the entity; `Environment::addEntityToLocation` returns nothing. diff --git a/src/tests.cpp b/src/tests.cpp index 34fe722..222775b 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1323,6 +1323,49 @@ void testAddingEntityToLocationInGridReportsWhetherItWasPlaced() { std::cout << " --- " << "Success" << std::endl; } +void testMovingEntityInDirectionStartsFromItsLocationId() { + std::cout << "Test 80 - Moving an entity in a direction starts from the location its location id names"; + Environment environment(0, "Earth", 3); + Entity entity(0, "Daniel"); + environment.addEntityToLocation(entity, environment.getGrid()->getLocationByCoordinates(1, 1)); + // the entity stays stored in 0-1-1, but its location id now names 0-0-0 + entity.setLocationId("0-0-0"); + assert(environment.moveEntityRight(0) == true); + assert(entity.getLocationId() == "0-1-0"); + assert(environment.getGrid()->getLocation("0-1-0").isEntityPresent(&entity) == true); + assert(environment.getGrid()->getLocation("0-1-1").isEntityPresent(&entity) == false); + assert(environment.getGrid()->getLocation("0-2-1").isEntityPresent(&entity) == false); + assert(environment.getNumEntities() == 1); + std::cout << " --- " << "Success" << std::endl; +} + +void testMovingEntityWhoseLocationIdNamesNoGridLocationThrows() { + std::cout << "Test 81 - Moving an entity whose location id names no grid location throws and leaves it in place"; + Environment environment(0, "Earth", 3); + Entity entity(0, "Daniel"); + environment.addEntityToLocation(entity, environment.getGrid()->getLocationByCoordinates(1, 1)); + entity.setLocationId("no-such-location"); + bool threwDirectional = false; + try { + environment.moveEntityUp(0); + } catch (const std::runtime_error& e) { + threwDirectional = true; + } + bool threwRandom = false; + try { + environment.moveEntityToRandomAdjacentLocation(0); + } catch (const std::runtime_error& e) { + threwRandom = true; + } + assert(threwDirectional == true); + assert(threwRandom == true); + assert(entity.getLocationId() == "no-such-location"); + assert(entity.getEnvironmentId() == 0); + assert(environment.getGrid()->getLocation("0-1-1").isEntityPresent(&entity) == true); + assert(environment.getNumEntities() == 1); + std::cout << " --- " << "Success" << std::endl; +} + void seedRandomNumberGenerator() { srand (time (NULL)); } @@ -1411,5 +1454,7 @@ int main() { testAddingEntityToUnknownLocationLeavesEnvironmentIdOfSameIdGridEntity(); testEnvironmentCannotBeCopiedOrAssigned(); testAddingEntityToLocationInGridReportsWhetherItWasPlaced(); + testMovingEntityInDirectionStartsFromItsLocationId(); + testMovingEntityWhoseLocationIdNamesNoGridLocationThrows(); return 0; } \ No newline at end of file