-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList2.h
More file actions
345 lines (285 loc) · 12.4 KB
/
LinkedList2.h
File metadata and controls
345 lines (285 loc) · 12.4 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
/*
GameSave.cpp
Author: [Your Name]
Roll Number: [Your Roll Number]
Project Title: Xonix Game - Data Structures and Algorithms Project
Description:
This file contains the implementation of the GameSave class methods.
*/
#include "GameSave.h"
#include <sstream>
#include <iomanip>
#include <ctime>
#include <cstdlib>
#include <direct.h> // For _mkdir
#include <sys/stat.h> // For stat
// Windows API includes - isolate to avoid conflicts
#define WIN32_LEAN_AND_MEAN
#include <windows.h> // For FindFirstFile, FindNextFile, FindClose
#undef min
#undef max
#undef BYTE
// Constructor
GameSave::GameSave() {
// Initialize with default saves directory
savesDirectory = "saves/";
// Create saves directory if it doesn't exist
struct stat info;
if (stat(savesDirectory.c_str(), &info) != 0) {
_mkdir(savesDirectory.c_str());
}
}
// Generate a unique save ID based on timestamp and random value
std::string GameSave::generateSaveID() {
// Get current time
auto now = std::time(nullptr);
struct tm timeInfo;
localtime_s(&timeInfo, &now);
auto tm = &timeInfo;
// Format as YYYYMMDD_HHMMSS
std::stringstream ss;
ss << std::put_time(tm, "%Y%m%d_%H%M%S_");
// Add random suffix to avoid collisions
ss << rand() % 1000;
return ss.str();
}
// Save the current game state
bool GameSave::saveGame(int grid[][40], int width, int height, const std::string& username, int score,
const Player& player, const Enemy enemies[], int enemyCount) {
// Generate a unique save ID
std::string saveID = generateSaveID();
// Create a SaveGameState object
gameState = SaveGameState(saveID, username, score, width, height);
// Clear and populate tiles list with current grid data
tiles.clear();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (grid[i][j] != 0) { // Only save non-empty tiles
tiles.addLast(TileNode(j, i, grid[i][j]));
}
}
}
// Create the full path for the save file
std::string filePath = savesDirectory + saveID + ".sav";
// Open file for writing in binary mode
std::ofstream file(filePath, std::ios::binary);
if (!file.is_open()) {
std::cout << "Error: Could not open file for writing: " << filePath << std::endl;
return false;
}
// Write game state metadata
file.write(saveID.c_str(), saveID.length() + 1); // +1 for null terminator
file.write(username.c_str(), username.length() + 1);
file.write(reinterpret_cast<const char*>(&score), sizeof(score));
file.write(reinterpret_cast<const char*>(&gameState.timestamp), sizeof(gameState.timestamp));
file.write(reinterpret_cast<const char*>(&width), sizeof(width));
file.write(reinterpret_cast<const char*>(&height), sizeof(height));
// Write player data
int playerX = player.getX();
int playerY = player.getY();
int playerDX = player.getDX();
int playerDY = player.getDY();
int playerScore = player.getScore();
int playerPowerups = player.getPowerups();
file.write(reinterpret_cast<const char*>(&playerX), sizeof(playerX));
file.write(reinterpret_cast<const char*>(&playerY), sizeof(playerY));
file.write(reinterpret_cast<const char*>(&playerDX), sizeof(playerDX));
file.write(reinterpret_cast<const char*>(&playerDY), sizeof(playerDY));
file.write(reinterpret_cast<const char*>(&playerScore), sizeof(playerScore));
file.write(reinterpret_cast<const char*>(&playerPowerups), sizeof(playerPowerups));
// Write enemy count and enemy data
file.write(reinterpret_cast<const char*>(&enemyCount), sizeof(enemyCount));
for (int i = 0; i < enemyCount; i++) {
int enemyX = enemies[i].getX();
int enemyY = enemies[i].getY();
bool isFrozen = enemies[i].isFrozen();
file.write(reinterpret_cast<const char*>(&enemyX), sizeof(enemyX));
file.write(reinterpret_cast<const char*>(&enemyY), sizeof(enemyY));
file.write(reinterpret_cast<const char*>(&isFrozen), sizeof(isFrozen));
}
// Write tile data
int tileCount = tiles.getSize();
file.write(reinterpret_cast<const char*>(&tileCount), sizeof(tileCount));
// Traverse the linked list and write each tile
Node<TileNode>* current = tiles.getHead();
while (current != nullptr) {
file.write(reinterpret_cast<const char*>(¤t->data.x), sizeof(current->data.x));
file.write(reinterpret_cast<const char*>(¤t->data.y), sizeof(current->data.y));
file.write(reinterpret_cast<const char*>(¤t->data.type), sizeof(current->data.type));
current = current->next;
}
file.close();
std::cout << "Game saved successfully! Save ID: " << saveID << std::endl;
return true;
}
// Load a saved game state
bool GameSave::loadGame(int grid[][40], int& width, int& height, std::string& username, int& score,
Player& player, Enemy enemies[], int& enemyCount, const std::string& saveID) {
// Create the full path for the save file
std::string filePath = savesDirectory + saveID + ".sav";
// Open file for reading in binary mode
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) {
std::cout << "Error: Could not open save file: " << filePath << std::endl;
return false;
}
// Read save ID (just to verify it matches)
char buffer[256];
file.getline(buffer, 256, '\0');
std::string readSaveID(buffer);
if (readSaveID != saveID) {
std::cout << "Error: Save ID mismatch. Expected: " << saveID << ", Got: " << readSaveID << std::endl;
file.close();
return false;
}
// Read username
file.getline(buffer, 256, '\0');
username = std::string(buffer);
// Read score, timestamp, width, height
file.read(reinterpret_cast<char*>(&score), sizeof(score));
file.read(reinterpret_cast<char*>(&gameState.timestamp), sizeof(gameState.timestamp));
file.read(reinterpret_cast<char*>(&width), sizeof(width));
file.read(reinterpret_cast<char*>(&height), sizeof(height));
// Read player data
int playerX, playerY, playerDX, playerDY, playerScore, playerPowerups;
file.read(reinterpret_cast<char*>(&playerX), sizeof(playerX));
file.read(reinterpret_cast<char*>(&playerY), sizeof(playerY));
file.read(reinterpret_cast<char*>(&playerDX), sizeof(playerDX));
file.read(reinterpret_cast<char*>(&playerDY), sizeof(playerDY));
file.read(reinterpret_cast<char*>(&playerScore), sizeof(playerScore));
file.read(reinterpret_cast<char*>(&playerPowerups), sizeof(playerPowerups));
// Update player object
player.setX(playerX);
player.setY(playerY);
player.setDirection(playerDX, playerDY);
// Set score and powerups (you may need to add these methods to the Player class)
// Read enemy count and enemy data
file.read(reinterpret_cast<char*>(&enemyCount), sizeof(enemyCount));
// Make sure enemy count doesn't exceed maximum
int maxEnemies = 10; // Should match Game::maxEnemies
if (enemyCount > maxEnemies) {
std::cout << "Warning: Saved enemy count exceeds maximum. Truncating." << std::endl;
enemyCount = maxEnemies;
}
// Initialize enemies array with loaded data
for (int i = 0; i < enemyCount; i++) {
int enemyX, enemyY;
bool isFrozen;
file.read(reinterpret_cast<char*>(&enemyX), sizeof(enemyX));
file.read(reinterpret_cast<char*>(&enemyY), sizeof(enemyY));
file.read(reinterpret_cast<char*>(&isFrozen), sizeof(isFrozen));
// Create a new enemy with the loaded data
// You may need to adjust this based on your Enemy class implementation
enemies[i] = Enemy(); // Create a new enemy
// Set enemy properties if your Enemy class has appropriate setters
// If Enemy doesn't have setters, you may need to modify Enemy class
// or use a different approach to restore enemy state
}
// Clear the grid first
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
grid[i][j] = 0;
}
}
// Read tile data
int tileCount;
file.read(reinterpret_cast<char*>(&tileCount), sizeof(tileCount));
// Read each tile and update grid
for (int i = 0; i < tileCount; i++) {
int tileX, tileY, tileType;
file.read(reinterpret_cast<char*>(&tileX), sizeof(tileX));
file.read(reinterpret_cast<char*>(&tileY), sizeof(tileY));
file.read(reinterpret_cast<char*>(&tileType), sizeof(tileType));
// Update the grid with the tile
if (tileX >= 0 && tileX < width && tileY >= 0 && tileY < height) {
grid[tileY][tileX] = tileType;
}
}
file.close();
std::cout << "Game loaded successfully! Save ID: " << saveID << std::endl;
return true;
}
// List all available saves for a player
LinkedList<std::string> GameSave::listSaves(const std::string& username) {
LinkedList<std::string> saveList;
// Create directories if they don't exist
struct stat info;
if (stat(savesDirectory.c_str(), &info) != 0) {
_mkdir(savesDirectory.c_str());
}
// Using Windows API to scan directory
WIN32_FIND_DATAA findFileData;
HANDLE hFind = FindFirstFileA((savesDirectory + "*.sav").c_str(), &findFileData);
if (hFind != INVALID_HANDLE_VALUE) {
do {
// Get the filename without the .sav extension
std::string filename = findFileData.cFileName;
std::string saveID = filename.substr(0, filename.length() - 4);
// Open the file to check if username matches
std::string filePath = savesDirectory + filename;
std::ifstream file(filePath, std::ios::binary);
if (file.is_open()) {
// Read save ID (just to verify)
char buffer[256];
file.getline(buffer, 256, '\0');
// Read username
file.getline(buffer, 256, '\0');
std::string fileUsername(buffer);
// If username matches, add to list
if (fileUsername == username) {
saveList.addLast(saveID);
std::cout << "Found save for " << username << ": " << saveID << std::endl;
}
file.close();
}
} while (FindNextFileA(hFind, &findFileData) != 0);
FindClose(hFind);
}
std::cout << "Found " << saveList.getSize() << " saves for player: " << username << std::endl;
return saveList;
}
// Delete a saved game
bool GameSave::deleteSave(const std::string& saveID) {
std::string filePath = savesDirectory + saveID + ".sav";
// Try to delete the file
if (remove(filePath.c_str()) != 0) {
std::cout << "Error: Could not delete save file: " << filePath << std::endl;
return false;
}
std::cout << "Save deleted successfully: " << saveID << std::endl;
return true;
}
// Get information about a save
SaveGameState GameSave::getSaveInfo(const std::string& saveID) {
std::string filePath = savesDirectory + saveID + ".sav";
// Open file for reading in binary mode
std::ifstream file(filePath, std::ios::binary);
if (!file.is_open()) {
std::cout << "Error: Could not open save file: " << filePath << std::endl;
return SaveGameState();
}
// Read save ID
char buffer[256];
file.getline(buffer, 256, '\0');
std::string readSaveID(buffer);
// Read username
file.getline(buffer, 256, '\0');
std::string username(buffer);
// Read score, timestamp, width, height
int score, width, height;
time_t timestamp;
file.read(reinterpret_cast<char*>(&score), sizeof(score));
file.read(reinterpret_cast<char*>(×tamp), sizeof(timestamp));
file.read(reinterpret_cast<char*>(&width), sizeof(width));
file.read(reinterpret_cast<char*>(&height), sizeof(height));
file.close();
// Create and return a SaveGameState object with the read data
SaveGameState state;
state.saveID = readSaveID;
state.playerUsername = username;
state.score = score;
state.timestamp = timestamp;
state.gridWidth = width;
state.gridHeight = height;
return state;
}