fp-elevators
Person.cpp
Overview
Person is a class declared in Person.h and is responsible for representing a single person inside the building. Each Person will reside on a Floor, slowly getting angrier until they are picked up by an Elevator.
Member Data
turnis anintthat stores the turn on which aPersonwill be spawnedangerLevelis anintrepresenting how angry a Person is. Once this reachesMAX_ANGER, thePersonwill explode. This will deduct points from the overall score inGame.currentFlooris anintrepresenting the floor a Person is currently on.targetFlooris anintrepresenting which floor a Person is trying to reach.
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 function:
Person
/*
* Requires: inputString is correctly formatted
* Modifies: turn, currentFloor, targetFloor, angerLevel
* Effects: Parses input_string to set member variables
* An example of input_string is "7f4t8a3".
*/
Person(string inputString);
Person has a single non-default constructor which parses a special string to initialize a Person’s data members. Refer here in section “The ticks and locations where people will spawn” for details on how the input_string will be formatted. These strings will be how the Person will be stored in game.in, so we will use this constructor to parse each Person’s information.
| int | char | int | char | int | char | int |
|---|---|---|---|---|---|---|
tick |
f |
floorNum |
t |
targetFloorNum |
a |
angerLevel |
string personInfo = "7f4t8a3";
Person newPerson(personInfo);
On tick 7, a person will appear on floor 4 wanting to go to floor 8. They have an anger level of 3.
You may find it useful to use the C++ stringstream class in this function. A stringstream allows you to use >> and << on string input, like in this example:
#include <sstream>
string str = "123abc";
stringstream ss(str);
int x;
ss >> x;
string s;
ss >> s;
// x now contains the integer 123
// s now contains "abc"
tick
/**
* Requires: nothing
* Modifies: angerLevel
* Effects: Every TICKS_PER_ANGER_INCREASE, this function increases the
* person's anger level by 1. If, after increasing, the person
* has an anger level of MAX_ANGER, the function will return
* true. If the person has an anger level less than MAX_ANGER,
* the function will return false.
*/
bool tick(int currentTime);
tick is the function that advances the game one turn. In the context of Person, this involves updating their angerLevel at certain preset intervals. Specifically, we do the following:
- If the
currentTimeis divisible byTICKS_PER_ANGER_INCREASE, then we will increment the person’sangerLevel. - Determine if the person exploded (their
angerLevelis greater than or equal toMAX_ANGER), and returntrueif they did,falseotherwise.
print
/**
* Requires: nothing
* Modifies: outs
* Effects: Prints the infoString of the person NOT including the turn
*
* Example: Person p("7f4t8a3");
* cout << "\nThe person you just entered is: ";
* p.print(cout);
*
* This will print "f4t8a3"
*/
void print(ostream &outs);
This function prints the infoString of the person. It does NOT include the turn. Refer here in section “The ticks and locations where people will spawn” for details on how the infoString will be formatted. Here is an example:
Person p("7f4t8a3");
p.print(cout); // prints f4t8a3
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 |
|---|---|
Person() |
This function does not call any other functions |
tick() |
This function does not call any other functions |
print() |
This function does not call any other functions |