Question
Flooris a class declared inFloor.hand is responsible for representing the people and requests present on each floor of aBuilding.Floors andElevators interact within the context of
Flooris a class declared inFloor.hand is responsible for representing the people and requests present on each floor of aBuilding.Floors andElevators interact within the context of aBuildingduring gameplay.
Member Data
- people[MAX_PEOPLE_PER_FLOOR]is an array ofPersonobjects representing each of the people who are waiting to be serviced on this floor.
- numPeopleis anintrepresenting the number of people currently on this floor. It should always be smaller or equal toMAX_PEOPLE_PER_FLOOR
- hasUpRequestis aboolrepresenting if there are any people currently on this floor that want to go up. This information is displayed by theBuildingand seen by the user to help them decide where to service next.
- hasDownRequestis aboolthat 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); |
tickis the function that advances the game one turn. In the context ofFloor, this means we need totickall the people on the floor. Specifically, we do the following.
- Calltickon eachPersonon this floor. Recall thatPerson::tick()returns whether or not aPersonexploded.
- Keep track of all the indexes where people exploded, and callremovePeople()with those indices.
- Return the number of people who exploded during thistick.
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 mostMAX_PEOPLE_PER_FLOORpeople. IfnumPeopleis strictly less than this limit, we can add thisPersonto the next available slot in ourpeoplearray. For example,
- IfnumPeopleis 0, then we add this person atpeople[0]
- IfnumPeopleis 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 incrementnumPeople.
The second argument,requestcontains the differencetargetFloor - currentFloorfor this particular person. As such, ifrequest > 0, then this person constitutes an up request, and, ifrequest < 0, then this person constitutes a down request. Use this information to updatehasUpRequestorhasDownRequestaccordingly.
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 thepeoplearray, when we remove people from the array (either after they are picked up or have exploded) we need to ensure the rest of thepeopleremain in contiguous slots in thepeoplearray. This function is responsible for removing the people specified inindicesToRemove, updatingnumPeople, as well as making sure that the rest ofPersonobjects in the array remain in the firstnumPeopleslots 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 thePersonobjects in positions 0,2 and 5 are no longer in the array, and the remaining people have been "squished" to the firstnumPeopleslots of the array.
Also note thatindicesToRemovemay not be in a sorted order. If the function were called asremovePeople([5, 0, 2], 3), it should have the same effect. We'll want to ensure that we deal with unsorted input correctly.
Walkthrough videos
This video explains the specification in more detail and describes two possible implementations.
This video details a third possible implementation.
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 ofhasUpRequest/hasDownRequestcould change. This function will iterate through the people on the floor and compare their current and target floors to compute new values forhasUpRequestandhasDownRequest.hasUpRequestwill be true when at least one person wants to go up, andhasDownRequestwill be true when at least one person wants to go down. Both can be true at the same time.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started