fp-elevators
Building.cpp
Overview
Building
is a class declared in Building.h
and is responsible for containing the full state of the game. It controls all of the Elevator
s and Floor
s by performing the Move
s and adding the Person
objects that are presented to it. There is a single instance of a Building
class that will be used by the Game
class to track the state of the assets while the game is played.
Member Data
elevators[NUM_ELEVATORS]
is an array that represents all of the elevators in the building.floors[NUM_FLOORS]
is an array that represents each floor in the buildingtime
is an integer that represents the internal clock of the game, whichtick
s forward on each turn. It is used to regulate new spawns and the movement and updating of the elevators and floors.
Member Functions
Many member functions have been written for you. Please refer to their RME’s for reference.
You will be writing the implementations for the following functions:
tick
(Difficulty: ★★☆☆☆)update
(Difficulty: ★★★☆☆)spawnPerson
(Difficulty: ★☆☆☆☆)
tick
/*
* Requires: move is a valid move
* Modifies: The private member variables of building
* Effects: Increments time and calls update() on the input move.
* Then, ticks all of the elevators with the new time.
* Next, ticks all of the building floors, keeping track of new
* exploded people.
* Returns the total number of people that exploded in this tick
*/
int tick(Move move);
tick
is the function that advances the game one turn. During each turn, we give the player the opportunity to make one Move
, which comes in as an argument to this function. Overall, this function is responsible for:
- Updating the
time
- Applying the
Move
to the building and updating the appropriate state. - Calling
tick
with the new time on all the assets in the building. - Finally, return number of people who exploded.
The Building::tick()
function will update the time and call the tick()
function of each of the Floor
s and Elevator
s. The Floor::tick()
function will then call the tick()
function of each of Person
s waiting for an elevator on that floor. The below diagram helps explain how the tick()
functions will be used to carry out updates throughout the game.
update
/*
* Requires: move is a valid move
* Modifies: The building member variables affected by the move
* Effects: Applies the move to the building:
* * If the move is a Pass Move, nothing happens
* * If the move is a Pickup Move, copies the list of people to
* pickup into an array, and calls removePeople() on the
* appropriate floor
* * For both Pickup Moves and Service Moves, the appropriate
* elevator should be sent to service the targetFloor of the move
*/
void update(Move move);
This is one part of a tick
, during which we take the incoming Move
and use it to update the appropriate Floor
or Elevator
. (See the section on Move
for more details) for definitions of Pass Move, Pickup Move, and Service Move.
- There’s nothing to be done for Pass Moves. Game moves (save or quit moves) will never be passed to this function by the game.
- Both Pickup Moves and Service Moves require the appropriate elevator to change floors. This is done by calling
elevator.serviceRequest()
(See theElevator
section for more details) - For Pickup Moves, we have to remove
People
from theFloor
where theMove
is happening. This is done usingfloor.removePeople()
.
Tutorial Video for update
spawnPerson
/*
* Requires: newPerson is a valid Person object
* Modifies: A floor in the building - floors
* Effects: Adds a person to the Floor corresponding to currentFloor
* of the person
*/
void spawnPerson(Person newPerson);
Each Person
corresponds to a person appearing on a Floor
. So, to process a person, we call floor.addPerson()
on the Floor
where this Person
appeared. The Floor::addPerson
function requires two arguments: a Person
, provided as the parameter of this function, and an int
which you must calculate based on the newPerson
’s target and current floors.
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 |
---|---|
spawnPerson() |
Person::getCurrentFloor() , Person::getTargetFloor() , Floor::addPerson() |
update() |
Move::isPassMove() , Move::getElevatorId() , Elevator::getCurrentFloor() , Move::isPickupMove() , Move::copyListOfPeopleToPickup() , Move::getNumPeopleToPickup() , Floor::removePeople() , Elevator::serviceRequest() , Move::getTargetFloor() |
tick() |
Building::update() , Elevator::tick() , Floor:tick() |