From bf2c6f2ea8942aa882b649dacc3bac93d7d53734 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Sat, 14 Nov 2026 22:02:12 -0700 Subject: [PATCH] Pin what printInfo prints and that grid presence matches by id Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- src/tests.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d827029..8eeff35 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ A `Grid` stores its locations by value in a `std::vector`. `Grid::getL ## Grid Layout and Movement -`Environment(id, name, size)` creates a square grid of `size` × `size` locations. The grid is given the environment's own `id`, and each location's id has the form `--` (for example `0-2-1`). Locations are generated row by row, with `x` and `y` each running from `0` to `size - 1`; `Grid::getLocations()` and `Environment::printConsoleRepresentation()` follow that order, and `Environment::getFirstEntity` returns the first entity of the first occupied location in that order, regardless of the order in which entities were added. `printConsoleRepresentation()` prints each location as `[x] ` when it is occupied or `[ ] ` when it is empty. +`Environment(id, name, size)` creates a square grid of `size` × `size` locations. The grid is given the environment's own `id`, and each location's id has the form `--` (for example `0-2-1`). Locations are generated row by row, with `x` and `y` each running from `0` to `size - 1`; `Grid::getLocations()` and `Environment::printConsoleRepresentation()` follow that order, and `Environment::getFirstEntity` returns the first entity of the first occupied location in that order, regardless of the order in which entities were added. `printConsoleRepresentation()` prints each location as `[x] ` when it is occupied or `[ ] ` when it is empty. `Environment::printInfo()` prints two lines, `=== ===` and `Num entities: `, where `` is `getNumEntities()`. `Environment::addEntity` (and `Grid::addEntity`) places the entity in a location chosen by `Grid::getRandomLocation()`, not in the first location. To place an entity at a specific location, use `addEntityToLocation`. @@ -61,7 +61,7 @@ A directional move starts from the location named by the entity's location id (l ## 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. +Entities are matched by `Entity::getId()`, not by address. `Location::isEntityPresent`, `Location::removeEntity`, and `Environment::getEntity` all compare ids, as do `Grid::isEntityPresent` and `Environment::isEntityPresent`, which ask each location in turn, 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. 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. diff --git a/src/tests.cpp b/src/tests.cpp index 7eebf97..ef29dc9 100644 --- a/src/tests.cpp +++ b/src/tests.cpp @@ -1489,6 +1489,44 @@ void testRetrievingRandomLocationReturnsAGridElement() { std::cout << " --- " << "Success" << std::endl; } +std::string captureInfo(Environment& environment) { + std::ostringstream buffer; + std::streambuf* previousBuffer = std::cout.rdbuf(buffer.rdbuf()); + environment.printInfo(); + std::cout.rdbuf(previousBuffer); + return buffer.str(); +} + +void testPrintingEnvironmentInfoPrintsNameAndPointerCount() { + std::cout << "Test 86 - Printing environment info prints the current name and the number of stored entity pointers"; + Environment environment(0, "Earth", 2); + assert(captureInfo(environment) == "=== Earth ===\nNum entities: 0\n"); + + // an entity added twice is stored twice, and printInfo reports getNumEntities() as is + Entity entity(0, "Daniel"); + Location& location = environment.getGrid()->getLocationByCoordinates(0, 0); + environment.addEntityToLocation(entity, location); + environment.addEntityToLocation(entity, location); + environment.setName("Mars"); + assert(captureInfo(environment) == "=== Mars ===\nNum entities: 2\n"); + std::cout << " --- " << "Success" << std::endl; +} + +void testGridPresenceMatchesSameIdTwin() { + std::cout << "Test 87 - A grid reports a same-id twin of a stored entity as present"; + Grid grid(0, 2); + Entity original(5, "Daniel"); + Entity twin(5, "Bob"); + Entity other(6, "Phil"); + grid.addEntityToLocation(original, grid.getLocationByCoordinates(1, 1)); + assert(grid.isEntityPresent(twin) == true); + assert(grid.isEntityPresent(other) == false); + // the twin was never placed, so its own ids are untouched by the lookup + assert(twin.getGridId() == -1); + assert(twin.getLocationId() == "N/S"); + std::cout << " --- " << "Success" << std::endl; +} + void seedRandomNumberGenerator() { srand (time (NULL)); } @@ -1583,5 +1621,7 @@ int main() { testMovingEntityToItsCurrentLocationLeavesItThere(); testPlacingThroughGridOrLocationSetsOnlyThatLayersIds(); testRetrievingRandomLocationReturnsAGridElement(); + testPrintingEnvironmentInfoPrintsNameAndPointerCount(); + testGridPresenceMatchesSameIdTwin(); return 0; } \ No newline at end of file