Floor is a class declared in
Floor.h and is responsible for representing the people and requests present on each floor of a
Elevators interact within the context of a
Building during gameplay.
people[MAX_PEOPLE_PER_FLOOR]is an array of
Personobjects representing each of the people who are waiting to be serviced on this floor.
intrepresenting the number of people currently on this floor. It should always be less than or equal to
boolrepresenting if there are any people currently on this floor that want to go up. This information is displayed by the
Buildingand seen by the user to help them decide where to service next.
boolthat is analogous to
hasUpRequest, but reflects a person wanting to go down.
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:
/* * 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.
Personon this floor. Recall that
Person::tick()returns whether or not a
- Keep track of all the indexes where people exploded, and call
removePeople()with those indices.
- Return the number of people who exploded during this
/* * 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,
numPeopleis 0, then we add this person at
numPeopleis 5, then those people are stored in
people, so we add this person at
After adding this person, we make sure to increment
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
/* * 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(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
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.
This video explains the specification in more detail and describes two possible implementations.
This video details a third possible implementation.
/* * 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
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 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.
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|
||This function does not call any other functions|