forked from AA789-ai/WarzoneGame-COMP345
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandProcessing.cpp
More file actions
440 lines (367 loc) · 14.5 KB
/
CommandProcessing.cpp
File metadata and controls
440 lines (367 loc) · 14.5 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
#include "CommandProcessing.h"
#include <string>
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
using std::string;
using std::ifstream;
using std::vector;
// this function gets the first substring of a string split by whitespace
string getFirstSubstring(const string& input) {
size_t firstSpace = input.find(' ');
if(firstSpace != string::npos){
return input.substr(0, firstSpace);
}
return input;
}
// Implemenetation of Command class members
Command::Command() {
_command = new string();
_effect = new string();
}
// Non default constructor for Command class
Command::Command(string& command) {
_command = new string(command);
_effect = new string();
}
// Copy constructor for Command class
Command::Command(const Command& command){
_command = new string(*command._command);
_effect = new string(*command._effect);
}
// overloading the assignment operator for Command class
Command& Command::operator=(const Command& rhs){
if(this != &rhs){
delete _command;
delete _effect;
_command = new string(*rhs._command);
_effect = new string(*rhs._effect);
}
return *this;
}
// Destructor for Command class
Command::~Command() {
delete _command;
delete _effect;
}
// getter for the command field
string& Command::getCommand(){
return *_command;
}
// save the effect of the command
void Command::saveEffect(const string& effect){
*_effect = effect;
Notify(this); // log effect
}
// returns a formatted string representing the command effect (used for logging purposes)
string Command::stringToLog(){
string newCommandEffect = this->getCommandEffect();
return "Command's Effect: " + newCommandEffect;
};
string Command::stringToTourLog(int game){
return "";
};
// Method that returns the command effect
string& Command::getCommandEffect(){
return *_effect;
}
// Overriding the stream ingestion operator for Command class
std::ostream& operator <<(std::ostream& out, const Command& command) {
out << "Command: " << *command._command << ", Effect: " << *command._effect;
return out;
}
// Implementation of TournamentConfiguration class members
// Non default constructor for TournamentConfiguration class
TournamentConfiguration::TournamentConfiguration(int& numberOfGames, int& maxTurns, vector<string>& mapFiles, vector<string>& playerStrategies) {
_mapFiles = new vector<string>(mapFiles);
_playerStrategies = new vector<string>(playerStrategies);
_numberOfGames = new int(numberOfGames);
_maxTurns = new int(maxTurns);
}
// Copy constructor for TournamentConfiguration class
TournamentConfiguration::TournamentConfiguration(const TournamentConfiguration& tc) {
_mapFiles = new vector<string>(*(tc._mapFiles));
_playerStrategies = new vector<string>(*(tc._playerStrategies));
_numberOfGames = new int(*(tc._numberOfGames));
_maxTurns = new int(*(tc._maxTurns));
}
// Overriding the assignment operator for TournamentConfiguration class
TournamentConfiguration& TournamentConfiguration::operator=(const TournamentConfiguration& rhs) {
if (this != &rhs) {
delete _mapFiles;
delete _playerStrategies;
delete _numberOfGames;
delete _maxTurns;
_mapFiles = new vector<string>(*(rhs._mapFiles));
_playerStrategies = new vector<string>(*(rhs._playerStrategies));
_numberOfGames = new int(*(rhs._numberOfGames));
_maxTurns = new int(*(rhs._maxTurns));
}
return *this;
}
// Destructor for TournamentConfiguration class
TournamentConfiguration::~TournamentConfiguration() {
delete _mapFiles;
delete _playerStrategies;
delete _numberOfGames;
delete _maxTurns;
}
// Overriding the stream ingestion operator for the
ostream& operator<<(std::ostream& out, const TournamentConfiguration& tc) {
out << "Tournament Configuration Parameters: \n" << endl;
out << "\tNumber of Games: " << *tc._numberOfGames << endl;
out << "\tMax Turns: " << *tc._maxTurns << endl;
out << "\tMap Files: " << endl;
for (string mapFile : *tc._mapFiles) {
out << "\t\t" << mapFile << endl;
}
out << "\tPlayer Strategies: " << endl;
for (string playerStrategy : *tc._playerStrategies) {
out << "\t\t" << playerStrategy << endl;
}
return out << "\n";
}
// Method that validates and parses the configuration from the tournament command
TournamentConfiguration* TournamentConfiguration::validateAndParseCommand(string commandStr) {
// Find the positions of parameters in the command
size_t posM = commandStr.find("-M ");
if (posM == std::string::npos) return nullptr;
size_t posP = commandStr.find("-P ");
if (posP == std::string::npos) return nullptr;
size_t posG = commandStr.find("-G ");
if (posG == std::string::npos) return nullptr;
size_t posD = commandStr.find("-D ");
if (posD == std::string::npos) return nullptr;
// Get the lists of map files and player strategies
std::string mapFilesStr = commandStr.substr(posM + 3, posP - posM - 4);
std::string playerStrategiesStr = commandStr.substr(posP + 3, posG - posP - 4);
// Get the num of turns and games
int numberOfGames = std::stoi(commandStr.substr(posG + 2, posD - posG - 2));
int maxNumberOfTurns = std::stoi(commandStr.substr(posD + 2));
// Vectors to store map files and player strategies
std::vector<std::string> mapFiles;
std::vector<std::string> playerStrategies;
// split ,
std::stringstream mapFilesStream(mapFilesStr);
std::stringstream playerStrategiesStream(playerStrategiesStr);
std::string mapFile;
std::string playerStrategy;
// Get map files
while (std::getline(mapFilesStream, mapFile, ',')) {
mapFiles.push_back(mapFile);
}
// Get player strategies
while (std::getline(playerStrategiesStream, playerStrategy, ',')) {
playerStrategies.push_back(playerStrategy);
}
// validate num of games
if(numberOfGames < 1 || numberOfGames > 5){
cout << "Please enter 1-5 Games";
return nullptr;
}
// validate num of turns
if(maxNumberOfTurns < 10 || maxNumberOfTurns > 50){
cout << "Please enter 10-50 Turns";
return nullptr;
}
// validate num of maps
if(mapFiles.size() < 1 || mapFiles.size() > 5){
cout << "Please enter 1-5 Maps";
return nullptr;
}
// validate num of players
if(playerStrategies.size() < 2 || playerStrategies.size() > 4){
cout << "Please enter 2-4 Players";
return nullptr;
}
// validate strategies
for(int i = 0; i < playerStrategies.size(); i++){
string player = "player" + std::to_string(i);
std::string strategy = playerStrategies[i];
if(strategy != "Aggressive" && strategy != "Benevolent" && strategy != "Neutral" && strategy != "Cheater") {
cout << strategy << " is not a valid Computer";
return nullptr;
}
}
// return params for game
return new TournamentConfiguration(numberOfGames, maxNumberOfTurns, mapFiles, playerStrategies);
}
int &TournamentConfiguration::getNumberOfGames() {
return *_numberOfGames;
}
int &TournamentConfiguration::getMaxTurns() {
return *_maxTurns;
}
vector<string> &TournamentConfiguration::getMapsFiles() {
return *_mapFiles;
}
vector<string> &TournamentConfiguration::getPlayerStrategies() {
return *_playerStrategies;
}
// Implementation of CommandProcessor members
// Default constructor for CommandProcessor class
CommandProcessor::CommandProcessor(){
_commands = new std::vector<Command*>;
}
// Copy constructor for CommandProcessor class
CommandProcessor::CommandProcessor(const CommandProcessor& cp){
_commands = new std::vector<Command*>;
for(Command* command: *cp._commands) {
Command* newCommand = new Command(*command);
_commands->push_back(newCommand);
}
}
// Assignment operator for CommandProcessor class
CommandProcessor& CommandProcessor::operator=(const CommandProcessor& other) {
if (this != &other) {
for (Command* command : *_commands) {
delete command;
}
delete _commands;
_commands = new std::vector<Command*>;
for (Command* command : *other._commands) {
Command* newCommand = new Command(*command);
_commands->push_back(newCommand);
}
}
return *this;
}
// Destructor for CommandProcessor class
CommandProcessor::~CommandProcessor() {
for (Command* command : *_commands) {
delete command;
}
delete _commands;
}
// Method that reads the next console input from the user
string& CommandProcessor::readCommand(){
std::cout << "\n* Please enter a command: ";
string command;
std::getline(std::cin, command);
return *(new string(command));
}
// Method that adds a new command to the vector of command in the command processor
Command& CommandProcessor::saveCommand(string& command) {
// Create a new command and add it to the vector of commands
Command* newCommand = new Command(command);
_commands->push_back(newCommand);
// for logging purposes
Notify(this);
// return the newly created command
return *newCommand;
}
// Method that reads the next input from the user and saves it in the vector of commands and returns it
Command& CommandProcessor::getCommand() {
string commandValue = readCommand();
return saveCommand(commandValue);
}
// Method that validates the command based on the current game state
bool CommandProcessor::validate(Command& command, GameStateType gameState) {
string commandValue = getFirstSubstring(command.getCommand());
// if the game state is one of the game triggered events, consider the command valid
if(gameState == GameStateType::ASSIGN_REINFORCEMENT ||
gameState == GameStateType::ISSUE_ORDERS ||
gameState == GameStateType::EXECUTE_ORDERS) return true;
// determining if the command is valid based on the game state
if(gameState == GameStateType::START){
if(commandValue == "loadmap" || commandValue == "tournament") {
return true;
}
return false;
}
else if(gameState == GameStateType::MAP_LOADED){
if(commandValue == "loadmap" || commandValue == "validatemap") {
return true;
}
return false;
}
else if(gameState == GameStateType::MAP_VALIDATED){
if(commandValue == "addplayer") {
return true;
}
return false;
}
else if(gameState == GameStateType::PLAYERS_ADDED){
if(commandValue == "addplayer" || commandValue == "gamestart") {
return true;
}
return false;
}
else if(gameState == GameStateType::WIN){
if(commandValue == "replay" || commandValue == "quit") {
return true;
}
return false;
}
return false;
}
// Method that returns a formatted string representing the last command saved in the command processor (used for logging purposes)
string CommandProcessor::stringToLog(){
string newCommand = (_commands->back())->getCommand();
return "Command: " + newCommand;
};
// method for tournament logs
string CommandProcessor::stringToTourLog(int game){
return "";
};
// Overriding the stream ingestion operator for CommandProcessor class
std::ostream& operator<<(std::ostream& os, const CommandProcessor& commandProcessor) {
os << "Commands in the the command processor: \n" << std::endl;
for (Command* command : *commandProcessor._commands) {
os << "\t" << *command << std::endl;
}
return os << "\n";
}
// Implemenation of the FileLineReader and FileCommandProcessorAdapter
// Default constructor for FileLineReader class
FileLineReader::FileLineReader() : _fileStream(nullptr) {}
// Non default constructor for FileLineReader class
FileLineReader::FileLineReader(const string& filePath) : _fileStream(new ifstream(filePath)) {}
// Destructor for FileLineReader class
FileLineReader::~FileLineReader() {
if (_fileStream) {
_fileStream->close();
delete _fileStream;
}
}
// Overriding the stream ingestion operator for FileLineReader class
ostream& operator<<(std::ostream& out, const FileLineReader &flr) {
out << "FileLineReader";
return out;
}
// Method that reads the next line from the file and returns it, if the end of file is reached, it returns "EOD"
string& FileLineReader::readLineFromFile() {
string line;
if (_fileStream && _fileStream->is_open() && std::getline(*_fileStream, line)) {
return *(new string(line));
}
return *(new string("EOD"));
}
// Default constructor for FileCommandProcessorAdapter class
FileCommandProcessorAdapter::FileCommandProcessorAdapter() : _fileLineReader(nullptr) {}
// Non default constructor for FileCommandProcessorAdapter class
FileCommandProcessorAdapter::FileCommandProcessorAdapter(const string& filePath) : _fileLineReader(new FileLineReader(filePath)) {}
// Copy constructor for FileCommandProcessorAdapter class
FileCommandProcessorAdapter::FileCommandProcessorAdapter(const FileCommandProcessorAdapter& fcp) : _fileLineReader(fcp._fileLineReader ? new FileLineReader(*(fcp._fileLineReader)) : nullptr) {}
// Destructor for FileCommandProcessorAdapter class
FileCommandProcessorAdapter::~FileCommandProcessorAdapter() {
delete _fileLineReader;
}
// Overriding the assignment operator for FileCommandProcessorAdapter class
FileCommandProcessorAdapter& FileCommandProcessorAdapter::operator=(const FileCommandProcessorAdapter &rhs) {
if (this != &rhs) {
delete _fileLineReader;
_fileLineReader = rhs._fileLineReader ? new FileLineReader(*(rhs._fileLineReader)) : nullptr;
}
return *this;
}
// Overriding the stream ingestion operator for FileCommandProcessorAdapter class
std::ostream& operator<<(std::ostream& out, const FileCommandProcessorAdapter &fcp) {
out << "FileCommandProcessorAdapter";
return out;
}
// Method that reads the next line from the file and returns it, if the end of file is reached, it returns "EOD"
string& FileCommandProcessorAdapter::readCommand() {
return _fileLineReader ? _fileLineReader->readLineFromFile() : *(new std::string("EOD"));
}