-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
349 lines (284 loc) · 6.9 KB
/
Game.cpp
File metadata and controls
349 lines (284 loc) · 6.9 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include "Game.h"
Game::Game()
{
choice = 0; //initialize choice
playing = true; //runs while playing is true
activeCharacter = 0; //for vector value
fileName = "characters.txt"; //obligatory file name ;)
}
Game::~Game() //virtual deconstructor
{
//something is supposed to go here :P
}
//Functions
bool is_empty(std::ifstream& infile)
{
while(infile.peek() == '\n')
{
infile.clear();
infile.ignore(1000, '\n');
}
return infile.peek() == std::ifstream::traits_type::eof();
}
void Game::initGame()
{
//std::string playerName;
//std::cout << "Enter your name: ";
//std::cin >> playerName;
//debug
/*Inventory inv;
inv.addItem(Weapon(0, "Wep"));
inv.addItem(Weapon(0, "Wep"));
inv.addItem(Weapon(0, "Wep"));
inv.addItem(Armor(0, "Amr"));
inv.addItem(Armor(0, "Amr"));
inv.addItem(Armor(0, "Amr"));
for (size_t i = 0; i < inv.size(); i++)
{
std::cout << inv[i].debugPrint() << '\n';
}*/
//debug
/*Puzzle puzzle("Puzzles/1.txt");
std::cout << puzzle.getAsString() << '\n';*/
std::ifstream infile;
infile.open("characters.txt");
if (infile.is_open() && !is_empty(infile))
this->loadCharacters();
else if (!infile.is_open())
std::cout << "File can't open!";
else if(infile.is_open() && is_empty(infile))
{
createNewCharacter();
this->saveCharacters();
}
infile.close();
}
void Game::mainMenu()
{
system("CLS");
//print menu
std::cout << "= MAIN MENU =" << "\n\n";
std::cout << "= Active Character: " << this->characters[activeCharacter].getName()
<< " =" << "\n\n";
std::cout << "= Current number of characters: " << this->characters.size()
<< " =" << "\n\n";
std::cout << "0: Quit" << '\n';
std::cout << "1: Travel" << '\n';
std::cout << "2: Shop" << '\n';
std::cout << "3: Character sheet" << '\n';
std::cout << "4: Create new character" << '\n';
std::cout << "5: Save characters" << '\n';
std::cout << "6: Load characters" << '\n';
std::cout << "7: Select character" << '\n';
std::cout << '\n';
std::cout << "\nChoice: ";
std::cin >> this->choice;
while (std::cin.fail())
{
std::cout << "Faulty input.\n";
std::cin.clear();
std::cin.ignore(100, '\n');
std::cout << "\nChoice: ";
std::cin >> this->choice;
}
std::cin.ignore(100, '\n');
std::cout << '\n';
//switch case
switch(this->choice)
{
//QUIT
case 0:
playing = false;
this->saveCharacters();
break;
//TRAVEL
case 1:
Travel();
break;
//SHOP
case 2:
std::cout << "We're closed." << '\n';
std::cout << "Press anything to continue...";
system("pause>0");
break;
//CHARACTER SHEET
case 3:
system("CLS");
characters[activeCharacter].printStats();
break;
//NEW CHARACTER
case 4:
createNewCharacter();
saveCharacters();
break;
//SAVE CHARACTER(S)
case 5:
saveCharacters();
system("pause>0");
break;
//LOAD CHARACTER(S)
case 6:
loadCharacters();
system("pause>0");
break;
//SWITCH CHARACTER(S)
case 7:
selectCharacter();
break;
//INVALID INPUT
default:
std::cout << "Try again, kid." << '\n';
std::cout << "Press anything to continue...";
system("pause>0");
break;
}
}
void Game::createNewCharacter()
{
std::string name = "";
std::cout << "Enter your character name: ";
getline(std::cin, name);
while (name == "" || name == " ")
{
int message = rand() % 2;
if (message == 1)
{
std::cout << "Well, you gotta have a name...";
system("pause>0");
system("CLS");
std::cout << "Enter your character name: ";
getline(std::cin, name);
}
else
{
std::cout << "Don't be shy, you can tell me...";
system("pause>0");
system("CLS");
std::cout << "Enter your character name: ";
getline(std::cin, name);
}
}
bool exist = false;
for (size_t i = 0; i < this->characters.size(); i++)
{
while (name == this->characters[i].getName())
{
exist = true;
std::cout << "Character already exists.\n\n";
std::cout << "Character name: ";
getline(std::cin, name);
}
}
characters.push_back(Character());
activeCharacter = characters.size() - 1;
characters[activeCharacter].initialize(name);
}
void Game::saveCharacters()
{
std::ofstream outfile(fileName);
if (outfile.is_open())
{
for (size_t i = 0; i < characters.size(); i++)
{
outfile << characters[i].getAsString() << '\n';
std::cout << characters[i].getName() << " saved.\n";
}
}
outfile.close();
}
void Game::loadCharacters()
{
std::ifstream infile(fileName);
//this->characters.clear();
std::string name = "";
int distanceTravelled = 0;
int currency = 0;
int level = 0;
int exp = 0;
int expNext = 0;
int vitality = 0;
int psyche = 0;
int hpMax = 0;
int ppMax = 0;
int offense = 0;
int defense = 0;
int iq = 0;
//int speed = 0;
std::string line = "";
std::stringstream str;
if (infile.is_open() && !is_empty(infile))
{
while (getline(infile, line))
{
if (line.empty())
{
str.clear();
str.ignore(1000, '\n');
}
else
{
str.str(line);
str >> name;
str >> distanceTravelled;
str >> currency;
str >> level;
str >> exp;
str >> expNext;
str >> vitality;
str >> psyche;
str >> hpMax;
str >> ppMax;
str >> offense;
str >> defense;
str >> iq;
//str >> speed;
Character temp(name, distanceTravelled, currency, level, exp, expNext,
vitality, psyche, hpMax, ppMax, offense, defense, iq);
this->characters.push_back(Character(temp));
std::cout << "Character " << name << " loaded!\n";
str.clear();
}
}
}
else if (infile.is_open() && is_empty(infile))
{
std::cout << "File is empty. No characters loaded!\n";
str.clear();
}
infile.close();
if (this->characters.size() <= 0)
{
throw "Error. No characters loaded, or it's an empty file.";
}
}
void Game::selectCharacter()
{
for (size_t i = 0; i < this->characters.size(); i++)
{
std::cout << i+1 << ": " << this->characters[i].getName()
<< ", Level: " << this->characters[i].getLevel() << "\n\n";
}
std::cout << "Select character: ";
std::cin >> this->choice;
this->choice -= 1;
while (std::cin.fail() || this->choice >= this->characters.size() || this->choice < 0)
{
std::cout << "Faulty input.\n";
std::cin.clear();
std::cin.ignore(100, '\n');
std::cout << "\nSelect character: ";
std::cin >> this->choice;
}
std::cin.ignore(100, '\n');
std::cout << '\n';
this->activeCharacter = this->choice;
std::cout << this->characters[this->activeCharacter].getName()
<< " has been selected.\n\n";
system("pause>0");
}
void Game::Travel()
{
this->characters[activeCharacter].travel();
Event event;
event.generateEvent(this->characters[activeCharacter], this->enemies);
}