Repository navigation
Expand file tree
/
Copy pathCommandProcessing.cpp
More file actions
501 lines (385 loc) · 10.6 KB
/
Copy pathCommandProcessing.cpp
File metadata and controls
501 lines (385 loc) · 10.6 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
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
501
#include "CommandProcessing.h"
using namespace CommandProcessing;
//======================Command=======================
//Constructors
Command::Command()
{
commandname = new string("Empty");
//set effect as empty until validate method is called
commandeffect = new string("Empty");
}
//Parameters:
//name: The name to initialize the command with
Command::Command(string name)
{
//set name to passed parameter
commandname = new string(name);
//set effect as empty until validate method is called
commandeffect = new string("Empty");
}
//copy constructor
Command::Command(const Command& other)
{
this->commandname = new string(*(other.commandname));
this->commandeffect = new string(*(other.commandeffect));
}
//destructor
Command::~Command()
{
delete commandname;
delete commandeffect;
}
//Operators
//assignment operator
Command& Command::operator=(const Command& other)
{
if (this == &other)
{
return *this;
}
Command c(other);
return c;
}
//stream insertion operator
ostream& operator <<(ostream& o, Command& g)
{
o << g.getTitle();
return o;
}
//Function Definitions
//Parameters:
//effect: Effect of the command to be stored in the Command object
void Command::saveEffect(string effect)
{
//delete previous value, then store new one
delete commandeffect;
commandeffect = new string(effect);
//Sending command to log file
Subject::Notify("Command: Save Effect(" + effect + ")");
}
string Command::getEffect()
{
return *commandeffect;
}
string Command::getTitle()
{
return *commandname;
}
void Command::stringToLog(string s)
{
stringsToLog = s;
}
//==================CommandProcessor==================
//Constructors
CommandProcessor::CommandProcessor()
{
//initialize current game state to Start
currentgamestate = new string("start");
}
//copy constructor
CommandProcessor::CommandProcessor(const CommandProcessor& other)
{
//copy game state from other object
this->currentgamestate = new string(*(other.currentgamestate));
}
CommandProcessor::~CommandProcessor()
{
delete currentgamestate;
}
//Operators
// assignment operator
CommandProcessor& CommandProcessor::operator=(const CommandProcessor& other)
{
if (this == &other)
{
return *this;
}
CommandProcessor c(other);
return c;
}
//stream insertion operator
ostream& operator <<(ostream& o, CommandProcessor& g)
{
o << "Command Processor"; //not sure what else the command process could really return...
return o;
}
//Function Definitions
void CommandProcessor::getCommand()
{
CommandProcessor::displayInfo();
//read command from console
CommandProcessor::readCommand();
//create new command object and initialize it with the user input
Command c(userinput);
CommandProcessor::validate(*currentgamestate, userinput, c);
CommandProcessor::saveCommand(c);
}
void CommandProcessor::readCommand()
{
//cin >> userinput; //get user input and store it
//We use getline so that spaces can be stored as well (for filenames, etc.)
getline(cin, userinput);
}
void CommandProcessor::saveCommand(Command commandobj)
{
lc.push_back(commandobj);
cout << lc.back().getEffect() << endl << endl;
if (commandobj.getEffect() == "Exiting program..." && *currentgamestate == "exit")
{
//printList(); //For debugging to make sure elements are properly saved to the list
exit(0);
}
}
//Parameters:
//s: State to change to
void CommandProcessor::changeState(string s)
{
//delete old value and replace with new one
delete currentgamestate;
currentgamestate = new string(s);
}
bool CommandProcessor::validate(vector<string> listofmapfiles, vector<string> listofplayerstrategies, int numberofgames, int maxnumberofturns)
{
if(listofmapfiles.size()-1 < 1 || listofmapfiles.size()-1 > 5)
{
cout << "Validation Failed! Incorrect number of map files" << endl;
return false;
}
else if(listofplayerstrategies.size()-1 < 2 || listofplayerstrategies.size()-1 > 4)
{
cout << "Validation Failed! Incorrect number of player strategies" << endl;
return false;
}
else if(numberofgames < 1 || numberofgames > 5)
{
cout << "Validation Failed! Incorrect number of games" << endl;
return false;
}
else if(maxnumberofturns < 10 || maxnumberofturns > 50)
{
cout << "Validation Failed! Incorrect number max turns" << endl;
return false;
}
else
{
cout << "Validation Passed! All Checks completed" << endl;
return true;
}
}
//Parameters:
//cgs: String representing the current game state
//ui: String representing the user input
void CommandProcessor::validate(string cgs, string ui, Command& commandobj)
{
//manual commands
if (ui.substr(0, 8) == "loadmap " && (cgs == "start" || cgs == "maploaded"))
{
changeState("maploaded");
commandobj.saveEffect("Transitioned to MapLoaded state");
}
else if (ui == "validatemap" && cgs == "maploaded")
{
changeState("mapvalidated");
commandobj.saveEffect("Transitioned to MapValidated state");
}
else if (ui.substr(0, 10) == "addplayer " && (cgs == "mapvalidated" || cgs == "playersadded"))
{
changeState("playersadded");
commandobj.saveEffect("Transitioned to PlayersAdded state");
}
else if (ui == "gamestart" && cgs == "playersadded")
{
changeState("assignreinforcement");
commandobj.saveEffect("Transitioned to AssignReinforcement state");
}
else if (ui == "replay" && cgs == "win")
{
changeState("start");
commandobj.saveEffect("Transitioned to Start state");
}
else if (ui == "quit" && cgs == "win")
{
changeState("exit");
commandobj.saveEffect("Exiting program...");
}
//game-triggered commands
else if (ui == "issueorder" && (cgs == "assignreinforcement" || cgs == "issueorders"))
{
changeState("issueorders");
commandobj.saveEffect("Transitioned to IssueOrders state");
}
else if (ui == "issueordersend" && cgs == "issueorders" )
{
changeState("executeorders");
commandobj.saveEffect("Transitioned to ExecuteOrders state");
}
else if (ui == "execorder" && cgs == "executeorders")
{
changeState("executeorders");
commandobj.saveEffect("Transitioned to ExecuteOrders state");
}
else if (ui == "endexecorders" && cgs == "executeorders")
{
changeState("assignreinforcement");
commandobj.saveEffect("Transitioned to AssignReinforcement state");
}
else if (ui == "win" && cgs == "executeorders")
{
changeState("win");
commandobj.saveEffect("Transitioned to Win state");
}
//Error
else
{
commandobj.saveEffect("Invalid command for current state; no transition.");
}
}
//Parameters:
//a: New game state
void CommandProcessor::setGameState(string a)
{
//delete previous string stored to avoid memory leaks, then replace it with the new game state
delete currentgamestate;
currentgamestate = new string(a);
}
//Utility function to display the current state and valid commands
void CommandProcessor::displayInfo()
{
cout << "Current state: " << *currentgamestate << endl;
cout << "Available commands: ";
if (*currentgamestate == "start")
{
cout << "loadmap <filename>" << endl;
}
else if (*currentgamestate == "maploaded")
{
cout << "loadmap <filename>, validatemap" << endl;
}
else if (*currentgamestate == "mapvalidated")
{
cout << "addplayer <playername>" << endl;
}
else if (*currentgamestate == "playersadded")
{
cout << "addplayer <playername>, gamestart" << endl;
}
else if (*currentgamestate == "assignreinforcement")
{
cout << "issueorder" << endl;
}
else if (*currentgamestate == "issueorders")
{
cout << "issueorder, issueordersend" << endl;
}
else if (*currentgamestate == "executeorders")
{
cout << "execorder, endexecorders, win" << endl;
}
else if (*currentgamestate == "win")
{
cout << "replay, quit" << endl;
}
}
//Utility function to print the command list
void CommandProcessor::printList()
{
for (auto i : lc)
{
cout << "Title: " << i.getTitle() << endl;
cout << "Effect: " << i.getEffect() << endl << endl;
}
}
void CommandProcessor::stringToLog(string s)
{
stringsToLog = s;
}
//===================FileLineReader===================
//Constructors
FileLineReader::FileLineReader(string s)
{
filename = new string(s);
ifs.open(*filename);
line = new string(""); //placeholder value so later calls of delete are safe
}
FileLineReader::FileLineReader(const FileLineReader& other)
{
ifs.open(*(other.filename)); //get file to open from other instance
this->line = new string(*(other.line));
}
//Destructor
FileLineReader::~FileLineReader()
{
ifs.close(); //close stream
delete line; //delete line variable
}
//Operators
// assignment operator
FileLineReader& FileLineReader::operator=(const FileLineReader& other)
{
if (this == &other)
{
return *this;
}
FileLineReader c(other);
return c;
}
//stream insertion operator
ostream& operator <<(ostream& o, FileLineReader& g)
{
o << "File Line Reader"; //not sure what else the file line reader could really return...
return o;
}
//Functions
string FileLineReader::readLineFromFile()
{
//read a line and store it in the line variable
getline(ifs, *line);
return *line;
}
//=============FileCommandProcessorAdapter============
//Constructors
FileCommandProcessorAdapter::FileCommandProcessorAdapter(string s)
{
flr = new FileLineReader(s);
}
FileCommandProcessorAdapter::FileCommandProcessorAdapter(const FileCommandProcessorAdapter& other)
{
this->flr = new FileLineReader(*(other.flr));
}
//Destructor
FileCommandProcessorAdapter::~FileCommandProcessorAdapter()
{
delete flr;
}
//Operators
// assignment operator
FileCommandProcessorAdapter& FileCommandProcessorAdapter::operator=(const FileCommandProcessorAdapter& other)
{
if (this == &other)
{
return *this;
}
FileCommandProcessorAdapter c(other);
return c;
}
//stream insertion operator
ostream& operator <<(ostream& o, FileCommandProcessorAdapter& g)
{
o << "File Command Processor Adapter"; //not sure what else the file line reader could really return...
return o;
}
//Functions
void FileCommandProcessorAdapter::readCommand()
{
userinput = flr->readLineFromFile();
}
void FileCommandProcessorAdapter::getCommand()
{
CommandProcessor::displayInfo();
//read command from console
FileCommandProcessorAdapter::readCommand();
//create new command object and initialize it with the user input
Command c(userinput);
CommandProcessor::validate(*currentgamestate, userinput, c);
CommandProcessor::saveCommand(c);
}