-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveload.cpp
215 lines (180 loc) · 5.82 KB
/
Saveload.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "Saveload.h"
Saveload::Saveload(){
getAllSavedFile();
}
Saveload::~Saveload() { }
int Saveload::getAllSavedFile(){
this->listOfSavedGames.clear();
std::ifstream file{GAMESAVE};
string gameNames = "";
// if the saves.azul file exist get all Name
if(file){
while(file){
std::getline(file, gameNames);
if(gameNames.length()>0){ //To prevent passing in the empty Line
this->listOfSavedGames.push_back(gameNames);
}
}
file.close();
return SAVEFILE_EXIST;
}else{
file.close();
return SAVEFILE_DOESNT_EXIST;
}
}
bool Saveload::gameNameExist(string gameName){
if(std::find(this->listOfSavedGames.begin(), this->listOfSavedGames.end(), gameName) != this->listOfSavedGames.end()) {
//if gameName Exist
return true;
}
return false;
}
int Saveload::saveGame(string factories, string mosaic, string tileBagBoxLid, string player, string gameName){
getAllSavedFile();
//if saves.azul file exist and has game saved inside of it
if(gameNameExist(gameName)==false){
//check if the gameName already exist, if not exist
//create saves.azul, append to it if the function is called again
std::ofstream mainfile{GAMESAVE, std::ios::app};
if (!mainfile)
{
// Print an error and return state
std::cerr << "couldn't open the file " << GAMESAVE << '\n';
return SAVEGAME_FAIL;
}
mainfile << gameName << '\n';
mainfile.close();
}
string fileName = gameName + string("-");
//create the factories.save
std::ofstream frfile(fileName+FSAVE);
if (!frfile)
{
// Print an error and return state
std::cerr << "couldn't open the file " << FSAVE << '\n';
return SAVEGAME_FAIL;
}
frfile << factories;
frfile.close();
//create the mosaic.save
std::ofstream msfile(fileName+MSAVE);
if (!msfile)
{
// Print an error and return state
std::cerr << "couldn't open the file " << MSAVE << '\n';
return SAVEGAME_FAIL;
}
msfile << mosaic;
msfile.close();
//create tilebag_boxlid.save
std::ofstream tbfile(fileName+TSAVE);
if (!tbfile)
{
// Print an error and return state
std::cerr << "couldn't open the file " << TSAVE << '\n';
return SAVEGAME_FAIL;
}
tbfile << tileBagBoxLid;
tbfile.close();
//create players.save
std::ofstream plfile(fileName+PSAVE);
if (!plfile)
{
// Print an error and return state
std::cerr << "couldn't open the file " << PSAVE << '\n';
return SAVEGAME_FAIL;
}
plfile << player;
plfile.close();
return SAVEGAME_SUCCESS;
}
int Saveload::loadGameCheckFileStatus(){
if(getAllSavedFile()==SAVEFILE_DOESNT_EXIST){
return LOADGAME_FAIL;
}
string currentGame;
string gameName = "";
for(string currentGame: this->listOfSavedGames){
gameName = currentGame + string("-");
std::ifstream factorySave(gameName + FSAVE);
if(!factorySave){
std::cerr << "cannot open or located the " << FSAVE << " in " << currentGame << '\n';
return LOADGAME_FAIL;
}
std::ifstream mosaicSave(gameName + MSAVE);
if(!mosaicSave){
std::cerr << "cannot open or located the " << MSAVE << " in " << currentGame << '\n';
return LOADGAME_FAIL;
}
std::ifstream tileBagSave(gameName + TSAVE);
if(!tileBagSave){
std::cerr << "cannot open or located the " << TSAVE << " in " << currentGame << '\n';
return LOADGAME_FAIL;
}
std::ifstream playersSave(gameName + PSAVE);
if(!playersSave){
std::cerr << "cannot open or located the " << PSAVE << " in " << currentGame << '\n';
return LOADGAME_FAIL;
}
}
//if all the files exists
return LOADGAME_SUCCESS;
};
vector<string> Saveload::getListOfGameSaves(){
return this->listOfSavedGames;
}
int Saveload::listOfSavedGamesSize(){
return this->listOfSavedGames.size();
}
vector<string> Saveload::loadGame_factories(string gameName){
string fileName = gameName + string("-");
std::ifstream file(fileName + FSAVE);
string dataInput;
vector<string> factoryData;
while(file){
std::getline(file, dataInput);
if(dataInput.length()>0){ //To prevent passing in the empty Line
factoryData.push_back(dataInput);
}
}
return factoryData;
}
vector<string> Saveload::loadGame_mosaic(string gameName){
string fileName = gameName + string("-");
std::ifstream file(fileName + MSAVE);
string dataInput;
vector<string> mosaicData;
while(file){
std::getline(file, dataInput);
if(dataInput.length()>0){ //To prevent passing in the empty Line
mosaicData.push_back(dataInput);
}
}
return mosaicData;
}
vector<string> Saveload::loadGame_tileBagBoxLid(string gameName){
string fileName = gameName + string("-");
std::ifstream file(fileName + TSAVE);
string dataInput;
vector<string> tileBoxLidData;
while(file){
std::getline(file, dataInput);
if(dataInput.length()>0){ //To prevent passing in the empty Line
tileBoxLidData.push_back(dataInput);
}
}
return tileBoxLidData;
}
vector<string> Saveload::loadGame_players(string gameName){
string fileName = gameName + string("-");
std::ifstream file(fileName + PSAVE);
string dataInput;
vector<string> playerData;
while(file){
std::getline(file, dataInput);
if(dataInput.length()>0){ //To prevent passing in the empty Line
playerData.push_back(dataInput);
}
}
return playerData;
}