-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecorder.cpp
More file actions
228 lines (178 loc) · 7.04 KB
/
Copy pathRecorder.cpp
File metadata and controls
228 lines (178 loc) · 7.04 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
#include "Recorder.h"
#include <fstream>
#include <sstream>
////////////////////////////////////////// Action Conversion /////////////////////////////////////////////
inline Action stringToAction(const string& str) {
if (str == "MOVE_UP") return Action::MOVE_UP;
if (str == "MOVE_DOWN") return Action::MOVE_DOWN;
if (str == "MOVE_LEFT") return Action::MOVE_LEFT;
if (str == "MOVE_RIGHT") return Action::MOVE_RIGHT;
if (str == "STAY") return Action::STAY;
if (str == "DROP_ITEM") return Action::DROP_ITEM;
if (str == "ESC") return Action::ESC;
if (str == "ANSWER_RIDDLE") return Action::ANSWER_RIDDLE;
return Action::STAY;
}
inline string actionToString(Action action) {
switch (action) {
case Action::MOVE_UP: return "MOVE_UP";
case Action::MOVE_DOWN: return "MOVE_DOWN";
case Action::MOVE_LEFT: return "MOVE_LEFT";
case Action::MOVE_RIGHT: return "MOVE_RIGHT";
case Action::STAY: return "STAY";
case Action::DROP_ITEM: return "DROP_ITEM";
case Action::ANSWER_RIDDLE: return "ANSWER_RIDDLE";
case Action::ESC: return "ESC";
default: return "UNKNOWN";
}
}
////////////////////////////////////////// GameEvent::write /////////////////////////////////////////////
void GameEvent::write(std::ostream& out) const
{
switch (type) {
case GameEventType::SCREEN_CHANGE:
out << "SCREEN_CHANGE CYCLE: " << cycle << " ROOM: " << roomId << "\n";
break;
case GameEventType::LIFE_LOST:
out << "LIFE_LOST CYCLE: " << cycle << " ROOM: " << roomId
<< " PLAYER: " << playerId << "\n";
break;
case GameEventType::RIDDLE_ANSWERED:
out << "RIDDLE CYCLE: " << cycle << " ROOM: " << roomId
<< " QUESTION: \"" << question << "\""
<< " ANSWER: " << answerGiven
<< " CORRECT: " << (wasCorrect ? "YES" : "NO") << "\n";
break;
case GameEventType::QUIT:
out << "QUIT CYCLE: " << cycle << " ROOM: " << roomId << "\n";
break;
}
}
////////////////////////////////////////// GameEvent::read /////////////////////////////////////////////
bool GameEvent::read(std::istream& in)
{
std::string eventType;
std::string dummy;
if (!(in >> eventType)) return false;
if (!(in >> dummy >> cycle)) return false;
if (!(in >> dummy >> roomId)) return false;
if (eventType == "SCREEN_CHANGE") type = GameEventType::SCREEN_CHANGE;
else if (eventType == "LIFE_LOST")
{
type = GameEventType::LIFE_LOST;
if (!(in >> dummy >> playerId)) return false;
}
else if (eventType == "RIDDLE")
{
type = GameEventType::RIDDLE_ANSWERED;
if (!(in >> dummy)) return false;
char c;
while (in.get(c) && c != '"') {}
question.clear();
while (in.get(c) && c != '"') question += c;
if (!(in >> dummy >> answerGiven)) return false;
std::string correctStr;
if (!(in >> dummy >> correctStr)) return false;
wasCorrect = (correctStr == "YES");
}
else if (eventType == "QUIT") type = GameEventType::QUIT;
else return false; // Unknown event type
return true;
}
////////////////////////////////////////// ActionRecord::write /////////////////////////////////////////////
void ActionRecord::write(ostream &output) const
{
output << "CYCLE: " << cycle
<< " PLAYER: " << playerId
<< " ACTION: " << actionToString(action);
if (action == Action::ANSWER_RIDDLE) output << " ANSWER: " << answer;
output << "\n";
}
////////////////////////////////////////// ActionRecord::read /////////////////////////////////////////////
bool ActionRecord::read(istream &input)
{
string dummy;
string actionStr;
if (!(input >> dummy >> cycle)) return false;
if (!(input >> dummy >> playerId)) return false;
if (!(input >> dummy >> actionStr)) return false;
action = stringToAction(actionStr);
if (action == Action::ANSWER_RIDDLE) {
string answerLabel;
if (!(input >> answerLabel >> answer)) return false;
}
return true;
}
////////////////////////////////////////// RecordedSteps::loadFromFile /////////////////////////////////////////////
// Made with AI
ErrorCode RecordedSteps::loadFromFile(const string& filename)
{
ifstream file(filename);
if (!file.is_open()) return ErrorCode::FILE_NOT_FOUND;
actions.clear();
screenNames.clear();
currActionIndex = 0;
randomSeed = 0; // Default
colorMode = false; // Default per user request (legacy compatible)
// Check for header line
if (file.peek() != EOF) {
string line;
streampos oldPos = file.tellg();
getline(file, line);
stringstream ss(line);
string key;
// We'll read the line and parse for keywords
// Structure: RANDOM_SEED: <seed> [SCREENS: <list>] [COLOR_MODE: <ON/OFF>]
ss >> key;
if (key == "RANDOM_SEED:") {
ss >> randomSeed;
string temp;
while (ss >> temp) {
if (temp == "SCREENS:") {
string screensList;
ss >> screensList; // Assuming comma separated list follows immediately
stringstream ss2(screensList);
string segment;
while(std::getline(ss2, segment, ',')) {
screenNames.push_back(segment);
}
} else if (temp == "COLOR_MODE:") {
string modeStr;
ss >> modeStr;
colorMode = (modeStr == "ON");
}
}
} else {
// Not a seed line, rewind
file.seekg(oldPos);
}
}
while (file >> ws && file.peek() != EOF) {
ActionRecord record;
if (!record.read(file)) {
// Check if it's just garbage or end
// Actually, read handles structure.
// If read fails mid-stream, it's an error.
file.close();
return ErrorCode::READ_ERROR;
}
actions.push_back(record);
}
file.close();
return ErrorCode::NONE;
}
////////////////////////////////////////// RecordedSteps::getCurrentAction /////////////////////////////////////////////
const ActionRecord* RecordedSteps::getCurrentAction() const
{
if (currActionIndex >= actions.size()) return nullptr;
return &actions[currActionIndex];
}
////////////////////////////////////////// RecordedSteps::getActionsForCycle /////////////////////////////////////////////
vector<ActionRecord> RecordedSteps::getActionsForCycle(unsigned long curr) const
{
vector<ActionRecord> result;
for (const ActionRecord& record : actions) {
if (record.cycle == curr) result.push_back(record);
}
return result;
}