-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandProcessing.cpp
More file actions
470 lines (382 loc) · 14 KB
/
CommandProcessing.cpp
File metadata and controls
470 lines (382 loc) · 14 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
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
#include "CommandProcessing.h"
#include "GameEngine.h"
#include <iostream>
using std:: endl;
using std:: cout;
using std:: cin;
#include <fstream>
#include <string>
using std:: ifstream;
// COMMAND CLASS SECTION
Command:: Command(){
commandName = "";
effect = "";
}
Command:: Command(string commandName){
this->commandName = commandName;
}
Command:: Command(string commandName, string effect){
this->commandName = commandName;
this->effect = effect;
}
Command:: Command(const Command& copy){
this->commandName = copy.commandName;
this->effect = copy.effect;
}
string Command:: getCommandName(){
return commandName;
}
string Command:: getCommandEffect(){
return effect;
}
void Command:: setCommandName(string name){
this->commandName = name;
}
void Command:: setCommandEffect(string effect){
this->effect = effect;
}
ostream& operator <<(ostream& output, Command& command){
return output<<command.commandName<<endl;
}
Command& Command:: operator=(const Command& rhs){
this->commandName = rhs.commandName;
this->effect = rhs.effect;
return *this;
}
// The string effect is the command name that is passed to this function as a parameter
// This command name (string effect) is then set to be the effect of this command
void Command:: saveEffect(string effect){
this->setCommandEffect(getFirstWord(effect));
Notify(this);
}
string Command::stringToLog() {
return "Command's Effect: ...";
}
Command:: ~Command(){}
// COMMAND PROCESSOR CLASS SECTION
CommandProcessor:: CommandProcessor(){
commandList = vector <Command*> ();
}
CommandProcessor:: CommandProcessor(const CommandProcessor& copy){
commandList = vector <Command*> ();
for(auto it : copy.commandList){
commandList.push_back(new Command (*it));
}
}
CommandProcessor& CommandProcessor:: operator = (const CommandProcessor& rhs){
commandList = vector <Command*> ();
for(auto it : rhs.commandList){
commandList.push_back(new Command (*it));
}
return *this;
}
ostream& operator <<(ostream& output, CommandProcessor& command){
return output<<"Command Processor"<<endl;
}
// This saves the command object which is passed in the vector list of command objects
// which the commandprocessor object possesses
void CommandProcessor::saveCommand(Command* command) {
//cout<<"Saving command \" "<<command->getCommandName()<<" \"
commandList.push_back(command);
cout<<"\nCommand list: \n";
int lineNumber = 1;
for (auto it: commandList){
cout<<lineNumber<<": "<<*it;
++lineNumber;
}
cout<<"\n";
Notify(this);
}
// This function allows the command processor object to read user input
// This function then saves the command by first checking if it's valid or not
// Then, it saves the effect
Command* CommandProcessor:: readCommand(){
string command;
cout<<"\n*** Reading Command ***\nEnter your command: ";
getline(cin, command);
Command* newCommand = new Command(command);
// Start tournament mode
if (!GameEngine::instance()->hasGameStarted() && getFirstWord(command) == "tournament") {
startTournamentMode(command);
return NULL;
}
// If command is invalid, an error message will be saved as its effect
if (!validate(command)) {
cout<<"Command not valid\n";
newCommand->saveEffect("error");
cout<<newCommand->getCommandEffect();
return NULL;
}
cout<<"Command is valid, command saved\n";
newCommand->saveEffect(command);
saveCommand(newCommand);
return newCommand;
}
// This function calls the readCommand function
Command* CommandProcessor:: getCommand(){
Command* newCommand = readCommand();
return newCommand;
}
// This validates the command string passed which is the name of the command object
bool CommandProcessor:: validate(string command){
State *nextState = GameEngine::instance()->getGameLoop()->getCurrentState()->executeCommand(getFirstWord(command));
return (nextState != NULL);
}
void CommandProcessor::startTournamentMode(string command) {
// Get all parameters as strings
string maps = copyInBetweenAngleBrackets("-M ", command);
string players = copyInBetweenAngleBrackets("-P ", command);
string gamesString = copyInBetweenAngleBrackets("-G ", command);
string turnsString = copyInBetweenAngleBrackets("-D ", command);
// If some parameters are absent, print error message
if (maps == "" || players == "" || gamesString == "" || turnsString == "") {
cerr << "Please write the tournament settings in the following format:" << endl;
cerr << "tournament -M <listofmapfiles> -P <listofplayerstrategies> -G <numberofgames> -D <maxnumberofturns>" << endl;
return;
}
// Get maps as a 5-array of strings
string* mapArray = splitByCommas(maps, 5);
if (mapArray == NULL) {
cerr << "Too many maps. Please enter 5 maps at most." << endl;
return;
}
// Get player strategies as a 4-array of strings
string* playerStringArray = splitByCommas(players, 4);
if (playerStringArray == NULL || playerStringArray[1] == "") {
cerr << "Invalid number of players. Please enter between 2 and 4 players." << endl;
delete[] mapArray;
return;
}
// Make player strategies a 4-array of Player objects, return an error if the string is not the name of a strategy
Player** playerArray = new Player*[4]();
string playerNames[4] = { "Afaf", "Ally", "Parker", "Victoria" };
for (int i = 0; i < 4; i++) {
if (playerStringArray[i] == "") {
break;
}
else if (playerStringArray[i] == "Aggressive") { // Make aggressive player
Player* player = new Player(playerNames[i]);
player->setStrategy(new AggressivePlayerStrategy(player));
player->getStrategy()->setName("Aggressive");
playerArray[i] = player;
}
else if (playerStringArray[i] == "Benevolent") { // Make benevolent player
Player* player = new Player(playerNames[i]);
player->setStrategy(new BenevolentPlayerStrategy(player));
playerArray[i] = player;
player->getStrategy()->setName("Benevolent");
}
else if (playerStringArray[i] == "Neutral") { // Make netral player
Player* player = new Player(playerNames[i]);
player->setStrategy(new NeutralPlayerStrategy(player));
playerArray[i] = player;
player->getStrategy()->setName("Neutral");
}
else if (playerStringArray[i] == "Cheater") { // Make cheater player
Player* player = new Player(playerNames[i]);
player->setStrategy(new CheaterPlayerStrategy(player));
playerArray[i] = player;
player->getStrategy()->setName("Cheater");
}
else {
cerr << "Please enter valid strategies in the -P parameter. The alid strategies are \"Aggressive\", \"Benevolent\", \"Neutral\" and \"Cheater\"" << endl;
delete[] mapArray;
delete[] playerStringArray;
delete[] playerArray;
return;
}
}
delete[] playerStringArray;
// Check if number of games valid
int games;
try {
games = stoi(gamesString);
}
catch (exception e) {
cerr << "Please enter a valid integer for the number of games." << endl;
delete[] mapArray;
delete[] playerArray;
return;
}
if (games < 1 || games > 5) {
cerr << "Invalid number of games. Please enter an integer between 1 and 5 in the -G parameter." << endl;
delete[] mapArray;
delete[] playerArray;
return;
}
// Check if number of turns valid
int turns;
try {
turns = stoi(turnsString);
}
catch (exception e) {
cerr << "Please enter a valid integer for the number of turns." << endl;
delete[] mapArray;
delete[] playerArray;
return;
}
if (turns < 10 || turns > 50) {
cerr << "Invalid number of turns. Please enter an integer between 10 and 50 in the -G parameter." << endl;
delete[] mapArray;
delete[] playerArray;
return;
}
// Tournament command has been validated, now make the tournament
cout << "Command valid. Creating tournament" << endl;;
if (Tournament::newTournament(mapArray, playerArray, games, turns)) {
Tournament::instance()->runTournament();
}
return;
}
string CommandProcessor::stringToLog() {
return "Command: ...";
}
CommandProcessor:: ~CommandProcessor(){
for(auto it: commandList){
delete it;
it = nullptr;
}
}
// FILE LINE READER CLASS SECTION
FileLineReader:: FileLineReader(){}
FileLineReader:: FileLineReader(const FileLineReader& copy){}
FileLineReader& FileLineReader :: operator =(FileLineReader& rhs) {return *this;}
ostream& operator << (ostream& output, const FileLineReader& reader){
return cout<<"This is a file line reader object"<<endl;
}
// reads line from file
// This method is called by another function to read the lines form the file
vector <Command*> FileLineReader:: readLineFromFile(string fileName){
//static int count = 1;
string commandString;
vector <Command*> commandsFromFile;
ifstream inputFile;
inputFile.open(fileName);
if(!inputFile){
cout<<"\nError. Commands file does not exist."<<endl;
return commandsFromFile;
}
while(getline(inputFile, commandString)){
//cout<<"Reading command line "<<count<<" : "<<commandString<<"\n";
Command* lineCommand = new Command(commandString);
commandsFromFile.push_back(lineCommand);
// ++count;
}
return commandsFromFile;
}
FileLineReader:: ~FileLineReader(){}
// FILE COMMAND PROCESSOR ADAPTER CLASS SECTION
FileCommandProcessorAdapter:: FileCommandProcessorAdapter(){}
FileCommandProcessorAdapter::FileCommandProcessorAdapter(string theFileName) {
flr = new FileLineReader();
lineNumber = 0;
fileName = theFileName;
commandList = flr->readLineFromFile(theFileName);
}
FileCommandProcessorAdapter& FileCommandProcessorAdapter:: operator =(FileCommandProcessorAdapter& rhs){
this->flr = rhs.flr;
this->lineNumber = rhs.lineNumber;
this->fileName = rhs.fileName;
this->commandList = rhs.commandList;
return *this;
}
FileCommandProcessorAdapter:: FileCommandProcessorAdapter(const FileCommandProcessorAdapter& copy){
this->flr = copy.flr;
}
string FileCommandProcessorAdapter::getFileName() {
return fileName;
}
FileLineReader *FileCommandProcessorAdapter::getflr() {
return flr;
}
void FileCommandProcessorAdapter::setFileName(string theFileName) {
fileName = theFileName;
}
void FileCommandProcessorAdapter::setFlr(FileLineReader* theflr) {
flr = theflr;
}
Command* FileCommandProcessorAdapter::getCommandAt(int i) {
return commandList[i];
}
bool FileCommandProcessorAdapter::isEntireFileRead() {
return (lineNumber >= commandList.size());
}
ostream& operator <<(ostream& output, FileCommandProcessorAdapter& adapter){
return output<<"\nThis is a file command processor adapter object"<<endl;
}
// The readCommand is used to read and then save both the effect and command
// The command is saved in the vector list
Command* FileCommandProcessorAdapter:: readCommand(){
Command* newCommand = commandList[lineNumber];
string commandString = newCommand->getCommandName();
cout<<"\nLine "<<lineNumber + 1<<" : "<< commandString;
++lineNumber;
// Start tournament mode
if (!GameEngine::instance()->hasGameStarted() && getFirstWord(commandString) == "tournament") {
startTournamentMode(commandString);
return NULL;
}
if (!validate(commandString)) {
cout << "\nError. Command not valid.";
newCommand->saveEffect("error");
return NULL;
}
cout << ". Command is valid\n";
newCommand->saveEffect(commandString);
saveCommand(newCommand);
return newCommand;
}
FileCommandProcessorAdapter:: ~FileCommandProcessorAdapter(){
delete flr;
flr = nullptr;
}
// In here we are separating the command string to just take the keyword
// the game engine checks if the current state allows for this command to be executed
string getFirstWord(string command) {
string firstWord;
if (command.find(" ")) {
firstWord = command.substr(0, command.find(" "));
}
else {
firstWord = command;
}
return firstWord;
}
string copyInBetweenAngleBrackets(string prefix, string command) {
string result = "";
int i = command.find(prefix + "<");
if (i == string::npos) {
return result;
}
i += prefix.length() + 1;
char currentChar = command[i];
while (currentChar != '>' && i < command.length()) {
i++;
result += currentChar;
currentChar = command[i];
} if (i >= command.length()) {
return "";
}
return result;
}
string* splitByCommas(string input, int size) {
int position = 0, i = 0;
string tempInput = input;
string* splitValues = new string[5];
position = tempInput.find(", ");
while (i < size) {
if (position == string::npos) {
splitValues[i] = tempInput;
break;
}
splitValues[i] = tempInput.substr(0, position);
tempInput.erase(0, position + 2);
position = tempInput.find(", ");
i++;
}
if (i >= size) {
delete[] splitValues;
splitValues = NULL;
}
return splitValues;
}