-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaver.cpp
More file actions
executable file
·432 lines (387 loc) · 12.2 KB
/
Saver.cpp
File metadata and controls
executable file
·432 lines (387 loc) · 12.2 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
#include "Saver.h"
// need to put this in gameEngine
TileType Master_Wall[5][5]{DARKBLUE, YELLOW, RED, BLACK, LIGTHBLUE,
LIGTHBLUE, DARKBLUE, YELLOW, RED, BLACK,
BLACK, LIGTHBLUE, DARKBLUE, YELLOW, RED,
RED, BLACK, LIGTHBLUE, DARKBLUE, YELLOW,
YELLOW, RED, BLACK, LIGTHBLUE, DARKBLUE};
void Saver::save(GameEngine* gameEngine, std::string fileName)
{
std::ofstream outputStream(fileName);
return save(gameEngine, outputStream);
}
void Saver::save(GameEngine* gameEngine, std::ofstream& outputStream)
{
outputStream << (gameEngine->getPlayer(0) == gameEngine->getPlayerTurnID() ? "true" : "false") << std::endl;
// Player 1 index
outputStream << gameEngine->getPlayer(0)->getName() << std::endl;
outputStream << gameEngine->getPlayer(0)->getScore() << std::endl;
// Player 2 index
outputStream << gameEngine->getPlayer(1)->getName() << std::endl;
outputStream << gameEngine->getPlayer(1)->getScore() << std::endl;
std::vector<TileType> centerPile = gameEngine->getCenterPile();
for (unsigned int i = 0; i < centerPile.size(); ++i)
{
outputStream << char(centerPile.at(i));
}
outputStream << std::endl;
for (int i = 0; i < NUM_FACTORIES; ++i)
{
outputStream << gameEngine->getFactory(i)->toStringNoSpace() << std::endl;
}
// Player 1 mosiac
Mosaic* player1Mosaic = gameEngine->getPlayer(0)->getMosaic();
for (int i = 0; i < NUMBER_OF_LINES; ++i)
{
outputStream << player1Mosaic->getLine(i)->toString() << std::endl;
}
outputStream << player1Mosaic->getBrokenTiles()->toString() << std::endl;
// Need to output Player 1 wall
outputWall(outputStream, player1Mosaic);
// Player 2 mosiac
Mosaic* player2Mosaic = gameEngine->getPlayer(1)->getMosaic();
for (int i = 0; i < NUMBER_OF_LINES; ++i)
{
outputStream << player2Mosaic->getLine(i)->toString() << std::endl;
}
outputStream << player2Mosaic->getBrokenTiles()->toString() << std::endl;
// Need to output Player 2 wall
outputWall(outputStream, player2Mosaic);
outputStream << gameEngine->getLid()->toString() << std::endl;
outputStream << gameEngine->getBag()->toString() << std::endl;
outputStream.close();
}
GameEngine* Saver::load(std::string fileName, Menu* menu)
{
GameEngine* gameEngine = nullptr;
std::ifstream inputStream(fileName);
if (inputStream.good())
gameEngine = load(inputStream, menu);
else
throw "File not found";
return gameEngine;
}
GameEngine* Saver::load(std::istream& inputStream, Menu* menu)
{
GameEngine* gameEngine = new GameEngine(menu);
int numberOfEachTile[NUMBER_OF_COLOURS] = {0};
bool addedFirstTile = false;
int currentLine = 0;
std::string lines[SAVE_FILE_LINES_LENGTH];
while (inputStream.good() && currentLine < SAVE_FILE_LINES_LENGTH)
{
std::getline(inputStream, lines[currentLine++]);
}
if (currentLine != SAVE_FILE_LINES_LENGTH)
{
delete gameEngine;
throw "Incorrect number of lines, missing info";
}
char c = '\0';
// Check if it's player 1's turn
bool player1Turn = true;
std::istringstream player1TurnStream(lines[0]);
if (player1TurnStream.good())
{
std::string value;
player1TurnStream >> value;
if (value != "true" && value != "false")
{
delete gameEngine;
throw "Player 1 turn value is invalid";
}
player1Turn = value == "true";
}
// player 1
if (isWhiteSpace(lines[1]))
{
delete gameEngine;
throw "Player 1 name cannot be empty";
}
std::string player1Name = lines[1];
int player1Score;
bool player1Ai = false;
try
{
player1Score = std::stoi(lines[2]);
}
catch (...)
{
delete gameEngine;
throw "Player 1 score is not valid";
}
// player 2
if (isWhiteSpace(lines[3]))
{
delete gameEngine;
throw "Player 2 name cannot be empty";
}
std::string player2Name = lines[3];
int player2Score;
bool player2Ai = false;
try
{
player2Score = std::stoi(lines[4]);
}
catch (...)
{
delete gameEngine;
throw "Player 2 score is not valid";
}
// Create center factory
std::vector<TileType> centerFactory;
std::istringstream centerFactoryStream(lines[5]);
while (centerFactoryStream.get(c))
{
if (selectableTile(c) || (c == FIRSTPLAYER && !addedFirstTile))
{
if (selectableTile(c)) ++numberOfEachTile[getTileIndex(c)];
addedFirstTile |= c == FIRSTPLAYER;
centerFactory.push_back(charToTileType(c));
}
else
{
delete gameEngine;
if (addedFirstTile) throw "Duplicate first player tile found in center factory";
throw "Problem reading center factory";
}
}
gameEngine->fillCenterPile(centerFactory);
// Create all factories
Factory* factories[NUM_FACTORIES] = {nullptr};
for (int i = 0; i < NUM_FACTORIES; ++i)
{
// Create a single factory
TileType tiles[FACTORY_SIZE] = {NOTILE, NOTILE, NOTILE, NOTILE};
if (!lines[6 + i].empty())
{
std::istringstream factoryStream(lines[6 + i]);
for (int j = 0; j < FACTORY_SIZE; ++j)
{
if (factoryStream.get(c))
{
if (c == NOTILE || selectableTile(c))
{
if (selectableTile(c)) ++numberOfEachTile[getTileIndex(c)];
tiles[j] = charToTileType(c);
}
else
{
delete gameEngine;
cleanUpFactories(factories);
throw "A factory contains an invalid tile";
}
}
else
{
delete gameEngine;
cleanUpFactories(factories);
throw "A factory is missing some tiles";
}
}
}
if (isValidFactory(tiles))
factories[i] = new Factory(tiles);
else
{
delete gameEngine;
cleanUpFactories(factories);
throw "A factory is invalid";
}
}
gameEngine->fillFactories(factories);
// Create player 1 mosaic
Mosaic* player1mosaic = nullptr;
try
{
player1mosaic = generateMosiac(lines, 11, addedFirstTile, numberOfEachTile);
}
catch (const char* errorMessage)
{
delete gameEngine;
throw errorMessage;
}
gameEngine->addPlayer(player1Name, player1Score, player1mosaic, player1Ai);
// Create player 2 mosaic
Mosaic* player2mosaic = nullptr;
try
{
player2mosaic = generateMosiac(lines, 18, addedFirstTile, numberOfEachTile);
}
catch (const char* errorMessage)
{
delete gameEngine;
throw errorMessage;
}
gameEngine->addPlayer(player2Name, player2Score, player2mosaic, player2Ai);
// Create lid
TileList* lid = new TileList();
std::istringstream lidStream(lines[25]);
while (lidStream.get(c))
{
if (!selectableTile(c))
{
delete gameEngine;
delete lid;
throw "The lid contains an unselectable tile";
}
else
{
++numberOfEachTile[getTileIndex(c)];
lid->addBack(charToTileType(c));
}
}
gameEngine->fillLid(lid);
// Create bag
TileList* bag = new TileList();
std::istringstream bagStream(lines[26]);
while (bagStream.get(c))
{
if (!selectableTile(c))
{
delete gameEngine;
delete bag;
throw "The bag contains an unselectable tile";
}
else
{
++numberOfEachTile[getTileIndex(c)];
bag->addBack(charToTileType(c));
}
}
gameEngine->fillBag(bag);
if (!addedFirstTile)
{
delete gameEngine;
throw "Missing first player tile";
}
int sumOfTiles = calculateSumOfTiles(numberOfEachTile) + (addedFirstTile ? 1 : 0);
if (sumOfTiles < 101)
{
delete gameEngine;
throw "Incorrect number of tiles, not enough tiles in file";
}
else if (sumOfTiles > 101)
{
delete gameEngine;
throw "Incorrect number of tiles, too many tiles in file";
}
if (player1Turn) gameEngine->setPlayerTurn(0);
else gameEngine->setPlayerTurn(1);
return gameEngine;
}
void Saver::outputWall(std::ofstream& outputStream, Mosaic* mosaic)
{
for (int row = 0; row < NUMBER_OF_LINES; ++row)
{
for (int col = 0; col < NUMBER_OF_LINES; ++col)
{
if (mosaic->isFilled(row, col))
outputStream << char(Master_Wall[row][col]);
else
outputStream << tileTypeToLower(Master_Wall[row][col]);
}
}
outputStream << std::endl;
}
Mosaic* Saver::generateMosiac(std::string lines[SAVE_FILE_LINES_LENGTH], int startingLine, bool& addedFirstTile, int numberOfEachTile[])
{
Mosaic* mosaic = new Mosaic();
std::istringstream wallStream(lines[startingLine + 6]);
for (int row = 0; row < NUMBER_OF_LINES; ++row)
{
for (int col = 0; col < NUMBER_OF_LINES; ++col)
{
char c = '\0';
if (wallStream.get(c))
{
if (selectableTile(std::toupper(c)))
{
if (std::isupper(c)) ++numberOfEachTile[getTileIndex(c)];
mosaic->setFilled(row, col, std::isupper(c));
}
else
{
delete mosaic;
throw "A mosaic wall contains an invalid tile";
}
}
else
{
delete mosaic;
throw "A mosaic wall is missing tiles";
}
}
}
for (int i = 0; i < NUMBER_OF_LINES; ++i)
{
std::istringstream lineStream(lines[startingLine + i]);
char c = '\0';
for (int j = 0; j < i+1; ++j)
{
lineStream >> c;
TileType toAdd = charToTileType(c);
if (c == NOTILE || (selectableTile(c) && mosaic->getLine(i)->canAddTiles(toAdd) && !mosaic->isFilled(i, toAdd)))
{
if (c != NOTILE) ++numberOfEachTile[getTileIndex(c)];
mosaic->insertTilesIntoLine(i, 1, toAdd);
}
else
{
delete mosaic;
throw "A mosaic contains an invalid line";
}
}
}
std::istringstream brokenTilesStream(lines[startingLine + 5]);
char c = '\0';
while (brokenTilesStream.get(c))
{
if (selectableTile(c) || (c == FIRSTPLAYER && !addedFirstTile))
{
if (selectableTile(c)) ++numberOfEachTile[getTileIndex(c)];
addedFirstTile |= c == FIRSTPLAYER;
mosaic->addToBrokenTiles(1, charToTileType(c));
}
else
{
delete mosaic;
if (addedFirstTile) throw "Duplicate first player tile found in a mosaic";
throw "There is a problem in the broken tiles";
}
}
return mosaic;
}
void Saver::cleanUpFactories(Factory* factories[])
{
for (int i = 0; i < NUM_FACTORIES; ++i)
if (factories[i] != nullptr) delete factories[i];
}
bool Saver::isValidFactory(TileType tiles[])
{
bool allNotile = true;
bool allSelectable = true;
for (int i = 0; i < FACTORY_SIZE; ++i)
{
allNotile &= tiles[i] == NOTILE;
allSelectable &= selectableTile(tiles[i]);
}
return allNotile || allSelectable;
}
int Saver::getTileIndex(char c)
{
int index = 0;
if (c == RED) index = 0;
else if (c == YELLOW) index = 1;
else if (c == DARKBLUE) index = 2;
else if (c == LIGTHBLUE) index = 3;
else if (c == BLACK) index = 4;
return index;
}
int Saver::calculateSumOfTiles(int numberOfEachTile[])
{
int total = 0;
for (int i = 0; i < NUMBER_OF_COLOURS; ++i)
total += numberOfEachTile[i];
return total;
}