52 fout.open(filesave.c_str());
58 std::string fileToBeLoaded;
62 std::cout <<
"Loading " << fileToBeLoaded <<
" ... ";
63 fin.open(fileToBeLoaded.c_str());
68 std::cout <<
"done" << std::endl;
74 fout << stock.
name << std::endl;
75 for (
unsigned int index = 0; index < stock.
history.size(); index++) {
76 fout << stock.
history[index] <<
" ";
78 fout << -1 << std::endl;
85 fout << stock.
split_count << std::endl << std::endl;
89 fout <<
event << std::endl;
98 std::getline(fin >> std::ws, stock.
name);
103 while (loadedPrice != -1) {
104 stock.
history.emplace_back(loadedPrice);
118 std::string emptyLine;
119 std::getline(fin >> std::ws, emptyLine);
120 std::getline(fin >> std::ws, emptyLine);
121 std::string loadedEventString;
122 while (std::getline(fin, loadedEventString)) {
124 std::istringstream(loadedEventString) >> loadedEvent;
133 assert(loadedEvent == comparedEvent &&
"Invalid event loaded");
161 return total_revenue;
169 return value < 0 ? 0 : static_cast<unsigned int>(value);
178 std::vector<float> recent_history;
179 if (rounds >=
history.size()) {
182 for (
unsigned int i =
history.size() - rounds; i <
history.size(); i++) {
183 recent_history.emplace_back(
history[i]);
185 return recent_history;
219 std::list<Stock_event>::iterator event_itr =
events.begin();
220 while (event_itr !=
events.end()) {
221 if (*event_itr == event) {
222 event_itr =
events.erase(event_itr);
228 events.emplace_back(event);
232 std::list<Stock_event>::iterator event_itr =
events.begin();
233 while (event_itr !=
events.end()) {
234 if (!event_itr->mutually_exclusive_events.empty()) {
235 for (
unsigned int i = 0; i < event_itr->mutually_exclusive_events.size();
237 if (event_itr->mutually_exclusive_events[i] == event.
event_id) {
248 std::list<Stock_event>::iterator event_itr =
events.begin();
249 while (event_itr !=
events.end()) {
250 if (event_itr->duration <= 0) {
251 event_itr =
events.erase(event_itr);
261 std::list<Stock_event>::iterator event_itr =
events.begin();
262 while (event_itr !=
events.end()) {
263 sum += event_itr->modifiers[attribute];
284 std::list<Stock_event>::iterator event_itr =
events.begin();
285 while (event_itr !=
events.end()) {
290 event_itr->duration = 0;
295 price *= (1 + price_diff);
308 std::vector<unsigned int> event_ids;
309 std::list<Stock_event>::iterator event_itr =
events.begin();
310 while (event_itr !=
events.end()) {
311 event_ids.emplace_back(event_itr->event_id);
323 switch (sortMethod) {
325 std::sort(stocks_list.begin(), stocks_list.end(),
326 [](
Stock a,
Stock b) { return a.get_name() < b.get_name(); });
329 std::sort(stocks_list.begin(), stocks_list.end(),
330 [](
Stock a,
Stock b) { return a.get_category() < b.get_category(); });
333 std::sort(stocks_list.begin(), stocks_list.end(),
334 [](
Stock a,
Stock b) { return a.get_price() < b.get_price(); });
337 std::sort(stocks_list.begin(), stocks_list.end(),
338 [](
Stock a,
Stock b) { return a.get_quantity() < b.get_quantity(); });
341 std::sort(stocks_list.begin(), stocks_list.end(), [](
Stock a,
Stock b) {
342 return a.get_total_attribute(standard_deviation) <
343 b.get_total_attribute(standard_deviation);
347 std::sort(stocks_list.begin(), stocks_list.end(), [](
Stock a,
Stock b) {
348 return a.get_total_attribute(mean) < b.get_total_attribute(mean);
352 std::sort(stocks_list.begin(), stocks_list.end(), [](
Stock a,
Stock b) {
353 return a.get_total_attribute(lower_limit) <
354 b.get_total_attribute(lower_limit);
358 std::sort(stocks_list.begin(), stocks_list.end(), [](
Stock a,
Stock b) {
359 return a.get_total_attribute(upper_limit) <
360 b.get_total_attribute(upper_limit);
367 std::reverse(stocks_list.begin(), stocks_list.end());
A class that represents a stock object in the game.
std::string category_name(void) const
Return the name of the category the stock belongs to.
unsigned int split_count
Contains the spliting count of a stock.
float sell(float &balance, unsigned int amount, float trading_fees_percent)
Sell a given number of stocks.
float calculateStockValue(const float &trading_fees_percent) const
Calculate the total value of the stocks the player is holding.
void add_event(const Stock_event &event)
Add an event to the stock.
unsigned int num_stocks_affordable(float balance, float trading_fees_percent) const
std::vector< float > history
Contains the stock price history.
std::map< stock_modifiers, float > attributes
Stores the initial value of the stock_modifiers (e.g.
unsigned int category
Use numbers to represent the category of the stock.
float price
Current price of the stock.
float delta_price(void)
Return the change of stock price using the most recent price and the current price.
std::string name
Name of the stock that we assigned to it.
bool can_add_event(const Stock_event &event)
Check if we can add the event to the stock.
float delta_price_percentage(void)
Return the percentage of change of stock price.
std::list< Stock_event > events
Stores all the events that will apply to this stock specifically.
void update_history(void)
Update the history array with the current price.
void next_round(void)
Call this when the game proceed to next round.
std::vector< float > return_most_recent_history(unsigned int rounds)
Get the stock price of recent rounds.
unsigned int quantity
Number of stocks the player has purchased.
void save(const std::string &playerName, int i)
Save the stock from a file.
void remove_obselete_event(void)
Remove obselete events from the list events that durations are less than/equal to 0 (In other words,...
Stock(void)
Constructor of class Stock.
void load(const std::string &playerName, int i)
Load the stock from a file.
float calculateTradingFeesLost(const float &trading_fees_percent) const
Calculate the amount of money player lost due to trading fees.
std::vector< unsigned int > get_event_ids(void)
Get the event ids of all the events that will apply to this stock specifically.
float get_total_attribute(stock_modifiers attribute)
Sums up get_base_attribute() and get_event_attribute().
float purchase(float &balance, unsigned int amount, float trading_fees_percent)
Purchase a given number of stocks.
float get_event_attribute(stock_modifiers attribute)
Get the total change of attribute of the stock due to events only.
Stock_event setup_STOCK_SPLIT_EVENT(void)
Set up a getStockSplitEvent() with proper values.
Stock_event getStockSplitEvent(void)
const std::vector< Stock_event > all_stock_events
The list of all events that can be applied to the stocks.
stock_modifiers
The attributes of a stock that Events will modify are hardcoded here.
@ upper_limit
The upper limit of the stock price percentage change.
@ standard_deviation
Amount of variation of the stock price percentage change.
@ mean
The expectation of the stock price percentage change.
@ lower_limit
The lower limit of the stock price percentage change.
const float defaultUpperLimit
Default upper limit.
const float defaultLowerLimit
Default lower limit.
constexpr float defaultMean
Default mean.
const unsigned int durationDecreaseMultiplier
Rate of decrease of duration.
Header files for file operation functions related to the game.
const std::string SAVE_FOLDER_PREFIX
const std::string SAVE_FILE_EXTENSION_TXT
const float trading_fees_percent
/ 100 means charging % more/portion of the money involved in stock operations.
float balance
Player's balance.
std::string playerName
Player's name.
vector< string > generate_name(unsigned int category, unsigned int num)
Generates a set of unique stock names based on the specified category and quantity.
string const category_list[category_list_size]
List of stock categories.
Declaration of the name generating function.
const int category_list_size
The size of the category list.
float init_stock_price(int price_profile)
Initialize starting stock price.
float percentage_change_price(Stock stock)
Calculate the percentage change of the stock price.
unsigned int random_integer(unsigned int max_integer)
python randint like function
float init_sd(void)
Initialize the standard deviation of the stock price.
Header file for random related functions.
std::ostream & operator<<(std::ostream &fout, const Stock &stock)
const int INVALID_OPERATION
void sortStocksList(std::vector< Stock > &stocks_list, SortingMethods sortMethod, SortingDirections sortDirection)
Sorts the stocks.
std::istream & operator>>(std::istream &fin, Stock &stock)
Declaration of the Stock class.
const float STOCK_PRICE_LIMIT
The upper limit of the stock price.
The data structure of an event that will be applied to the stocks.
std::string text
The text that will be displayed to the player.
unsigned int event_id
The id of the event.