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. Floors and Elevators interact within the context of a Building during gameplay.

Member Data

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

/*
 * 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.

  1. Call tick on each Person on this floor. Recall that Person::tick() returns whether or not a Person exploded.
  2. Keep track of all the indexes where people exploded, and call removePeople() with those indices.
  3. 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,

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 <= MAX_PEOPLE_PER_FLOOR, numPeopleToRemove <= numPeople,
 *           for all values of i such that 0 <= i < numPeopleToRemove, indicesToRemove[i] < numPeople
 * Modifies: people[], numPeople
 * Effects:  Removes objects from people[] at indices specified in
 *           indicesToRemove[].  The size of indicesToRemove is given by numPeopleToRemove
 */
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 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.

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 should 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.