fp-elevators
Move.cpp
Overview
Move
is a class declared in Move.h
and is responsible for representing what a Player (either a Human or AI) wants to do on a particular turn of the Game
. The Game
will take this move, validate it, and apply it to the Building
, which will change its state accordingly.
When a player types in a specially formatted string into the game display, the code will parse it and convert it into a Move
object.
There are four types of Move
s that the player can input:
- Pass Move: Do nothing this turn.
- Service Move: Send an elevator to a particular floor
- Pickup Move: Tell an elevator to pick up people from the floor that it is on.
- Game Move: Save or quit the game.
For details as to how the user can format these inputs, see this section.
Member Data
elevatorId
is anint
representing which elevator thisMove
pertains to.targetFloor
is anint
representing which floor thisMove
will take the elevator to. This is only valid for service and pickup moves.peopleToPickup[MAX_PEOPLE_PER_FLOOR]
is an array of indices for which people on a floor to pick up. This is only valid for pickup moves.numPeopleToPickup
is anint
that represents the current size of the array above. This is only valid for pickup moves.totalSatisfaction
is anint
that represents the total increase in satisfaction caused by this move.isPass
is abool
that represents whether or not the move is a pass move.isPickup
is abool
that represents whether or not the move is a pickup move.isSave
is abool
that represents whether or not the move is a save move.isQuit
is abool
that represents whether or not the move is a quit move.
Note that a Service Move is represented by isPass
, isPickup
, isSave
, and isQuit
all being false
.
Member Functions
Many member functions have been written for you. Please refer to their RME’s for reference on how to use them.
You will be writing the implementation for the following function:
Move
(Difficulty: ★★★☆☆)isValidMove
(Difficulty: ★★☆☆☆)setPeopleToPickup
(Difficulty: ★★★☆☆)
Constructor
/**
* Requires: commandString is a valid string
* Modifies: elevatorId, targetFloor, isPass, isPickup, isSave, isQuit
* Effects: creates a Move object based on
* the characters in commandString
*
* Examples:
* commandString = "e1f4"; - elevatorID of 1 has a targetFloor of 4
* commandString = "e1p"; - elevatorID of 1 will pickup people on
* its current floor
*
* when commandString is "", or "S", or "Q", etc.:
* sets the corresponding private data member to true
*/
Move(string commandString);
Move
has a single non-default constructor which parses a special string to initialize a Move
’s data members. Refer here for details on how the input string will be formatted. This input commandString
will be what the user types in when asked to enter a Move, and this constructor will be used to turn their input into a Move
object that our code can use.
If you’re using a stringstream for this, keep in mind that we shouldn’t be reading any characters from our stringstream if commandString
is empty (i.e. a Pass move).
Walkthrough video
You can find this and other walkthroughs in the Google drive folder for the project here.
isValidMove
/**
* Requires: elevators represents the elevator servicing states
* Modifies: nothing
* Effects: returns true if this Move instance is valid, false otherwise
*
* The following are the criterion for validity:
*
* If a move is a Pass Move, a Quit Move, or a Save Move it is valid
* For both Pickup Moves and Servicing Moves:
* 0 <= elevatorId < NUM_ELEVATORS
* The corresponding Elevator is currently not
* servicing a request.
* For Servicing Moves only:
* 0 <= targetFloor < NUM_FLOORS
* targetFloor is different from the corresponding
* Elevator's currentFloor
*/
bool isValidMove(Elevator elevators[NUM_ELEVATORS]) const;
This function is responsible for ensuring the move is valid. Below are the criterion to determine validity:
- If a move is a Pass Move, a Quit Move, or a Save Move it is valid.
- For both Pickup Moves and Servicing Moves:
0 <= elevatorId < NUM_ELEVATORS
- the corresponding
Elevator
in theelevators
array parameter is not servicing.
- For Servicing Moves only:
0 <= targetFloor < NUM_FLOORS
targetFloor
is different from thecurrentFloor
of the correspondingElevator
in theelevators
array parameter.
The function argument elevators
represents the current state of all the elevators in the building, so be sure to use that to verify some of these conditions.
setPeopleToPickup
/**
* Requires: pickupList is a valid list of indices
* currentFloor is the floor number where the pickup
* is taking place
* pickupFloor is the Floor instance where the pickup
* is taking place
* Modifies: numPeopleToPickup, peopleToPickup, totalSatisfaction, targetFloor
* Effects: sets numPeopleToPickup and totalSatisfaction to 0
* adds the indices specified in pickupList to peopleToPickup
* increments numPeopleToPickup by 1 for each person being picked up
* adds satisfaction gained from each person picked up to totalSatisfaction
* sets targetFloor to be the most extreme floor of those being picked up (furthest up or down)
*/
void setPeopleToPickup(const string& pickupList, const int currentFloor, const Floor& pickupFloor);
This function takes in a validated string of indices, converts them into integers, and stores them inside peopleToPickup
. For example, if pickupList
contains "0347"
, then peopleToPickup
would need to contain the indices 0
, 3
, 4
, and 7
.
While it does this, it also tracks a few other things:
- As it adds indices, it increments
numPeopleToPickup
to maintain accurate sizing information forpeopleToPickup
. This starts at 0 and increments for each index in the string. - As it adds indices, it uses them to lookup the corresponding
Person
’sangerLevel
. This information can be found from the argumentpickupFloor
, which contains the array of all people on this floor. Once theangerLevel
is found,totalSatisfaction
(which also is initialized to 0) is incremented byMAX_ANGER - angerLevel
. - Finally, the
targetFloor
is also calculated using the following rule. ThetargetFloor
is equal to thetargetFloor
of thePerson
who has to travel the furthest from thecurrentFloor
to reach their destination.- For example, if
currentFloor
were 5 and we were picking up 3 people, withtargetFloor
s 6, 7, and 8, ourMove
’stargetFloor
would be set to 8. - Similarly, if
currentFloor
were 5 and we were picking up 3 people, withtargetFloor
s 1, 2, and 3, ourMove
’stargetFloor
would be set to 1.
- For example, if
Walkthrough video
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 |
---|---|
Move() |
This function does not call any other functions |
isValidMove() |
Elevator::isServicing() , Elevator::getCurrentFloor() |
setPeopleToPickup() |
Floor::getPersonByIndex() , Person::getAngerLevel() , Person::getTargetFloor() |