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.cpp
Go to the documentation of this file.
1/// @file file_io.cpp
2/// Implementation of the file input/output functions.
3/*
4This program is free software: you can redistribute it and/or modify it under the
5terms of the GNU Lesser General Public License as published by the Free Software
6Foundation, either version 3 of the License, or (at your option) any later version.
7
8This program is distributed in the hope that it will be useful, but WITHOUT ANY
9WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
10PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
11
12You should have received a copy of the GNU Lesser General Public License along with this
13program. If not, see <https://www.gnu.org/licenses/>.
14*/
15
16#include "file_io.h"
17
18#include <algorithm>
19#include <filesystem>
20#include <fstream>
21#include <iostream>
22using namespace std;
23
24const std::string USER_SAVE_OPTION::NEW_GAME = "0";
25const std::string USER_SAVE_OPTION::LOAD_GAME = "1";
26const std::string USER_SAVE_OPTION::DELETE_GAME = "2";
27const std::string USER_SAVE_OPTION::EXIT_GAME = "3";
28const std::string USER_SAVE_OPTION_PROMPT =
29 "Please enter.\n" + USER_SAVE_OPTION::NEW_GAME + " for new save,\n" +
30 USER_SAVE_OPTION::LOAD_GAME + " for loading old save(s),\n" +
31 USER_SAVE_OPTION::DELETE_GAME + " for deleting save,\n" +
32 USER_SAVE_OPTION::EXIT_GAME + " to quit: ";
33
34vector<string> parseLogo(void) {
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}
68
69void createplayer(string & playerName) {
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}
91
92void load_hsi(std::vector<float> & hsi_history, const string & playerName) {
93 std::string filesave =
95 std::ifstream fin;
96 fin.open(filesave.c_str());
97 float hsi;
98 while (fin >> hsi) {
99 hsi_history.emplace_back(hsi);
100 }
101 fin.close();
102}
103
104void savestatus(unsigned int rounds_played, vector<Stock> stocks_list, float balance,
105 const string & playerName) {
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}
119
120void loadstatus(unsigned int & rounds_played, vector<Stock> & stocks_list,
121 float & balance, string & playerName, vector<float> & hsi_history) {
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}
157
158void delsave(string & mode) {
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}
213
214vector<string> get_saves(void) {
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}
221
222void printvector(const vector<string> & avector) {
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}
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
void loadstatus(unsigned int &rounds_played, vector< Stock > &stocks_list, float &balance, string &playerName, vector< float > &hsi_history)
Load an existing game status from .save files.
Definition file_io.cpp:120
const std::string USER_SAVE_OPTION_PROMPT
Definition file_io.cpp:28
vector< string > parseLogo(void)
returns the game logo, which is hardcoded inside the function.
Definition file_io.cpp:34
void delsave(string &mode)
Delete a save.
Definition file_io.cpp:158
void createplayer(string &playerName)
Create a player folder.
Definition file_io.cpp:69
void printvector(const vector< string > &avector)
Print the vector of saves aka player folders.
Definition file_io.cpp:222
void load_hsi(std::vector< float > &hsi_history, const string &playerName)
Definition file_io.cpp:92
void savestatus(unsigned int rounds_played, vector< Stock > stocks_list, float balance, const string &playerName)
Save the game status into *.save files.
Definition file_io.cpp:104
vector< string > get_saves(void)
Get the list of saves aka player folders.
Definition file_io.cpp:214
Header files for file operation functions related to the game.
const std::string SAVE_FOLDER_PREFIX
Definition file_io.h:45
const std::string SAVE_FILE_EXTENSION_TXT
Definition file_io.h:48
mode
hiding mean/sd/uplim/lowlim/event_id columns in the table
Definition main.cpp:104
float balance
Player's balance.
Definition main.cpp:68
unsigned int rounds_played
Number of rounds played.
Definition main.cpp:70
std::string playerName
Player's name.
Definition main.cpp:73
const int initial_stock_count
Initial stock count.
Definition stock.h:28