Repository navigation
Expand file tree
/
Copy pathloadGame.cpp
More file actions
323 lines (266 loc) · 10.1 KB
/
Copy pathloadGame.cpp
File metadata and controls
323 lines (266 loc) · 10.1 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
#include "loadGame.h"
#include <iostream>
#include "mario.h"
#include <stdexcept>
#include <sstream>
#include <chrono>
#include <random>
#include "ghost.h"
#include "barrels.h"
#include <cstdlib> // For srand() and rand()
#include <ctime> // For time()
// Constructor
loadGame::loadGame(bool _silentMode)
: baseGame(), silentMode(_silentMode), seed(0) {
baseGame::silentMode = _silentMode;
}
// Destructor
loadGame::~loadGame() {
closeFiles();
}
// Run the load game mode
void loadGame::run() {
loadScreenFiles(); // Load all valid screen files
checkScreenFiles(); // Check for valid screen files
for (size_t i = 0; i < screenFiles.size(); ++i) {
std::cout << "Loading screen: " << screenFiles[i] << std::endl;
Sleep(1000); // Sleep for 1 second
if (!initializeScreen(screenFiles[i])) {
if (!silentMode) std::cerr << "Skipping invalid board: " << screenFiles[i] << std::endl;
continue;
}
// Handle playing in silent or normal mode
handleLoadPlaying();
}
if (silentMode) {
std::cout << "Test completed. Silent mode validation finished.\n";
}
else {
std::cout << "All boards completed.\n";
}
}
// Update the game state for load mode
void loadGame::updateGame()
{
if (heartCollected)
{
heartCollected = false;
recordResult(iteration, status::HeartCollected);
}
if(!isSilentMode()) marioCharacter->draw();
addScore(10); // Increment score for surviving
iteration++;
updateBarrels();
updateGhosts();
if(!isSilentMode()) gameboard.printLegendInfo(marioCharacter->getLife(), score, getmarioHasHammer());
if (gameboard.getChar(marioCharacter->getX(), marioCharacter->getY()) == utilities::PAULINA) {
recordResult(iteration, status::Win); // Record the stage completion status
return;
}
if (!silentMode) {
Sleep(70);
marioCharacter->erase();
}
marioCharacter->move();
marioCharacter->resetPressP();
}
// Initialize the screen and open the necessary files
bool loadGame::initializeScreen(const std::string& screen) {
closeFiles(); // Close any open files
gameboard.loadBoardFromFile(screen); // Load the board from the file
if (!gameboard.validateBoard()) {
return false;
}
stepsFileName = screen.substr(0, screen.find_last_of('.')) + ".steps";
resultsFileName = screen.substr(0, screen.find_last_of('.')) + ".result";
stepFile.open(stepsFileName);
resultFile.open(resultsFileName);
if (!stepFile.is_open() || !resultFile.is_open()) {
throw std::runtime_error("Could not open required files."); // Throw an exception if files are not open
}
loadSeedFromFile();
loadResultsFromFile();
setGame();
return true;
}
// Load the seed from the .steps file
void loadGame::loadSeedFromFile() {
std::string line;
// Read the first two lines
for (int i = 0; i < 2; ++i) {
if (!std::getline(stepFile, line)) {
throw std::runtime_error("Error: Missing necessary information in the .steps file.");
}
if (line.rfind("SEED", 0) == 0) {
seed = std::stol(line.substr(5));
std::srand(seed); // Initialize random generator with this seed
}
else if (line.rfind("SCORE", 0) == 0) {
std::istringstream stateStream(line);
std::string dummy;
int loadedScore, loadedLives, colorMode;
stateStream >> dummy >> loadedScore >> dummy >> loadedLives >> dummy >> colorMode;
score = loadedScore; // Set the score
marioCharacter->setLife(loadedLives); // Set Mario's lives
if (colorMode) utilities::colorSwitch = true; // Set the color mode
}
}
}
// Load expected results from .results file
void loadGame::loadResultsFromFile() {
expectedResults.clear();
int iteration;
std::string statusStr;
while (resultFile >> iteration >> statusStr) {
status parsedStatus = parseStatus(statusStr);
addResult(iteration, parsedStatus);
}
if (expectedResults.empty()) {
std::cerr << "Warning: No valid results loaded from file.\n";
}
}
// Validate results during silent mode
void loadGame::validateResults() {
bool testPassed = true;
if (results.size() != expectedResults.size()) {
std::cerr << "Error: Mismatch in result sizes. Expected: " << expectedResults.size()
<< ", Got: " << results.size() << "\n";
testPassed = false;
}
if(expectedResults.size() > results.size()) {
std::cout << "Warning: Fewer results than expected. Expected: " << expectedResults.size()
<< ", Got: " << results.size() << "\n";
std::cout << "Skipping test" << std::endl;
return;
}
// Iterate and compare both vectors
for (size_t i = 0; i < expectedResults.size(); ++i) {
// Check for size mismatch first
const auto& [expectedIteration, expectedStatus] = expectedResults[i];
const auto& [actualIteration, actualStatus] = results[i];
// Ensure there is a corresponding result at the same position
// Check iteration match
if (expectedIteration != actualIteration) {
std::cerr << "Error: Iteration mismatch. Expected: " << expectedIteration
<< ", Got: " << actualIteration << "\n";
testPassed = false;
}
// Check status match
if (expectedStatus != actualStatus) {
std::cerr << "Error: Status mismatch at iteration " << actualIteration
<< ". Expected: " << statusToString(expectedStatus)
<< ", Got: " << statusToString(actualStatus) << "\n";
testPassed = false;
}
}
if (testPassed) {
std::cout << "All results match expected outcomes.\n";
}
}
// Convert status enum to string
std::string loadGame::statusToString(baseGame::status s) {
switch (s) {
case baseGame::status::GameOver: return "GameOver";
case baseGame::status::Win: return "Win";
case baseGame::status::HeartCollected: return "HeartCollected";
case baseGame::status::BarrelHit: return "BarrelHit";
case baseGame::status::GhostHit: return "GhostHit";
case baseGame::status::ExplosionHit: return "ExplosionHit";
case baseGame::status::FallDamage: return "FallDamage";
case baseGame::status::HammerHit: return "HammerHit";
default: return "Unknown";
}
}
// Close all open files
void loadGame::closeFiles()
{
if (stepFile.is_open()) stepFile.close();
if (resultFile.is_open()) resultFile.close();
}
// Set the game state for load mode
void loadGame::setGame() {
if (!silentMode) baseGame::setGame();
else {
system("cls"); // Clear the screen
results.clear(); // Clear results from previous games
ghosts.clear(); // Clear all ghosts
initializeGhosts(); // Initialize ghosts for the current stage
barrelsList.clear(); // Clear all barrels
iteration = 0; //Reset the game loop
gameboard.resetBoard(); // Reset the board
int marioStartX, marioStartY;
gameboard.getStartPos(marioStartX, marioStartY); // Get Mario's starting position
marioCharacter->setBoard(gameboard); // Set the board for Mario
marioCharacter->resetPosition(marioStartX, marioStartY); // Reset Mario's position
setHammerPos(); // Set the hammer position
if (marioHasHammer) {
marioCharacter->marioWithHammer();
gameboard.setChar(hammerX, hammerY, utilities::SPACE);
useHammer = false;
hammerX = hammerY = 0;
}
for (auto it = ghosts.begin(); it != ghosts.end(); ++it) {
(*it)->resetPosition((*it)->getX(), (*it)->getY()); // Reset each ghost's position
}
}
}
// Handle playing for the current board
void loadGame::handleLoadPlaying() {
int stepIteration;
char key;
int lastIteration = expectedResults.back().first; // Get the last iteration from the expected results
while (!stepFile.eof()) {
// Read the next step from the file
stepFile >> stepIteration >> key;
// Check if the iteration matches the expected iteration
while (iteration < stepIteration) {
updateGame();
}
if (iteration == stepIteration) {
marioCharacter->keyPressed(key);
updateGame();
}
}
// Check if the game completed all steps
while (iteration < lastIteration) {
updateGame();
}
system("cls");
std::cout << "Game completed all steps and iterations.\n";
if(silentMode) validateResults(); // Compare results in silent mode
Sleep(3000);
}
// Parse the status string to status enum
baseGame::status loadGame::parseStatus(const std::string& statusStr) {
if (statusStr == "GameOver") {
return baseGame::status::GameOver;
}
else if (statusStr == "Win") {
return baseGame::status::Win;
}
else if (statusStr == "HeartCollected") {
return baseGame::status::HeartCollected;
}
else if (statusStr == "BarrelHit") {
return baseGame::status::BarrelHit;
}
else if (statusStr == "GhostHit") {
return baseGame::status::GhostHit;
}
else if (statusStr == "ExplosionHit") {
return baseGame::status::ExplosionHit;
}
else if (statusStr == "FallDamage") {
return baseGame::status::FallDamage;
}
else if (statusStr == "HammerHit") {
return baseGame::status::HammerHit;
}
else {
return baseGame::status::UNKONWN; // Unknown status
}
}
// Add result to the expected results map
void loadGame::addResult(int iteration, status eventStatus) {
expectedResults.emplace_back(iteration, eventStatus);
}