-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrders.cpp
More file actions
551 lines (415 loc) · 16.4 KB
/
Orders.cpp
File metadata and controls
551 lines (415 loc) · 16.4 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
#include <string>
#include <iostream>
#include <typeinfo>
#include "Orders.h"
#include <limits>
/* -------------------------------------------------------------------------- */
/* OrdersList Class */
/* -------------------------------------------------------------------------- */
OrdersList::OrdersList() { // default constructor
}
OrdersList::OrdersList(OrdersList& copy){ // copy constructor
this->vectorOfOrders = vector <Order*> ();
for (auto order = copy.vectorOfOrders.begin(); order != copy.vectorOfOrders.end(); order++) {
vectorOfOrders.push_back(*order);
}
}
OrdersList& OrdersList::operator=(const OrdersList& ol) { // assignment operator
for (auto order = ol.vectorOfOrders.begin(); order != ol.vectorOfOrders.end(); order++) {
vectorOfOrders.push_back(*order);
}
return *this;
}
ostream& operator<<(ostream &out, OrdersList& o) { //displays description and effect of Order
int counter = 0;
cout << "Printing List of Orders..." << endl;
for (const auto o : o.vectorOfOrders) {
string str = typeid(*o).name();
cout << "The order at position " << counter << " in the list of orders is : " << str.substr(1) << " " << endl;
counter++;
}
cout << endl;
return out;
}
void OrdersList::add(Order* o) {
cout<< "Order added"<<endl;
vectorOfOrders.push_back(o);
//Notify(o);
}
void OrdersList::remove(int i) // removes order at position i
{
cout << "Erasing order at position" << i << "..." << endl;
cout << endl;
//delete vectorOfOrders[i];
vectorOfOrders.erase(vectorOfOrders.begin() + i);
}
void OrdersList::move(int from, int to) { // moves order from position <from> to position <to>
cout << "Moving order from position " << from << " to position " << to << endl;
cout << endl;
swap(vectorOfOrders[from], vectorOfOrders[to]);
}
OrdersList::~OrdersList() { // destructor
for (Order* order : vectorOfOrders){
delete order;
order = nullptr;
}
delete this;
};
string OrdersList::stringToLog() {
return "Order Issued: ...";
};
/* -------------------------------------------------------------------------- */
/* Order Class */
/* -------------------------------------------------------------------------- */
Order::Order() // default constructor
{
orderEffect = "Order has been placed on order list. Nothing happened yet\n";
}
Order::Order(Order& copy){ // copy constructor
orderEffect = copy.orderEffect;
}
string Order::getOrderEffect(){ // getter for orderEffect
return orderEffect;
}
ostream& operator<<(ostream &out, Order& o) { //displays description and effect of Order
string str = typeid(o).name();
cout << "Here is information for order of type " << str.substr(1) << ":" << endl;
out << "--- Order description: ---" << endl;
out << o.getDescription() << endl;
out << "--- Order Effect: ---" << endl;
out << o.getOrderEffect() << endl;
return out;
}
Order::~Order() // default constructor
{
delete this;
}
string Order::stringToLog() {
return "Order Executed: ...";
};
void Order::execute(Player* player) {
Notify(this);
}
/* -------------------------------------------------------------------------- */
/* Deploy Class */
/* -------------------------------------------------------------------------- */
Deploy::Deploy(Territory* target, int numberOfArmies){ //contructor
this->target = target;
this->numberOfArmies = numberOfArmies;
}
Deploy::Deploy(Deploy& copy){ //copy constructor
orderEffect = copy.orderEffect;
}
Deploy& Deploy::operator=(const Deploy& d){ //assignment operator
orderEffect = d.orderEffect;
return *this;
}
void Deploy::execute(Player* player){
if (Deploy::validate(player)) {
// int reinforcementAmount = 0;
// while (true) {
// cout << "\nPlayer " << player->getName() << "'s turn: " << endl;
// cout << "How many reinforcements do you wish to send ( 1 - " << player->getArmy() << " )?";
// if (!(cin >> reinforcementAmount)) {
// cin.clear();
// cin.ignore(numeric_limits<streamsize>::max(), '\n');
// cout << "Invalid input: Please try again!" << endl;
// } else if (reinforcementAmount < 1 || reinforcementAmount > player->getArmy())
// cout << "Invalid Input: Please try again!" << endl;
// else
// break;
// } // end of while loop
this->target->setArmy(this->target->getArmy() + numberOfArmies);
player->setArmy(player->getArmy()-numberOfArmies);
orderEffect = "Armies have been placed on players territories";
} else
orderEffect = "Unable to Deploy armies";
}
bool Deploy::validate(Player* player){
// Check ownership of target territory
if (this->target->getOwner()->getName() == player->getName()) {
if (player->getArmy() != 0) // Check if reinforcement pool is empty
return true;
else
cout << "There are no reinforcements available!";
} else
cout << "Invalid Order: Player does not own target territory!";
return false;
}
string Deploy::getDescription()
{
return "The Deploy order places some armies on one of the current players territories";
}
Deploy::~Deploy(){
}
/* -------------------------------------------------------------------------- */
/* Advance Class */
/* -------------------------------------------------------------------------- */
Advance::Advance(Territory* target, Territory* source, int units){
this->target = target;
this->source = source;
this->units = units;
}
Advance::Advance(Advance& copy){ // copy constructor
orderEffect = copy.orderEffect;
}
Advance& Advance::operator=(const Advance& a){ //assignment operator
orderEffect = a.orderEffect;
return *this;
}
void Advance::execute(Player* player){
if (Advance::validate(player)) { //validate the order
Player* enemy = target->getOwner();
if (player->getStrategy()->getName() == "human"){
player->removeCardOfTypeFromHand(player, CardType::Reinforcement);
}
if (this->target->getOwner()->getName() == player->getName()){
source->setArmy(source->getArmy() - units);
target->setArmy(target->getArmy() + units);
cout<< "Target territory belongs to you." << endl;
cout<< *source << " is now advancing "<<units<<" units to territory "<<*target<<endl;
orderEffect = "Advanced armies from one owned territory to another";
cout << *source << " now has " << source->getArmy() << " armies." << endl;
cout << *target << " now has " << target->getArmy() << " armies.\n" << endl;
Notify(this);
}
else if (this->target->getOwner()->getName() != player->getName()){
int attacking = units;
int defending = target->getArmy();
enemy->setIsAttacked(true);
cout<<"\nTarget territory belongs to another player.";
cout<<"\n" << *source << " has " <<units << " armies and is commencing attack on " << *target << " which has " << target->getArmy() << " armies."<<endl;
// for each unit in the attacking army, there is a 60 percent chance of victory over
// each unit in the defending army. Similarly, each defending unit has a 70 percent chance of victory
while (attacking != 0 && defending != 0){
// If the attacking unit defeats the defending unit
if (rand() % 100 < 60){
defending = defending-1;
}
// If the defending unit defeats the attacking unit
if (rand() % 100 < 70 ){
attacking = attacking-1;
}
}
// If the player conquers this target territory:
// Set the new number of army units in the target territory after it has been conquered
// Erase its previous owner by removing it from their list of territories owned
// Set it to its new owner (the conqueror)
if (defending == 0){
// Deleting territory from previous owner's list
int indexToDelete = -1;
for (int i = 0; i < enemy->getPlayerTerritories().size(); i++) {
if (enemy->getPlayerTerritories()[i] == target) {
indexToDelete = i;
break;
}
}
// Erase the territory pointer from the vector
enemy->removeTerritory(indexToDelete);
player->addTerritory(target);
target->setOwner(player);
cout<<"\n" << player->getName() << ":"<< endl;
cout << source->getName() << " has defeated " << target->getName()<< ". Target territory conquered. You won the battles.\n";
target->setArmy(attacking);
source->setArmy(this->source->getArmy() - units);
cout << "Ownership of " << target->getName() << " has been changed to " << player->getName() << "." << endl;
cout << "You now own " << target->getName() << " with " << target->getArmy() << " armies on it.\n" << endl;
// give the player a card since they conquered a territory
//player->myHand->handCards.push_back(myDeck->draw());
// If the attacking army is defeated. Meaning, the target territory has not been conquered
} else {
// Deleting territory from previous owner's list
int indexToDelete = -1;
for (int i = 0; i < player->getPlayerTerritories().size(); i++) {
if (player->getPlayerTerritories()[i] == target) {
indexToDelete = i;
break;
}
}
// Erase the territory pointer from the vector
player->removeTerritory(indexToDelete);
enemy->addTerritory(target);
target->setOwner(enemy);
cout <<"\n" << player->getName() << ":" << endl;
cout << target->getName() << " has defeated " << source->getName()<< ". Source territory conquered. You lost the battle.\n";
this->source->setArmy(this->source->getArmy() - this->units);
this->target->setArmy(defending);
cout << "You no longer own " << source->getName() << "." << endl;
cout << "Ownership of " << source->getName() << " has been changed to " << enemy->getName() << "." << endl;
cout << enemy->getName() << " now owns: " <<source->getName() << " with " << source->getArmy() << " armies on it.\n" << endl;
}
orderEffect = "Attacked with armies from owned territory to enemy territory";
Notify(this);
}
}else{
//display error message or whatever has to happen
orderEffect = "Unable to Advance armies";
}
}
bool Advance::validate(Player* player){
if (this->source->getOwner()->getName() != player->getName()){
cout<<"\nTerritory does not belong to player"<<endl;
return false;
} else {
for (auto it: this->source->getAdjacentTerritories()){
if (it->getName() == target->getName()){
return true;
}
}
cout<<"\nTarget territory is not adjacent"<<endl;
return false;
}
}
string Advance::getDescription()
{
return "The Advance order moves some armies from one of the current players territories (source) to an adjacent territory (target). If the target territory belongs to the current player, the armies are moved to the target territory. If the target territory belongs to another player, an attack happens between the two territories.";
}
Advance::~Advance(){
}
/* -------------------------------------------------------------------------- */
/* Bomb Class */
/* -------------------------------------------------------------------------- */
Bomb::Bomb(Territory* target){
this->target = target;
}
Bomb::Bomb(Bomb& copy){ // copy constructor
orderEffect = copy.orderEffect;
}
Bomb& Bomb::operator=(const Bomb& b){ //assignment operator
orderEffect = b.orderEffect;
return *this;
}
void Bomb::execute(Player* player){
if (Bomb::validate(player)){ //validate the order
this->target->setArmy(this->target->getArmy() / 2);
cout << "The target territory has been bombed (half the army is gone)";
//bomb area
orderEffect = "Destroyed half of the armies located on the opponents territory";
player->removeCardOfTypeFromHand(player, CardType::Bomb);
Notify(this);
} else
orderEffect = "Invalid Order: Player owns target territory!";
}
bool Bomb::validate(Player* player) {
// Check ownership of target territory
if (this->target->getOwner()->getName() != player->getName())
// Iterate through owned territories
for (Territory* pTerritory : player->getPlayerTerritories())
// Iterate through adjacent territories
for (Territory* adjTerritory : pTerritory->getAdjacentTerritories())
// Check target territory is adjacent
if (adjTerritory->getName() == this->target->getName())
return true;
return false;
}
string Bomb::getDescription(){
return "The Bomb order destroys half of the armies located on an opponents territory that is adjacent to one of the current players territories.";
}
Bomb::~Bomb(){
}
/* -------------------------------------------------------------------------- */
/* Blockade Class */
/* -------------------------------------------------------------------------- */
Blockade::Blockade(Territory* target){
this->target=target;
}
Blockade::Blockade(Blockade& copy){ // copy constructor
orderEffect = copy.orderEffect;
}
Blockade& Blockade::operator=(const Blockade& b){ //assignment operator
orderEffect = b.orderEffect;
return *this;
}
void Blockade::execute(Player* player){
if (Blockade::validate(player)) {
this->target->setArmy(this->target->getArmy() * 2);
// IMPLEMENT: give ownership to neutralPlayer
//target->setOwner();
orderEffect = "Blockaded the territory. Territory is now a neutral zone";
player->removeCardOfTypeFromHand(player, CardType::Blockade);
Notify(this);
} else
orderEffect = "Invalid Order: Player does not own target territory!";
}
bool Blockade::validate(Player* player){
if (this->target->getOwner()->getName() == player->getName())
return true;
return false;
}
string Blockade::getDescription(){
return "The Blockade order triples the number of armies on one of the current players territories and makes it a neutral territory.";
}
Blockade::~Blockade(){
}
/* -------------------------------------------------------------------------- */
/* Airlift Class */
/* -------------------------------------------------------------------------- */
Airlift::Airlift(Territory* target, Territory* source, int units){
this->target = target;
this->source = source;
this->units = units;
}
Airlift::Airlift(Airlift& copy){ // copy constructor
orderEffect = copy.orderEffect;
}
Airlift& Airlift::operator=(const Airlift& a){ //assignment operator
orderEffect = a.orderEffect;
return *this;
}
void Airlift::execute(Player* player) {
if (Airlift::validate(player)){
this->source->setArmy(this->source->getArmy() - this->units);
this->target->setArmy(this->target->getArmy() + this->units);
orderEffect = "Airlifted armies from one territory to another";
player->removeCardOfTypeFromHand(player, CardType::Airlift);
Notify(this);
}else{
//display error message or whatever has to happen
orderEffect = "Unable to perform Airlift";
}
}
bool Airlift::validate(Player* player){
if (this->target->getOwner()->getName() == player->getName())
if (this->source->getOwner()->getName() == player->getName())
return true;
return false;
}
string Airlift::getDescription(){
return "The Airflift order advances some armies from one of the current players territories to any another territory.";
}
Airlift::~Airlift(){
}
/* -------------------------------------------------------------------------- */
/* Negotiate Class */
/* -------------------------------------------------------------------------- */
Negotiate::Negotiate(Player* player){
this->target = target;
}
Negotiate::Negotiate(Negotiate& copy){ //copy constructor
orderEffect = copy.orderEffect;
}
Negotiate& Negotiate::operator=(const Negotiate& n){ //assignment operator
orderEffect = n.orderEffect;
return *this;
}
void Negotiate::execute(Player* player){
if (Negotiate::validate(player)){ //validate the order
// CREATE TRUCE VARIABLE FOR PLAYERS
orderEffect = "Negotiations performed. Attacks have been prevented until the end of turn";
player->removeCardOfTypeFromHand(player, CardType::Diplomacy);
Notify(this);
}else{
//display error message or whatever has to happen
orderEffect = "Unable to Negotiate";
}
}
bool Negotiate::validate(Player* player){
if (this->target->getOwner()->getName() != player->getName())
return true;
return false;
}
string Negotiate::getDescription(){
return "The Negotiate order prevents attacks between the current player and another player until the end of the turn.";
}
Negotiate::~Negotiate(){
}