-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameHandler.cpp
383 lines (347 loc) · 8.88 KB
/
GameHandler.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include "GameHandler.h"
//metody publiczne
GameHandler::GameHandler()
{
playerid = 0;
clearBoard();
linesInDisplayBoard = 24;
}
GameHandler::~GameHandler()
{
for (auto p : players)
{
destroyVector <Profession*>(p->getUnits());
}
destroyVector<Player*>(players);
delete pf;
//std::cout << "Destroyed!!\n";
}
void GameHandler::createProfessionsFactory()
{
pf = new ProfessionsFactory();
pf->init();
}
void GameHandler::manageGame()
{
createPlayer();
createPlayer();
std::cout << "Take a look how does the board look like: \n";
displayBoard();
std::cout << "First all players have to buy units and place them on board.\n";
system("pause");
for (auto player : players)
{
askPlayerToBuyUnits(player);
putAllPlayerUnitsOnBoard(player);
}
updatePositionData();
system("cls");
//start rozgrywki
bool gameON = true;
while (gameON)
{
for (auto player : players)
{
if (!gameON) break;
for (auto unit : player->getUnits())
{
gameON = checkIfPlayersUnitsAreAlive();
if (!gameON) break;
if (unit->isAlive())
{
if (unit->getStunned()) //jesli unit jest zestunowany i jest jego kolej to zdejmuje mu stuna i pomijam jego ruch, stun ma zabierac jeden ruch
{
unit->setStunned(false);
continue;
}
displayBoard();
displayAllplayersUnitsAndBasicStats();
displayLog();
player->doSomethingWithUnit(unit, positionData, professionBoardData, player->getUnits(), players[abs(player->getID() - 1)]->getUnits(), gameLOG,gameON); //abs(player->getID()-1) daje przeciwnego playera, jesli player ma id 0 to przeciwny ma abs(0-1),jesli 1 to abs(1-1)
updatePositionData();
system("cls");
}
if (!gameON) break;//konieczne
}
}
updateAllUnitsStatsAfterRound();
roundsCounter++;
}
displayBoard();
displayAllplayersUnitsAndBasicStats();
displayLog();
printGameLogToFileAndDelete();
std::cout << "<----- GAME OVER ----->\n";
system("pause");
return;
}
void GameHandler::resetData()
{
for (auto p : players)
{
destroyVector <Profession*>(p->getUnits());
}
destroyVector<Player*>(players);
clearBoard();
playerid = 0;
}
//metody prywatne
void GameHandler::displayBoard()
{
std::cout << std::endl << " ";
for (int i = 0; i < 10; i++) std::cout << " " << i << " ";
std::cout << std::endl;
std::cout << " " << char(201) << char(205);
for (int i = 0; i < 2 * BOARD_SIZE - 1; i++)
{
if (i % 2 == 0) std::cout << char(205);
else std::cout << char(205) << char(203) << char(205);
}
std::cout << char(205) << char(187) << std::endl;
for (int j = 0; j < 10; j++)
{
std::cout << j << " ";
for (int i = 0; i < 2 * BOARD_SIZE + 1; i++)
{
if (i % 2 == 0) std::cout << char(186);
else std::cout << " " << positionData[i / 2][j] << " ";
}
if (j == 9)break;
std::cout << std::endl << " " << char(204) << char(205);
for (int i = 0; i < 2 * BOARD_SIZE - 1; i++)
{
if (i % 2 == 0) std::cout << char(205);
else std::cout << char(205) << char(206) << char(205);
}
std::cout << char(205) << char(185) << std::endl;
}
std::cout << std::endl << " " << char(200) << char(205);
for (int i = 0; i < 2 * BOARD_SIZE - 1; i++)
{
if (i % 2 == 0) std::cout << char(205);
else std::cout << char(205) << char(202) << char(205);
}
std::cout << char(205) << char(188) << std::endl;
}
void GameHandler::clearBoard()
{
for (int i = 0; i < BOARD_SIZE; i++)
{
for (int j = 0; j < BOARD_SIZE; j++)
{
positionData[i][j] = ' ';
professionBoardData[i][j] = nullptr;
}
}
}
void GameHandler::updatePositionData()
{
clearBoard();
for (auto player : players)
{
for (auto player_unit : player->getUnits())
{
if (player_unit->isAlive())
{
positionData[player_unit->getPosition().first][player_unit->getPosition().second] = player_unit->getDisplayChar();
professionBoardData[player_unit->getPosition().first][player_unit->getPosition().second] = player_unit;
}
}
}
}
void GameHandler::displayAllplayersUnitsAndBasicStats()
{
//w tej metodzie i nastepnej cofam linie w coucie i wyswietlam obok juz wyswietlonych rzeczy inne rzeczy uzywaj¹c kilku tab
//wygl¹da to troche nieelegancko ale dzia³a i nie znalaz³em innego sposobu
int newNewLines = 0;
for (int i = 0; i < linesInDisplayBoard - 5; i++)
{
std::cout << "\033[F"; //cofam linie w coucie
}
for (auto p : players)
{
std::cout << "\t\t\t\t\t\t\t" << "Player " << p->getID()+1 << ":\n";
newNewLines++;
for (auto u : p->getUnits())
{
if (u->isAlive())
{
if (u->getStunned())
{
std::cout << "\t\t\t\t\t\t\t" << u->getPosition() << " " << u->getName() << " STUNNED\n";
continue;
}
std::cout << "\t\t\t\t\t\t\t" << u->getPosition() << " " << u->getName() << " HP: " << u->getCurrentHP() << "/" << u->getStat(HP_MAX);
std::cout << " MANA: " << u->getCurrentMana() << "/" << u->getStat(MANA_MAX) << " AR: " << u->getStat(AR) << "\n";
}
else
std::cout << "\t\t\t\t\t\t\t" << u->getPosition() << " " << u->getName() << " DEATH \n";
newNewLines++;
}
}
for (int i = 0; i < linesInDisplayBoard - 5 - newNewLines; i++)
{
std::cout << "\n"; //odcofuje linie w coucie
}
}
void GameHandler::displayLog()
{
int linesToBack = 0;
if (!gameLOG.empty())
{
linesToBack = 1;
std::cout << "\t\t\t\t\t\t" << "Game LOG: \n";
}
for (int i = (int)gameLOG.size() - 1; i > (int)gameLOG.size() - 6; i--)
{
if (i < 0) break;
if (gameLOG.empty()) break;
std::cout << "\t\t\t\t\t\t" << gameLOG[i];
linesToBack++;
}
for (int i = 0; i < linesToBack; i++) std::cout << "\033[F"; //cofam linie w coucie
}
void GameHandler::createPlayer()
{
players.push_back(new Player(playerid++));
}
void GameHandler::askPlayerToBuyUnits(Player* _p)
{
int id;
while (true)
{
system("cls");
std::cout << "Hi player " << _p->getID() +1<< std::endl;//ewentualnie nickname
std::cout << "You have " << _p->getGold() << " gold.\n";
pf->getProfLibrary()->displayUnitsAndCosts();
std::cout << "Write profession id to buy unit or -1 to exit shop\n";
id=getValidInput<int>();
if (id == -1)
{
if (_p->getUnits().size() <= 0)
{
std::cout << "You have to buy at least 1 unit\n";
system("pause");
continue;
}
else
break;
}
try
{
if (pf->getProfLibrary()->getProfessionCost(id) > _p->getGold()) // playera nie stac
std::cout << "You have not enough gold to buy this unit!\n";
else
_p->buyUnit(pf->createUnit(id), pf->getCost(id));
}
catch (const char* error)
{
std::cout << "Something went wrong,check if unit ID is valid\n";
system("pause");
}
}
}
void GameHandler::putAllPlayerUnitsOnBoard(Player* _p)
{
system("cls");
for (auto unit : _p->getUnits())
{
bool ok = false;
std::cout << "P" << _p->getID() + 1 << ":\n";
std::cout << "Write x and y cords to put your " << unit->getName() << " on board.\n";
std::pair<int, int> coords;
while (ok != true)
{
coords = askPlayersForCordsToPutUnit();
if (_p->getID() == 0 && coords.second > 3)
{
std::cout << "At the beginning you must put your unit on one of first 3 rows\nTry again\n";
continue;
}
if (_p->getID() == 1 && coords.second < 6)
{
std::cout << "At the beginning you must put your unit on one of last 3 rows\nTry again\n";
continue;
}
ok = true;
}
unit->setPosition(coords);
updatePositionData();
system("cls");
}
std::stringstream allplayerUnits;
allplayerUnits<<"P" <<(_p->getID() + 1) << " ";
for (auto unit : _p->getUnits())
{
allplayerUnits << unit->getName() << " " << unit->getPosition() << " ";
}
allplayerUnits << "\n";
gameLOG.push_back(allplayerUnits.str());
}
std::pair<int, int> GameHandler::askPlayersForCordsToPutUnit()
{
int x=-1, y=-1;
while (x < 0 || x>9 || y < 0 || y>9)
{
std::cout << "Write x cord: \n";
x=getValidInput<int>();
std::cout << "Write y cord: \n";
y=getValidInput<int>();
if (x < 0 || x>9 || y < 0 || y>9||professionBoardData[x][y] != nullptr)
{
std::cout << "There is already unit on this coords/Out of board range, try again\n";
x = -1;
y = -1;
}
}
return std::make_pair(x, y);
}
bool GameHandler::checkIfPlayersUnitsAreAlive()
{
bool temp = true;
for (auto p : players)
{
temp = false;
for (auto u : p->getUnits())
{
if (u->isAlive()&&temp==false)
{
temp=true;
}
}
if (temp == false)
{
gameLOG.push_back("Game Finished\n");
return temp;
}
}
return temp;
}
void GameHandler::printGameLogToFileAndDelete()
{
const time_t time = std::time(nullptr);
std::ostringstream oss;
oss<< std::put_time(std::localtime(&time), "%F_%T");
std::string fileName = "LOG_" + oss.str() + ".txt";
std::replace(fileName.begin(), fileName.end(), ':', ';'); // w nazwie pliku nie moze byc :
std::ofstream file(fileName);
if (file.is_open())
{
while (!gameLOG.empty())
{
file << gameLOG.back();
gameLOG.pop_back();
}
file.close();
}
}
void GameHandler::updateAllUnitsStatsAfterRound()
{
for (auto p : players)
{
for (auto u : p->getUnits())
{
u->updateStatsAfterRound();
}
}
}