From 8a90137fc408d56af31330829042e3377487ea65 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Thu, 19 Nov 2026 20:27:16 -0700 Subject: [PATCH] Pin the console representation after adding or removing locations printConsoleRepresentation walks getLocations() and breaks rows every getSize() locations, so removeLocation shifts later locations back a slot and addLocation's location is printed last, whatever its coordinates. A characterization test pins both, and the README now says so alongside the existing setSize note. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- src/tests.cpp | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cb6d3ca..eb949d5 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ A `Grid` stores its locations by value in a `std::vector`. `Grid::getL `addEntityToLocation` uses its `location` argument only for its id: the entity is added to the grid's own location with that id. The argument may therefore be either a reference obtained from the grid (for example from `getLocation` or `getLocationByCoordinates`) or a separately constructed `Location` with a matching id; in the latter case the separate object is left unchanged. -`Grid::setSize` and `Grid::setId` only overwrite the stored value. Neither regenerates the grid's locations nor renames their ids, so after either call `getSize()` or `getId()` no longer describes the existing locations. `printConsoleRepresentation()` breaks rows every `getSize()` locations, so it misaligns after `setSize`. +`Grid::setSize` and `Grid::setId` only overwrite the stored value. Neither regenerates the grid's locations nor renames their ids, so after either call `getSize()` or `getId()` no longer describes the existing locations. `printConsoleRepresentation()` prints the locations in `getLocations()` order and breaks rows every `getSize()` locations, without looking at their coordinates. It therefore misaligns after `setSize`, and also after `Grid::removeLocation`, which shifts every later location back one slot, or `Grid::addLocation`, whose location is printed after the existing ones, whatever its coordinates. `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. diff --git a/src/tests.cpp b/src/tests.cpp index 8fc90d9..20bfc0f 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1033,6 +1033,25 @@ void testPrintingConsoleRepresentationMisalignsAfterSettingGridSize() { std::cout << " --- " << "Success" << std::endl; } +void testPrintingConsoleRepresentationFollowsLocationVectorAfterAddingOrRemovingLocations() { + std::cout << "Test 90 - Printing the console representation follows the location vector after adding or removing locations"; + // a removed location shifts every later one back a slot, so the rows no longer match y + Environment shrunk(0, "Earth", 2); + Entity first(0, "Daniel"); + shrunk.addEntityToLocation(first, shrunk.getGrid()->getLocationByCoordinates(0, 1)); + shrunk.getGrid()->removeLocation(shrunk.getGrid()->getLocationByCoordinates(0, 0)); + assert(captureConsoleRepresentation(shrunk) == "[ ] [x] \n[ ] \n"); + + // an added location is printed after the generated ones, whatever its coordinates + Environment grown(0, "Earth", 2); + Location extra("extra-location", 5, 5); + grown.getGrid()->addLocation(extra); + Entity second(1, "Bob"); + grown.addEntityToLocation(second, extra); + assert(captureConsoleRepresentation(grown) == "[ ] [ ] \n[ ] [ ] \n[x] \n"); + std::cout << " --- " << "Success" << std::endl; +} + void testRetrievingFirstEntityFollowsLocationOrder() { std::cout << "Test 62 - Retrieving the first entity follows location order, not insertion order"; Environment environment(0, "Earth", 2); @@ -1678,5 +1697,6 @@ int main() { testGridPresenceMatchesSameIdTwin(); testLocationLookupsUseFirstMatchButRemovalTakesEvery(); testAddingOccupiedLocationToGridCopiesItsEntityPointers(); + testPrintingConsoleRepresentationFollowsLocationVectorAfterAddingOrRemovingLocations(); return 0; } \ No newline at end of file