-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameEngine.h
503 lines (415 loc) · 15.6 KB
/
GameEngine.h
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
502
503
//To prevent mutiple inclusion:
#ifndef GAMEENGINE_H
#define GAMEENGINE_H
#include <vector>
#include <string>
#include "Map.h"
#include "CommandProcessing.h"
#include "LoggingObserver.h"
#include <set>
#include "PlayerStrategies.h"
using std::cout;
using std::endl;
class CommandProcessor;
//solve dependencies
//Game is defined at the end
class GameEngine;
//An abstract class:
class GameState
{
public:
//pure virtual function
//needs to be overridden
virtual void transition(GameEngine* GameEngine, string command)=0;
virtual ~GameState(); //destructor
virtual bool validate(string command) = 0;
virtual string getName() = 0;
//implemented when called to print current state
//pure virtual function needs to be overridden
virtual void toString() = 0;
//clone() function needed by the copy constructor of GameEngine
//Reason: GameState is abstract, and _currentState is of type GameState
// We cannot create object with GameState, so we need a vritual
// clone() methhod to get a deep copy of each state
virtual GameState* clone() const = 0;
virtual void commandMessage() = 0;
};
ostream& operator<<(ostream& os, const GameState& s);
/**********************************************************************
********************Start/Initial State:*******************************
***********************************************************************/
class Start : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
//Constructor
Start();
//Destructor
~Start();
bool validate(string command);
string getName();
Start(const Start& other); //copy constructor
Start& operator = (const Start& s); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const Start& s);
//overridden clone() function
GameState* clone() const;
//toString()
void toString();
void commandMessage();
private:
string* _command;
};
/**********************************************************************
********************MapLoaded State:***********************************
***********************************************************************/
class MapLoaded : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
MapLoaded(); //constructor
~MapLoaded(); //destructor
bool validate(string command);
string getName();
MapLoaded(const MapLoaded& other); //deep copy constructor
MapLoaded& operator = (const MapLoaded& m); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const MapLoaded& m);
GameState* clone() const; //clone() overridden
//toString() overriden
void toString();
void commandMessage();
private:
string* _command1;
string* _command2;
};
/**********************************************************************
********************MapValidated State:********************************
***********************************************************************/
class MapValidated : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
MapValidated(); //constructor
~MapValidated(); //destructor
bool validate(string command);
string getName();
MapValidated(const MapValidated& other); //deep copy constructor
MapValidated& operator = (const MapValidated& mv); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const MapValidated& mv);
GameState* clone() const; //clone() overridden
//toString() overridden
void toString();
void commandMessage();
private:
string* _command;
};
/**********************************************************************
********************PlayersAdded State:********************************
***********************************************************************/
class PlayersAdded : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
PlayersAdded(); //constructor
~PlayersAdded(); //destructor
bool validate(string command);
string getName();
PlayersAdded(const PlayersAdded& other); //deep copy constructor
PlayersAdded& operator = (const PlayersAdded& p); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const PlayersAdded& p);
GameState* clone() const; //clone() overridden
//toString() overridden:
void toString();
void commandMessage();
private:
string* _command1;
string* _command2;
string* _commandGame;
};
/**********************************************************************
****************AssignedReinforcement State:***************************
***********************************************************************/
class AssignedReinforcement : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
AssignedReinforcement(); //constructor
~AssignedReinforcement(); //destructor
bool validate(string command);
string getName();
AssignedReinforcement(const AssignedReinforcement& other); //deep copy constructor
AssignedReinforcement& operator = (const AssignedReinforcement& a); //assignment operator
friend ostream& operator<<(ostream& os, const AssignedReinforcement& a);
GameState* clone() const; //clone() overridden
//toString() overridden
void toString();
void commandMessage();
private:
string* _command;
};
/**********************************************************************
******************Issue Orders State:**********************************
***********************************************************************/
class ReinforcementPhase : public GameState {
public:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
ReinforcementPhase(); //constructor
~ReinforcementPhase(); //destructor
ReinforcementPhase(const ReinforcementPhase& other); //deep copy constructor
ReinforcementPhase& operator = (const ReinforcementPhase& i); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const ReinforcementPhase& i);
GameState* clone() const; //clone() overridden
//toString overridden:
void toString();
void commandMessage();
private:
string* _command1;
string* _command2;
};
/**********************************************************************
******************Issue Orders State:**********************************
***********************************************************************/
class IssueOrders : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
IssueOrders(); //constructor
~IssueOrders(); //destructor
bool validate(string command);
string getName();
IssueOrders(const IssueOrders& other); //deep copy constructor
IssueOrders& operator = (const IssueOrders& i); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const IssueOrders& i);
GameState* clone() const; //clone() overridden
//toString overridden:
void toString();
void commandMessage();
private:
string* _command1;
string* _command2;
};
/**********************************************************************
******************Execute Orders State:********************************
***********************************************************************/
class ExecuteOrders : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
ExecuteOrders(); //constructor
~ExecuteOrders(); //destructor
bool validate(string command);
string getName();
ExecuteOrders(const ExecuteOrders& other); //deep copy constructor
ExecuteOrders& operator = (const ExecuteOrders& e); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const ExecuteOrders& e);
GameState* clone() const; //clone() overridden
//toString() overridden
void toString();
void commandMessage();
private:
string* _command1;
string* _command2;
string* _command3;
};
/**********************************************************************
*************************Win State:************************************
***********************************************************************/
class Win : public GameState {
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
Win(); //constructor
~Win(); //destructor
bool validate(string command);
string getName();
Win(const Win& other); //deep copy constructor
Win& operator = (const Win& w); //assignment operator
//stream insertion operator:
friend ostream& operator<<(ostream& os, const Win& w);
GameState* clone() const; //clone() overridden
//toString() overridden
void toString();
void commandMessage();
private:
string* _command1;
string* _command2;
};
/**********************************************************************
*************************End State:************************************
***********************************************************************/
class End: public GameState{
public:
//transition function:
/*Takes two arguments, validate the input command and makes decision about
whether stay in current ststate or transit to next state*/
void transition(GameEngine* GameEngine, string command);
End();
~End();
bool validate(string command);
string getName();
End(const End& other);
End& operator = (const End& e);
GameState* clone() const;
void toString();
void commandMessage();
};
/**********************************************************************
*************************GameEngine Class:*****************************
***********************************************************************/
/*Game class would be used to create new object which contains
the current state of the game, and transit between states when
received a command from the console.*/
class GameEngine : public ILoggable, public Subject{
//vector pointing to players
std::vector<Player*>* _players_ptr;
//vector showing each players army pool. ordered by player index
vector<int*>* _armyPool;
//vector to track if a player conquered a territory this round
vector<bool*>* _conqBool;
//The deck for the game
Deck * _deck;
queue<Order*>* _orders;
queue<string>* _mapQ;
public:
int* _round;
// Returns a pointer to the current players in game
std::vector<Player*>& get_players();
//Constructor
GameEngine();
//Deconstructor
~GameEngine();
//returns pointer of current state
GameState* getCurrentState() const;
//Used for logging
string stringToLog();
//transition function according to command inputs
void transit(string c);
//used to set Game object to next stats
void setState(GameState* newState);
//setter is used to set the status to false when we reach the End state
void setStatus(bool b);
//getter is used to get the current status of the game
bool getStatus();
bool fileReader;
//returns the _armyPool
vector<int*>& get_ArmyPools();
//return amount in army pool at index
int* get_ArmyPoolAt(int index);
//returns the _deck
Deck* getDeck();
//Copy Constructor and operator:
GameEngine(const GameEngine& other);
GameEngine& operator = (const GameEngine& e);
//get orders
queue<Order*>& get_orders();
set<string> already;
//stream insertion operator:
friend ostream& operator<<(ostream& os, const GameEngine& g);
//return the command processor:
CommandProcessor* getCommandProcessor();
//startup phase
void startupPhase(Observer* observer);
//add new player state
void add_new_player();
//map picker state
void map_picker();
//assign territores state
void assign_territories();
//determines order of play
void order_of_play();
//give each army pool initial value of 50
void give_initial_armies();
//Checks if an interval in army pool of 0
bool has_army(int i);
//draw 2 cards per player
void draw_initial_cards();
//Draws a card
void give_card(Player*);
//goes to reinforcement phase
void addReinforcementsPhase();
//goes to issue order phase
void deployReinforcementPhase();
//goes to execute order phase
void excecuteOrdersPhase();
//goes to orders picker phase
void ordersPicker(Player&);
//pick a card type for a player
bool cardValidator(Player& player, string type);
//validate a loaded map
void validateMap();
//Checks to see if the territories are all owned by the same player
bool checkWin(GameEngine& engine);
//Initializes the _conqBool
void popConqBool();
//Get the vector remembering if a player has conquered this round
vector<bool*>& getConq();
//Change value to true
void setConq(int i);
// Reset all conquered to false
void resetAllConq();
// Deploy new troops phase
void deploy_phase(Player& player);
// Change a human to a different strategy of player.
void changePlayerStrategy(Player& player);
// Changes the game state
void change_state(GameState*);
// Read all the maps from command
void get_all_map_commands();
// Get the maps read in queue
queue<string>& get_mapQ();
/*******************
********a3*********
*******************/
// Gets winner name
string* getWinnerName();
// Game game number
string* getGameNum();
// Get map number
string* getMapNum();
// set a winner
void setWinner(string w);
// set a game number
void setGameNum(string g);
// set a map number
void setMapNum(string m);
// get end game results
string* getResult();
// the results
string* result;
private:
//current state, used for logging
GameState* _currentState;
bool _continue;
CommandProcessor* _myProcessor;
/*******************
********a3*********
*******************/
string* _winner;
string* _game_num;
string* _map_num;
};
#endif