# 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

• people[MAX_PEOPLE_PER_FLOOR] is an array of Person objects representing each of the people who are waiting to be serviced on this floor.
• numPeople is an int representing the number of people currently on this floor. It should always be smaller or equal to MAX_PEOPLE_PER_FLOOR
• hasUpRequest is a bool representing if there are any people currently on this floor that want to go up. This information is displayed by the Building and seen by the user to help them decide where to service next.
• hasDownRequest is a bool that is analogous to hasUpRequest, 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

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


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 at people[0]
• If numPeople is 5, then those people are stored in people[0] through people[4], so we add this person at people[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(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.

If you are seeking additional help with the implementation of this function, check out this link to view the optional walkthrough videos.

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