This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSaveManager.cpp
139 lines (120 loc) · 3.68 KB
/
SaveManager.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
#include "SaveManager.h"
bool SaveManager::DetectSaveFile()
{
std::ifstream fin("maze.txt");
if (fin.fail() == false)
fin.close();
return !fin.fail();
}
bool SaveManager::DetectSaveFile(const char* fileName)
{
std::ifstream fin(fileName);
if (fin.fail() == false)
fin.close();
return !fin.fail();
}
bool SaveManager::LoadPrompt()
{
char loadResponse = CHAR_MIN;
char overwriteResponse = CHAR_MIN;
std::vector<Cell*> arr;
do {
std::cout << "A save file has been detected! Load? (Y/N): ";
std::cin >> loadResponse;
// If they wish to load the save file, load here.
if (toupper(loadResponse) == 'Y')
{
CLEAR_CONSOLE;
arr = SaveManager::LoadSaveFile("Maze.txt");
Maze::PromptConsole(arr);
break;
}
// Otherwise, ask them if they are sure.
else
{
do {
std::cout << "The current save file will be overwritten, are you sure? (Y/N): ";
std::cin >> overwriteResponse;
} while (toupper(overwriteResponse) != 'Y' && toupper(overwriteResponse) != 'N');
// If they are sure, overwrite the save file. Otherwise, continue.
if (toupper(overwriteResponse) == 'Y')
{
break;
}
}
} while (toupper(loadResponse) != 'Y' && toupper(loadResponse) != 'N' || toupper(overwriteResponse) == 'N');
return false;
}
std::vector<Cell*> SaveManager::LoadSaveFile(const char* fileName)
{
std::ifstream file(fileName);
std::vector<Cell*> array;
if (file.is_open())
{
int row = 0;
for (std::string line; std::getline(file, line); )
{
std::cout << line << std::endl;
for (int column = 0; column < line.length(); column++)
{
array.push_back(new Cell(column, row, line[column]));
}
MAZE_COLUMNS = line.length();
row++;
}
MAZE_ROWS = row;
}
file.close();
return array;
}
bool SaveFile(const char* fileName, std::vector<Cell*>& cells)
{
char confirmationResponse = CHAR_MIN;
std::string name;
do
{
if (std::string(fileName).length() < 6)
{
name = "";
do
{
std::cout << "Please enter a valid file name: ";
std::getline(std::cin, name);
} while (name.length() < 0);
}
else
{
name = std::string(fileName).substr(5, sizeof(fileName));
}
std::cout << "This maze will be saved as \'" << name << ".txt\', are you sure? (Y/N): ";
std::cin >> confirmationResponse;
} while (toupper(confirmationResponse) != 'Y' && toupper(confirmationResponse) != 'N');
if (toupper(confirmationResponse) == 'N')
{
return false;
}
else
{
std::string outputName = name + ".txt";
std::ofstream file(outputName, std::ios::app);
if (file.is_open())
{
for (int row = 0; row < MAZE_ROWS; row++)
{
for (int column = 0; column < MAZE_COLUMNS; column++)
{
int index = (row * MAZE_COLUMNS) + column;
char value = cells.at(index)->GetValue();
const char* ptr = &value;
file.write(ptr, 1);
}
char value = '\n';
const char* ptr = &value;
file.write(ptr, 1);
}
}
file.close();
std::cout << "\'" << outputName << "\' saved successfully." << std::endl;
return true;
}
}