Stock Market Simulator main e8c3612
A game that provides a realistic stock buying experience with unpredictable trends to test investment strategies.
Loading...
Searching...
No Matches
file_io.h File Reference

Header files for file operation functions related to the game. More...

#include "stock.h"
Include dependency graph for file_io.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  USER_SAVE_OPTION
 The user save options. More...
 

Functions

bool checkValidInput (const std::string &input)
 Check if the input is valid.
 
void createplayer (std::string &playerName)
 Create a player folder.
 
void delsave (std::string &mode)
 Delete a save.
 
std::vector< std::string > get_saves (void)
 Get the list of saves aka player folders.
 
void loadstatus (unsigned int &rounds_played, std::vector< Stock > &stocks_list, float &balance, std::string &playerName, std::vector< float > &hsi_history)
 Load an existing game status from .save files.
 
std::vector< std::string > parseLogo (void)
 returns the game logo, which is hardcoded inside the function.
 
void printvector (const std::vector< std::string > &avector)
 Print the vector of saves aka player folders.
 
void savestatus (unsigned int rounds_played, std::vector< Stock > stocks_list, float balance, const std::string &playerName)
 Save the game status into *.save files.
 

Variables

const std::string SAVE_FILE_EXTENSION_TXT = ".save"
 
const std::string SAVE_FOLDER_PREFIX = "saves/"
 
const std::string USER_SAVE_OPTION_PROMPT
 

Detailed Description

Header files for file operation functions related to the game.

Definition in file file_io.h.

Function Documentation

◆ checkValidInput()

bool checkValidInput ( const std::string & input)
inline

Check if the input is valid.

The input is valid if it is one of the options in the USER_SAVE_OPTION class.

Parameters
inputThe input string
Returns
true if the input is valid, false otherwise

Definition at line 37 of file file_io.h.

37 {
38 return input.compare(USER_SAVE_OPTION::NEW_GAME) == 0 ||
39 input.compare(USER_SAVE_OPTION::LOAD_GAME) == 0 ||
40 input.compare(USER_SAVE_OPTION::DELETE_GAME) == 0 ||
41 input.compare(USER_SAVE_OPTION::EXIT_GAME) == 0;
42}
static const std::string DELETE_GAME
Definition file_io.h:25
static const std::string EXIT_GAME
Definition file_io.h:26
static const std::string NEW_GAME
Definition file_io.h:23
static const std::string LOAD_GAME
Definition file_io.h:24

References USER_SAVE_OPTION::DELETE_GAME, USER_SAVE_OPTION::EXIT_GAME, USER_SAVE_OPTION::LOAD_GAME, and USER_SAVE_OPTION::NEW_GAME.

Referenced by initializePlayerSaves().

Here is the caller graph for this function:

◆ createplayer()

void createplayer ( std::string & playerName)

Create a player folder.

Parameters
playerNamePass the playerName by reference.

Definition at line 69 of file file_io.cpp.

69 {
70 ofstream fout;
71 string savefolder =
72 SAVE_FOLDER_PREFIX; // create folder when it does not exist (first run)
73 string foldername;
74 filesystem::create_directory(SAVE_FOLDER_PREFIX);
75 cout << "Enter player name:" << endl;
76 getline(cin, playerName);
77 foldername = SAVE_FOLDER_PREFIX + playerName;
78 while ((filesystem::exists(foldername) || playerName.find(' ') != string::npos) ||
79 playerName.empty()) { // check whether file already exists
80 if (!playerName.empty()) {
81 cout << "Invalid Playername. ";
82 cout << "Playername should not contain spaces or cannot be the same as "
83 "existing ";
84 cout << "playername" << endl << "Please enter a new player name: " << endl;
85 }
86 getline(cin, playerName);
87 foldername = SAVE_FOLDER_PREFIX + playerName;
88 }
89 filesystem::create_directory(foldername); // create a empty folder for new save
90}
const std::string SAVE_FOLDER_PREFIX
Definition file_io.h:45
std::string playerName
Player's name.
Definition main.cpp:73

References playerName, and SAVE_FOLDER_PREFIX.

Referenced by initializePlayerSaves(), and loadstatus().

Here is the caller graph for this function:

◆ delsave()

void delsave ( std::string & mode)

Delete a save.

Parameters
modeReturn the mode choice (0,1,2,3) by reference for further control after deleting

Definition at line 158 of file file_io.cpp.

158 {
159 string stockdel;
160 string stockname;
161 string inputname;
162 string confirm;
163 ifstream fin;
164 vector<string> players;
165 filesystem::create_directory(
166 SAVE_FOLDER_PREFIX); // prevent error when no folder exists
167 players = get_saves(); // generate a vector of name of folders
168 if (players.empty()) {
169 cout << "No player saves found, please enter " << USER_SAVE_OPTION::NEW_GAME
170 << " for new save or enter " << USER_SAVE_OPTION::EXIT_GAME
171 << " to quit: ";
172 std::cin >> mode;
173 while (
175 std::cout << "Invalid input. Please enter " + USER_SAVE_OPTION::NEW_GAME
176 << " for new save or enter " + USER_SAVE_OPTION::EXIT_GAME +
177 " to quit: ";
178 std::cin >> mode; // choose new file or load previous file
179 }
180 return;
181 }
182 cout << "Enter player name from the following:" << endl;
183 printvector(players);
184 while (std::find(players.begin(), players.end(), inputname) ==
185 players.end()) { // reject incorrect input
186 if (!inputname.empty()) {
187 cout << "Player name does not exist, please enter a new name from the "
188 "following: "
189 << endl;
190 printvector(players);
191 }
192 getline(cin, inputname);
193 }
194
195 cout << "WARNING! This action is irreversible and will delete all data associated "
196 "with the player save."
197 << endl;
198 cout << "Player save " << inputname
199 << " is going to be deleted, please enter Y to confirm" << endl;
200 cin >> confirm;
201 if (confirm == "Y" || confirm == "y") {
202 stockdel = SAVE_FOLDER_PREFIX + inputname;
203 for (const auto & path : std::filesystem::directory_iterator(stockdel)) {
204 std::filesystem::remove(path);
205 }
206 std::filesystem::remove_all(stockdel);
207 cout << "Player save " << inputname << " has been deleted." << endl;
208 }
209 else {
210 cout << "The deletion has been cancelled." << endl;
211 }
212}
void printvector(const vector< string > &avector)
Print the vector of saves aka player folders.
Definition file_io.cpp:222
vector< string > get_saves(void)
Get the list of saves aka player folders.
Definition file_io.cpp:214
mode
hiding mean/sd/uplim/lowlim/event_id columns in the table
Definition main.cpp:104

References USER_SAVE_OPTION::EXIT_GAME, get_saves(), USER_SAVE_OPTION::NEW_GAME, printvector(), and SAVE_FOLDER_PREFIX.

Referenced by initializePlayerSaves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_saves()

std::vector< std::string > get_saves ( void )

Get the list of saves aka player folders.

Returns
a vector containing the name all the player folders

Definition at line 214 of file file_io.cpp.

214 {
215 vector<string> saves;
216 for (const auto & entry : std::filesystem::directory_iterator(SAVE_FOLDER_PREFIX)) {
217 saves.emplace_back(entry.path().string().substr(SAVE_FOLDER_PREFIX.size()));
218 }
219 return saves;
220}

References SAVE_FOLDER_PREFIX.

Referenced by delsave(), and loadstatus().

Here is the caller graph for this function:

◆ loadstatus()

void loadstatus ( unsigned int & rounds_played,
std::vector< Stock > & stocks_list,
float & balance,
std::string & playerName,
std::vector< float > & hsi_history )

Load an existing game status from .save files.

Paramenters should be empty and values are returned by reference.

Parameters
rounds_playedThe number of rounds played
stocks_listA vector of stocks
balanceThe balance of the player
playerNameThe name of the player also the folder name
hsi_historyA vector of HSI history

Definition at line 120 of file file_io.cpp.

121 {
122 string stockload;
123 string stockname;
124 ifstream fin;
125 vector<string> players;
126 filesystem::create_directory(
127 SAVE_FOLDER_PREFIX); // prevent error when no folder exists
128 players = get_saves(); // generate a vector of name of folders
129 if (players.empty()) {
130 cout << "No player saves found, please create a new player." << endl;
132 return; // rejection of loading when no save files exist
133 }
134 cout << "Enter player name from the following:" << endl;
135 printvector(players);
136 while (std::find(players.begin(), players.end(), playerName) ==
137 players.end()) { // reject incorrect input
138 if (!playerName.empty()) {
139 cout << "Player name does not exist, please enter a new name from the "
140 "following:"
141 << endl;
142 printvector(players);
143 }
144 getline(cin, playerName);
145 }
146 stockload =
148 fin.open(stockload.c_str());
149 fin >> playerName >> rounds_played >> balance;
150 fin.close(); // output basic info from playerstatus.save and return by reference
151 load_hsi(hsi_history, playerName);
152 for (unsigned long i = 0; i < initial_stock_count; i++) {
153 stocks_list[i].load(
154 playerName, i); // load stocks info to class in seperate files
155 }
156}
void createplayer(string &playerName)
Create a player folder.
Definition file_io.cpp:69
void load_hsi(std::vector< float > &hsi_history, const string &playerName)
Definition file_io.cpp:92
const std::string SAVE_FILE_EXTENSION_TXT
Definition file_io.h:48
float balance
Player's balance.
Definition main.cpp:68
unsigned int rounds_played
Number of rounds played.
Definition main.cpp:70
const int initial_stock_count
Initial stock count.
Definition stock.h:28

References balance, createplayer(), get_saves(), initial_stock_count, load_hsi(), playerName, printvector(), rounds_played, SAVE_FILE_EXTENSION_TXT, and SAVE_FOLDER_PREFIX.

Referenced by initializePlayerSaves().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parseLogo()

std::vector< std::string > parseLogo ( void )

returns the game logo, which is hardcoded inside the function.

Returns
a vector of strings, each string is a line of the logo

Definition at line 34 of file file_io.cpp.

34 {
35 vector<string> logo;
36 // clang-format off
37 logo.reserve(27);
38 logo.emplace_back(R"( __ __)");
39 logo.emplace_back(R"( | \ | \)");
40 logo.emplace_back(R"( _______ _| $$_ ______ _______| $$ __)");
41 logo.emplace_back(R"( / | $$ \ / \ / | $$ / \)");
42 logo.emplace_back(R"(| $$$$$$$\$$$$$$ | $$$$$$| $$$$$$| $$_/ $$)");
43 logo.emplace_back(R"( \$$ \ | $$ __| $$ | $| $$ | $$ $$)");
44 logo.emplace_back(R"( _\$$$$$$\ | $$| | $$__/ $| $$_____| $$$$$$\)");
45 logo.emplace_back(R"(| $$ \$$ $$\$$ $$\$$ | $$ \$$\)");
46 logo.emplace_back(R"( \$$$$$$$ \$$$$ \$$$$$$ \$$$$$$$\$$ \$$)");
47 logo.emplace_back(R"( __ __)");
48 logo.emplace_back(R"( | \ | \)");
49 logo.emplace_back(R"( ______ ____ ______ ______ | $$ __ ______ _| $$_)");
50 logo.emplace_back(R"(| \ \ | \ / \| $$ / \/ | $$ \)");
51 logo.emplace_back(R"(| $$$$$$\$$$$\ \$$$$$$| $$$$$$| $$_/ $| $$$$$$\$$$$$$)");
52 logo.emplace_back(R"(| $$ | $$ | $$/ $| $$ \$| $$ $$| $$ $$| $$ __)");
53 logo.emplace_back(R"(| $$ | $$ | $| $$$$$$| $$ | $$$$$$\| $$$$$$$$| $$| \)");
54 logo.emplace_back(R"(| $$ | $$ | $$\$$ $| $$ | $$ \$$\\$$ \ \$$ $$)");
55 logo.emplace_back(R"( \$$ \$$ \$$ \$$$$$$$\$$ \$$ \$$ \$$$$$$$ \$$$$)");
56 logo.emplace_back(R"( __ __ __)");
57 logo.emplace_back(R"( | \ | \ | \)");
58 logo.emplace_back(R"( _______ \$$______ ____ __ __| $$ ______ _| $$_ ______ ______)");
59 logo.emplace_back(R"( / | | \ \| \ | | $$| | $$ \ / \ / \)");
60 logo.emplace_back(R"(| $$$$$$| $| $$$$$$\$$$$| $$ | $| $$ \$$$$$$\$$$$$$ | $$$$$$| $$$$$$\)");
61 logo.emplace_back(R"( \$$ \| $| $$ | $$ | $| $$ | $| $$/ $$| $$ __| $$ | $| $$ \$$)");
62 logo.emplace_back(R"( _\$$$$$$| $| $$ | $$ | $| $$__/ $| $| $$$$$$$| $$| | $$__/ $| $$)");
63 logo.emplace_back(R"(| $| $| $$ | $$ | $$\$$ $| $$\$$ $$ \$$ $$\$$ $| $$)");
64 logo.emplace_back(R"( \$$$$$$$ \$$\$$ \$$ \$$ \$$$$$$ \$$ \$$$$$$$ \$$$$ \$$$$$$ \$$ )");
65 // clang-format on
66 return logo;
67}

Referenced by drawLogo().

Here is the caller graph for this function:

◆ printvector()

void printvector ( const std::vector< std::string > & avector)

Print the vector of saves aka player folders.

Definition at line 222 of file file_io.cpp.

222 {
223 cout << avector[0];
224 // note: start from 1 to avoid printing the first element twice
225 for (unsigned long i = 1; i < avector.size(); i++) {
226 cout << ", " << avector[i];
227 }
228 cout << endl;
229}

Referenced by delsave(), and loadstatus().

Here is the caller graph for this function:

◆ savestatus()

void savestatus ( unsigned int rounds_played,
std::vector< Stock > stocks_list,
float balance,
const std::string & playerName )

Save the game status into *.save files.

Parameters
rounds_playedThe number of rounds played
stocks_listA vector of stocks
balanceThe balance of the player
playerNameThe name of the player also the folder name

Definition at line 104 of file file_io.cpp.

105 {
106 string stocksave;
107 ofstream fout;
108 stocksave =
110 fout.open(stocksave.c_str());
111 fout << playerName << " " << rounds_played << " " << balance
112 << endl; // saving basic info out of class inside playerstatus.save
113 fout.close();
114 for (unsigned long i = 0; i < stocks_list.size(); i++) {
115 stocks_list[i].save(
116 playerName, i); // save stock info inside class in seperate file
117 }
118}

References balance, playerName, rounds_played, SAVE_FILE_EXTENSION_TXT, and SAVE_FOLDER_PREFIX.

Referenced by initializePlayerSaves(), and main().

Here is the caller graph for this function:

Variable Documentation

◆ SAVE_FILE_EXTENSION_TXT

const std::string SAVE_FILE_EXTENSION_TXT = ".save"

Definition at line 48 of file file_io.h.

Referenced by get_hsi(), graphinput(), Stock::load(), load_hsi(), loadstatus(), Stock::save(), and savestatus().

◆ SAVE_FOLDER_PREFIX

const std::string SAVE_FOLDER_PREFIX = "saves/"

◆ USER_SAVE_OPTION_PROMPT

const std::string USER_SAVE_OPTION_PROMPT
extern

Definition at line 28 of file file_io.cpp.

Referenced by initializePlayerSaves().