fp-elevators
Floor.cpp
Overview
Floor
is a class declared in Floor.h
and is responsible for representing the people and requests present on each floor of a Building
. Floor
s and Elevator
s interact within the context of a Building
during gameplay.
Member Data
people[MAX_PEOPLE_PER_FLOOR]
is an array ofPerson
objects representing each of the people who are waiting to be serviced on this floor.numPeople
is anint
representing the number of people currently on this floor. It should always be less than or equal toMAX_PEOPLE_PER_FLOOR
hasUpRequest
is abool
representing if there are any people currently on this floor that want to go up. This information is displayed by theBuilding
and seen by the user to help them decide where to service next.hasDownRequest
is abool
that is analogous tohasUpRequest
, but reflects a person wanting to go down.
Member Functions
Many member functions have been written for you. Please refer to their RME’s for use reference.
You will be writing the implementation for the following functions:
tick
(Difficulty: ★★★☆☆)addPerson
(Difficulty: ★★☆☆☆)removePeople
(Difficulty: ★★★★★)resetRequests
(Difficulty: ★★★☆☆)
tick
/*
* Requires: nothing
* Modifies: people
* Effects: Ticks each person on this floor
* Also removes any Person who explodes
* Returns the number of exploded people
*/
int tick(int currentTime);
tick
is the function that advances the game one turn. In the context of Floor
, this means we need to tick
all the people on the floor. Specifically, we do the following.
- Call
tick
on eachPerson
on this floor. Recall thatPerson::tick()
returns whether or not aPerson
exploded. - Keep track of all the indexes where people exploded, and call
removePeople()
with those indices. - Return the number of people who exploded during this
tick
.
addPerson
/*
* Requires: request != 0
* Modifies: hasUpRequest, hasDownRequest, numPeople, people
* Effects: If there is still room, add newPerson to people.
* Updates hasUpRequest or hasDownRequest based on value of request
*/
void addPerson(Person newPerson, int request);
Each floor can have at most MAX_PEOPLE_PER_FLOOR
people. If numPeople
is strictly less than this limit, we can add this Person
to the next available slot in our people
array. For example,
- If
numPeople
is 0, then we add this person atpeople[0]
- If
numPeople
is 5, then those people are stored inpeople[0]
throughpeople[4]
, so we add this person atpeople[5]
.
After adding this person, we make sure to increment numPeople
.
The second argument, request
contains the difference targetFloor - currentFloor
for this particular person. As such, if request > 0
, then this person constitutes an up request, and, if request < 0
, then this person constitutes a down request. Use this information to update hasUpRequest
or hasDownRequest
accordingly.
removePeople
/*
* Requires: numPeopleToRemove >= 0, numPeopleToRemove <= MAX_PEOPLE_PER_FLOOR,
* numPeopleToRemove >= 0, numPeopleToRemove <= numPeople,
* for all values of i such that 0 <= i < numPeopleToRemove, indicesToRemove[i] < numPeople
* Modifies: people[], numPeople, hasUpRequest, hasDownRequest
* Effects: Removes objects from people[] at indices specified in indicesToRemove[].
* The size of indicesToRemove[] is given by numPeopleToRemove.
* After removals, calls resetRequests() to update hasUpRequest and hasDownRequest.
*/
void removePeople(const int indicesToRemove[MAX_PEOPLE_PER_FLOOR], const int numPeopleToRemove);
In order to preserve the integrity of the people
array, when we remove people from the array (either after they are picked up or have exploded) we need to ensure the rest of the people
remain in contiguous slots in the people
array. This function is responsible for removing the people specified in indicesToRemove
, updating numPeople
, as well as making sure that the rest of Person
objects in the array remain in the first numPeople
slots in the array. Remember, if you remove people, up and down requests on the floor may change! Make sure to account for this in your code. See below for an example of a before and after.
Note that the Person
objects in positions 0,2 and 5 are no longer in the array, and the remaining people have been “squished” to the first numPeople
slots of the array.
Also note that indicesToRemove
may not be in a sorted order. If the function were called as removePeople([5, 0, 2], 3)
, it should have the same effect. We’ll want to ensure that we deal with unsorted input correctly.
Walkthrough videos
There are several approaches to this function. Feel free to pick from any of the three different approaches outlined in the videos below, or your own approach.
Note: the parameter
indicesToRemove
isconst
and cannot be modified. Some of the strategies below involve sorting the parameter. A copy of the parameter arrayindicesToRemove
needs to be made to enable sorting. At about 6:45 into the first video below is a detailed explanation for creating and sorting a copy of the array parameter.
resetRequests
/*
* Requires: nothing
* Modifies: hasUpRequest, hasDownRequest
* Effects: Search through people to find if there are any
* pending up requests or down requests. If found, set the
* values of hasUpRequest and hasDownRequest appropriately.
* This function is used to recalculate requests whenever
* people on this floor are added or removed.
*/
void resetRequests();
When a new person is spawned on the floor or removed, it’s possible that the value of hasUpRequest
/hasDownRequest
could change. This function will iterate through the people on the floor and compare their current and target floors to compute new values for hasUpRequest
and hasDownRequest
. hasUpRequest
will be true when at least one person wants to go up, and hasDownRequest
will be true when at least one person wants to go down. Both can be true at the same time.
Function Table
The table below provides an outline of which other functions each function should call, if any. The functions listed here do not include any library functions you may wish to use. Please look at the starter code section of the spec to see which libraries you are allowed to use.
Function | Other functions it should call |
---|---|
tick() |
Person::tick() , Floor::removePeople() |
addPerson() |
This function does not call any other functions |
removePeople() |
Floor::resetRequests() |
resetRequests() |
Person::getTargetFloor() , Person::getCurrentFloor() |