Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ A `Grid` stores its locations by value in a `std::vector<Location>`. `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.

Expand Down
20 changes: 20 additions & 0 deletions src/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1678,5 +1697,6 @@ int main() {
testGridPresenceMatchesSameIdTwin();
testLocationLookupsUseFirstMatchButRemovalTakesEvery();
testAddingOccupiedLocationToGridCopiesItsEntityPointers();
testPrintingConsoleRepresentationFollowsLocationVectorAfterAddingOrRemovingLocations();
return 0;
}
Loading