-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandProcessing.cpp
500 lines (417 loc) · 14.5 KB
/
CommandProcessing.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//
// Created by Nicolo pt 2 on 2021-11-07.
//
#include "CommandProcessing.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
/**
* --- --- --- Command ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
**/
// constructors
Command::Command(): command(""), effect(""){
if(this->debug)
cout << "CommandProcessor constructor called (no params)" << endl;
}
Command::Command(string command): command(command) {
if(this->debug)
cout << "CommandProcessor constructor called (1 param)" << endl;
}
Command::Command(string command, string effect): command(command), effect(effect) {
if(this->debug)
cout << "CommandProcessor constructor called (2 params)" << endl;
}
// copy constructors
Command::Command(Command *const pc): command(pc->command), effect(pc->effect) {
if(this->debug)
cout << "Command copy constructor called (pointer)" << endl;
}
Command::Command(const Command &c): command(c.command), effect(c.effect) {
if(this->debug)
cout << "Command copy constructor called (object)" << endl;
}
// destructor
Command:: ~Command() = default;
//Saves the current effect
void Command::saveEffect(string e) {
effect = e;
Notify(this);
}
//Returns the current effect
string Command::getEffect() const {
return effect;
}
//Gets the command
string Command::getCommand() const {
return command;
}
// assignment operator overload
Command& Command::operator =(const Command &c) {
this->command = c.command;
this->effect = c.effect;
return *this;
}
// stream insertion operator overloads
ostream & operator << (ostream &out, const Command &c)
{
out << "Command: " << c.getCommand() << " Effect: " << c.getEffect() << endl;
return out;
}
// Logging function for the Command
string Command::stringToLog() {
string log = "Log :: Effect: " + getEffect();
return log;
}
/**
* --- --- --- CommandProcessor ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
**/
// constructors
CommandProcessor::CommandProcessor() {
if(this->debug)
cout << "CommandProcessor constructor called" << endl;
}
// copy constructors
CommandProcessor::CommandProcessor(CommandProcessor *const cp): commands(cp->commands) {
if(this->debug)
cout << "CommandProcessor copy constructor called (pointer)" << endl;
}
CommandProcessor::CommandProcessor(const CommandProcessor & c): commands(c.commands) {
if(this->debug)
cout << "CommandProcessor copy constructor called (object)" << endl;
}
// destructor
CommandProcessor:: ~CommandProcessor() = default;
/**
* Gets the command
* @param ge game engine gets passed in
* @return a command pointer
*/
Command* CommandProcessor::getCommand(GameEngine * ge) {
ge->printAvailableOptions();
if(this->debug)
cout << "calling getCommand from CommandProcessor" << endl;
// read command from command line
string userInput = readCommand();
// validate command
string response = validate(userInput, ge);
// check if response is valid
if(response == "valid") {
saveCommand(userInput);
// invalid response, save command effect
} else {
// response is the error received from validate function
saveCommand(userInput,response);
}
return commands.back();
}
//Reads the command and calls the getline function
string CommandProcessor::readCommand() {
string input;
cout << "Please choose an option:";
// get user input
getline(cin,input);
cout << input;
return input;
}
//saves the command and pushes to the commands vector
Command* CommandProcessor::saveCommand(string command, string effect) {
Command *c;
c = new Command(command);
currentCommand = c;
commands.push_back(c);
Notify(this);
for(Observer* observer : *_observers)
c->Attach(observer);
c->saveEffect(effect);
return c;
}
//validates the command based on the input andgame engine
string CommandProcessor::validate(string command, GameEngine *ge) {
string loadmap = "loadmap";
string addplayer = "addplayer";
string response;
if(command.rfind(loadmap,0) == 0) {
string delimiter = " ";
// check if map file was provided by comparing length of command against addplayer command
if(command.length() == loadmap.length()) {
response = "Command '" + command + "' does not specify mapfile!";
if(this->debug)
cout << response << endl;
return response;
} else if (!(ge->getState() == ST_START || ge->getState() == ST_MAP_LOADED)) {
response = "Cannot perform transition '" + command + "' from state '" + enum_state_str[ge->getState()] + "'!";
if(this->debug)
cout << response << endl;
return response;
} else {
string token = command.substr(8,command.find(delimiter));
if(token.length() == 0) {
response = "Command '" + command + "' does not specify mapfile!";
if(this->debug)
cout << response << endl;
return response;
}
}
} else if(command == "validatemap"){
if((ge->getState() != ST_MAP_LOADED)) {
response = "Cannot perform transition '" + command + "' from state '" + enum_state_str[ge->getState()] + "'!";
if(this->debug)
cout << response << endl;
return response;
}
} else if(command.rfind(addplayer,0) == 0) {
string delimiter = " ";
// check if player name was provided by comparing length of command against addplayer command
if(command.length() == addplayer.length()) {
response = "Command '" + command + "' does not specify player name!";
if(this->debug)
cout << response << endl;
return response;
} else if (!((ge->getState() == ST_MAP_VALIDATED || ge->getState() == ST_PLAYERS_ADDED))) {
response = "Cannot perform transition '" + command + "' from state '" + enum_state_str[ge->getState()] + "'!";
if(this->debug)
cout << response << endl;
return response;
} else {
string token = command.substr(10,command.find(delimiter));
if(token.length() == 0) {
response = "Command '" + command + "' does not specify player name!";
if(this->debug)
cout << response << endl;
return response;
}
}
} else if(command == "gamestart"){
if((ge->getState() != ST_PLAYERS_ADDED)) {
response = "Cannot perform transition '" + command + "' from state '" + enum_state_str[ge->getState()] + "'!";
if(this->debug)
cout << response << endl;
return response;
}
} else if(command == "quit"){
if((ge->getState() != ST_WIN)) {
response = "Cannot perform transition '" + command + "' from state '" + enum_state_str[ge->getState()] + "'!";
if(this->debug)
cout << response << endl;
return response;
}
} else if(command == "replay"){
if((ge->getState() != ST_WIN)) {
response = "Cannot perform transition '" + command + "' from state '" + enum_state_str[ge->getState()] + "'!";
if(this->debug)
cout << response << endl;
return response;
}
} else {
response = "'" + command + "' is an invalid transition!";
if(this->debug)
cout << response << endl;
return response;
}
return "valid";
}
// assignment operator overload
CommandProcessor& CommandProcessor::operator =(const CommandProcessor &cp) {
this->commands = cp.commands;
return *this;
}
// stream insertion operator overloads
ostream & operator << (ostream &out, const CommandProcessor &cp)
{
out << "CommandProcessor: " << endl;
for(Command *c : cp.commands){
out << "Command: " << *c << " " << endl;
}
return out;
}
// Logging function for the CommandProcessor
string CommandProcessor::stringToLog() {
string log = "Log :: Command: " + currentCommand->getCommand();
return log;
}
/**
* --- --- --- FileLineReader ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
**/
// constructor
FileLineReader::FileLineReader(string path): path(path), pos(0) {
this->load();
}
// copy constructors
FileLineReader::FileLineReader(FileLineReader *const fr): path(fr->path), pos(fr->pos) {
this->load();
if(this->debug)
cout << "FileLineReader copy constructor called (pointer)" << endl;
}
FileLineReader::FileLineReader(const FileLineReader & fr): path(fr.path), pos(fr.pos) {
this->load();
if(this->debug)
cout << "FileLineReader copy constructor called (object)" << endl;
}
//Default constructor
FileLineReader:: ~FileLineReader() = default;
//Loads the line that was passed in
void FileLineReader::load() {
string line;
ifstream file(path);
while(getline(file,line)) {
fileData.push_back(line);
}
}
//Gets the next position in the filelinereader
string FileLineReader::next() {
return fileData[pos++];
}
//checks if at the end of file
bool FileLineReader::isEof() {
return pos == (fileData.size() - 1);
}
// assignment operator overload
FileLineReader& FileLineReader::operator =(const FileLineReader &fr) {
this->path = fr.path;
this->pos = fr.pos;
return *this;
}
// stream insertion operator overloads
ostream & operator << (ostream &out, const FileLineReader &fr)
{
out << "FileLineReader: " << endl;
out << "Path:" << fr.path << ", Position: " << fr.pos << " " << endl;
return out;
}
/**
* --- --- --- FileCommandProcessorAdapter ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
**/
// constructors
FileCommandProcessorAdapter::FileCommandProcessorAdapter(FileLineReader flr): CommandProcessor(), flr(flr) {}
// copy constructors
FileCommandProcessorAdapter::FileCommandProcessorAdapter(FileCommandProcessorAdapter *const fcpa): CommandProcessor(fcpa), flr(fcpa->flr) {
if(this->debug)
cout << "FileCommandProcessorAdapter copy constructor called (pointer)" << endl;
}
// FileCommandProcessorAdapter is a child of base class CommandProcessor, so instance of fcpa can be passed to CommandProcessor() via its copy constructor
FileCommandProcessorAdapter::FileCommandProcessorAdapter(const FileCommandProcessorAdapter &fcpa): CommandProcessor(fcpa), flr(fcpa.flr) {
if(this->debug)
cout << "FileCommandProcessorAdapter copy constructor called (object)" << endl;
}
//gets the command based on the game engine
Command* FileCommandProcessorAdapter::getCommand(GameEngine * ge) {
Command* c;
string input = readCommand();
string response = validate(input, ge);
if(response == "valid") {
c = saveCommand(input);
} else {
c = saveCommand(input, response);
}
if(this->debug)
cout << *c << endl;
return c;
}
//reads the current command
string FileCommandProcessorAdapter::readCommand() {
// check if end of file reached
if(isEof()) {
return "Error: end of file reached!";
} else {
return flr.next();
}
}
//Checks if we are at end of file
bool FileCommandProcessorAdapter::isEof() {
return flr.isEof();
}
// assignment operator overload
FileCommandProcessorAdapter& FileCommandProcessorAdapter::operator =(const FileCommandProcessorAdapter &fcpa) {
this->flr = fcpa.flr;
return *this;
}
// stream insertion operator overloads
ostream & operator << (ostream &out, const FileCommandProcessorAdapter &fcpa)
{
out << "FileCommandProcessorAdapter: " << endl;
//out << fcpa.flr << endl;
return out;
}
bool FileCommandProcessorAdapter::hasCommand() {
return !flr.isEof();
}
// based on given tournament parameters, create a commands file which the file command processor can read from
string FileCommandWriter::writeTournamentFile(vector<string> maps, vector<string> players, int numGames) {
string gameFile = "../CommandProcessorFiles/commands_tournament.txt";
ofstream output;
output.open(gameFile,ios_base::trunc);
// loop over each map
for(int i = 0; i < maps.size(); i++) {
// loop over each game
for(int j = 0; j < numGames; j++) {
output << "loadmap " + maps[i] << endl;
output << "validatemap" << endl;
// loop over each player
for(int k = 0; k < players.size(); k++) {
output << "addplayer " + players[k] << endl;
}
output << "gamestart" << endl;
// check if last iteration
if(i == (maps.size() - 1) && j == (numGames - 1)) {
output << "quit";
} else {
output << "replay" << endl;
}
}
}
output.close();
return gameFile;
}
// parse comma seperated string of maps from valid tournament into vector of strings
vector<string> FileCommandWriter::parseMap(string input) {
// get substring between -M and -P
std:: regex reg ("\\-M (.*) -P");
return parseList(input, reg);
}
// parse comma seperated string of players from valid tournament into vector of strings
vector<string> FileCommandWriter::parsePlayer(string input) {
// get substring between -P and -G
std:: regex reg ("\\-P (.*) -G");
return parseList(input,reg);
}
// parse number of games string from valid tournament string into int
int FileCommandWriter::parseNumGames(string input) {
// get substring between -G and -D
std:: regex reg ("\\-G (.*) -D");
return parseInt(input,reg);
}
// parse max depth string from valid tournament string into int
int FileCommandWriter::parseMaxDepth(string input) {
// get substring after -D
std:: regex reg ("\\-D (.*)");
return parseInt(input,reg);
}
// given valid tournament string and regex expression, parse into int
int FileCommandWriter::parseInt(string input, regex re) {
smatch m;
regex_search(input,m, re);
// cast match to int
int num = stoi(m[1]);
return num;
}
// given valid tournament string and regex expression, parse into vector of strings
vector<string> FileCommandWriter::parseList(string input, regex re) {
vector<string> build;
smatch m;
regex_search(input,m, re);
std::stringstream ss(m[1]);
// get all comma seperated items and put into vector
for (string s; ss >> s;) {
if(s.back() == *",") {
s.pop_back();
build.push_back(s);
} else {
build.push_back(s);
}
if (ss.peek() == ',')
ss.ignore();
}
return build;
}