From d45169127bdcaaa6253645262c304a1bf0056138 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Sun, 15 Nov 2026 23:50:03 -0700 Subject: [PATCH] Pin first-match location lookups and occupied-location copies Co-Authored-By: Claude Opus 5.5 --- README.md | 4 +++- src/tests.cpp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8eeff35..cb6d3ca 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,9 @@ An `Environment` owns its `Grid`. The grid is allocated in the `Environment` con `Environment`'s copy constructor and copy assignment operator are deleted, since a copy would share the original's `Grid*` and both would delete it on destruction. Copying or assigning an `Environment` is therefore a compile error; pass it by reference or by pointer instead. -A `Grid` stores its locations by value in a `std::vector`. `Grid::getLocation`, `getLocationByCoordinates`, `getFirstLocation`, and `getRandomLocation` return a `Location&` into that vector, so any such reference may be invalidated by a later call to `Grid::addLocation` or `Grid::removeLocation`. `addLocation` also stores a copy of its argument; the grid subsequently operates on the copy, not on the caller's original object. +A `Grid` stores its locations by value in a `std::vector`. `Grid::getLocation`, `getLocationByCoordinates`, `getFirstLocation`, and `getRandomLocation` return a `Location&` into that vector, so any such reference may be invalidated by a later call to `Grid::addLocation` or `Grid::removeLocation`. `addLocation` also stores a copy of its argument; the grid subsequently operates on the copy, not on the caller's original object. If the argument already holds entities, the copy holds the same `Entity*` pointers, so the grid counts those entities and reports them as present, but `addLocation` sets none of their ids: their grid id stays as it was. From then on the two vectors are independent, so removing an entity from the original leaves it in the grid's copy. + +`addLocation` does not check for an existing location with the same id or coordinates. When several match, `Grid::getLocation`, `Grid::getLocationByCoordinates`, and `addEntityToLocation` all use the first match in `getLocations()` order, while `Grid::removeLocation` removes every location with the given id. ## Grid Layout and Movement diff --git a/src/tests.cpp b/src/tests.cpp index ef29dc9..8fc90d9 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1527,6 +1527,59 @@ void testGridPresenceMatchesSameIdTwin() { std::cout << " --- " << "Success" << std::endl; } +void testLocationLookupsUseFirstMatchButRemovalTakesEvery() { + std::cout << "Test 88 - Lookups and placement use the first matching location, removal takes every match"; + Grid grid(0, 1); + Location sameId("0-0-0", 3, 3); + Location sameCoordinates("extra-location", 0, 0); + grid.addLocation(sameId); + grid.addLocation(sameCoordinates); + assert(grid.getLocations().size() == 3); + + // the generated location comes first, so each lookup returns it rather than a later match + assert(grid.getLocation("0-0-0").getX() == 0); + assert(grid.getLocationByCoordinates(0, 0).getId() == "0-0-0"); + + // placement by id goes into the first match only + Entity entity(0, "Daniel"); + Location elsewhere("0-0-0", 9, 9); + assert(grid.addEntityToLocation(entity, elsewhere) == true); + assert(grid.getLocations()[0].getNumEntities() == 1); + assert(grid.getLocations()[1].getNumEntities() == 0); + + // removal takes out every location with the id, leaving only the one with other coordinates + grid.removeLocation(sameId); + assert(grid.getLocations().size() == 1); + assert(grid.getLocations()[0].getId() == "extra-location"); + assert(entity.getLocationId() == "N/S"); + assert(entity.getGridId() == -1); + std::cout << " --- " << "Success" << std::endl; +} + +void testAddingOccupiedLocationToGridCopiesItsEntityPointers() { + std::cout << "Test 89 - Adding an occupied location to a grid copies its entity pointers without setting grid ids"; + Grid grid(7, 1); + Location original("extra-location", 5, 5); + Entity entity(0, "Daniel"); + original.addEntity(&entity); + grid.addLocation(original); + + // the copy holds the same pointer, so the grid sees and counts the entity + assert(grid.getNumEntities() == 1); + assert(grid.isEntityPresent(entity) == true); + assert(grid.getLocation("extra-location").getEntities()[0] == &entity); + // but addLocation sets no ids: the grid id stays unset, the location id is the one Location::addEntity set + assert(entity.getGridId() == -1); + assert(entity.getLocationId() == "extra-location"); + + // the two vectors are independent afterwards: emptying the original leaves the copy's pointer + original.removeEntity(&entity); + assert(original.getNumEntities() == 0); + assert(grid.isEntityPresent(entity) == true); + assert(entity.getLocationId() == "N/S"); + std::cout << " --- " << "Success" << std::endl; +} + void seedRandomNumberGenerator() { srand (time (NULL)); } @@ -1623,5 +1676,7 @@ int main() { testRetrievingRandomLocationReturnsAGridElement(); testPrintingEnvironmentInfoPrintsNameAndPointerCount(); testGridPresenceMatchesSameIdTwin(); + testLocationLookupsUseFirstMatchButRemovalTakesEvery(); + testAddingOccupiedLocationToGridCopiesItsEntityPointers(); return 0; } \ No newline at end of file