From e8b37a78ac3f8eb349dc15e6b65641572bf1b9bc Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Thu, 12 Nov 2026 23:32:39 -0700 Subject: [PATCH 1/2] Pin which ids the grid and location layers set on their own Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/tests.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 761b80a..d827029 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ A directional move starts from the location named by the entity's location id (l 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. -A newly constructed entity has an environment id and grid id of `-1` and a location id of `"N/S"`. Placing it through an `Environment` sets all three; `Environment::removeEntity` resets them to those same values. +A newly constructed entity has an environment id and grid id of `-1` and a location id of `"N/S"`. Placing it through an `Environment` sets all three; `Environment::removeEntity` resets them to those same values. The lower layers set only the ids they know: `Grid::addEntity` and `Grid::addEntityToLocation` set the grid id and location id but not the environment id, and `Grid::removeEntity` resets the grid id and location id but leaves the environment id unchanged. `Location::addEntity` and `Location::removeEntity` set and reset only the location id. `Environment::removeEntity` and `Grid::removeEntity` do nothing when the entity is not present. Calling either on an entity that is not in that environment or grid — including one placed in a different environment — leaves its environment id, grid id, location id, and actual placement unchanged. `Location::removeEntity` likewise leaves the location id of an entity that is not in that location unchanged. diff --git a/src/tests.cpp b/src/tests.cpp index 6dd2fba..317f26d 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1436,6 +1436,53 @@ void testMovingEntityToItsCurrentLocationLeavesItThere() { std::cout << " --- " << "Success" << std::endl; } +void testPlacingThroughGridOrLocationSetsOnlyThatLayersIds() { + std::cout << "Test 84 - Placing and removing through a grid or a location sets only the ids that layer knows"; + Grid grid(3, 2); + Entity placedInGrid(0, "Daniel"); + grid.addEntity(placedInGrid); + assert(placedInGrid.getGridId() == 3); + assert(placedInGrid.getEnvironmentId() == -1); + assert(grid.getLocation(placedInGrid.getLocationId()).isEntityPresent(&placedInGrid) == true); + assert(grid.getNumEntities() == 1); + + // the grid has no knowledge of an environment, so removal leaves the environment id alone + placedInGrid.setEnvironmentId(9); + grid.removeEntity(placedInGrid); + assert(placedInGrid.getGridId() == -1); + assert(placedInGrid.getLocationId() == "N/S"); + assert(placedInGrid.getEnvironmentId() == 9); + + Location location("standalone", 0, 0); + Entity placedInLocation(1, "Bob"); + placedInLocation.setGridId(4); + placedInLocation.setEnvironmentId(5); + location.addEntity(&placedInLocation); + assert(placedInLocation.getLocationId() == "standalone"); + assert(placedInLocation.getGridId() == 4); + assert(placedInLocation.getEnvironmentId() == 5); + location.removeEntity(&placedInLocation); + assert(placedInLocation.getLocationId() == "N/S"); + assert(placedInLocation.getGridId() == 4); + assert(placedInLocation.getEnvironmentId() == 5); + std::cout << " --- " << "Success" << std::endl; +} + +void testRetrievingRandomLocationReturnsAGridElement() { + std::cout << "Test 85 - Retrieving a random location returns a reference into the grid's own locations"; + Grid grid(0, 3); + std::vector& locations = grid.getLocations(); + for (size_t i = 0; i < 100; i++) { + Location* random = &grid.getRandomLocation(); + assert(random >= &locations.front() && random <= &locations.back()); + } + Entity entity(0, "Daniel"); + grid.getRandomLocation().addEntity(&entity); + assert(grid.isEntityPresent(entity) == true); + assert(grid.getNumEntities() == 1); + std::cout << " --- " << "Success" << std::endl; +} + void seedRandomNumberGenerator() { srand (time (NULL)); } @@ -1528,5 +1575,7 @@ int main() { testMovingEntityWhoseLocationIdNamesNoGridLocationThrows(); testEnvironmentOfSizeZeroHasNoLocationsAndLookupsThrow(); testMovingEntityToItsCurrentLocationLeavesItThere(); + testPlacingThroughGridOrLocationSetsOnlyThatLayersIds(); + testRetrievingRandomLocationReturnsAGridElement(); return 0; } \ No newline at end of file From 9b92ad145adb8a66582dfdbcf328d66a90cc9c8c Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Thu, 12 Nov 2026 23:35:35 -0700 Subject: [PATCH 2/2] Pin that Grid::addEntityToLocation leaves the environment id unset Co-Authored-By: Claude Opus 5.5 --- src/tests.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/tests.cpp b/src/tests.cpp index 317f26d..7eebf97 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1453,6 +1453,12 @@ void testPlacingThroughGridOrLocationSetsOnlyThatLayersIds() { assert(placedInGrid.getLocationId() == "N/S"); assert(placedInGrid.getEnvironmentId() == 9); + Entity placedAtLocation(2, "Phil"); + assert(grid.addEntityToLocation(placedAtLocation, grid.getLocationByCoordinates(1, 1)) == true); + assert(placedAtLocation.getGridId() == 3); + assert(placedAtLocation.getLocationId() == "3-1-1"); + assert(placedAtLocation.getEnvironmentId() == -1); + Location location("standalone", 0, 0); Entity placedInLocation(1, "Bob"); placedInLocation.setGridId(4);