Repository navigation
Expand file tree
/
Copy pathPlayer.cpp
More file actions
621 lines (514 loc) · 16.3 KB
/
Copy pathPlayer.cpp
File metadata and controls
621 lines (514 loc) · 16.3 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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "Player.h"
#include "Board.h"
#include "Card.h"
#include "Decision.h"
#include "PacketID.h"
#include <iostream>
#include <sstream>
using namespace std;
Player::Player()
{
name = "";
actions = 0;
buys = 0;
coins = 0;
}
Player::~Player()
{
socket.disconnect();
}
void Player::displayMessage(string &messageOutput, bool responseRequired)
{
cout << name << " <- " << messageOutput;
sf::Packet sendPacket;
sendPacket << PacketID::STANDARD_MESSAGE << responseRequired << messageOutput;
socket.send(sendPacket);
}
void Player::broadcastToOtherPlayers(string &messageOutput)
{
for(unsigned int ii = 0; ii < otherPlayers.size(); ii++)
{
otherPlayers[ii]->displayMessage(messageOutput, false);
}
}
string Player::getInput()
{
sf::Packet receivedPacket;
socket.receive(receivedPacket);
string receivedText;
receivedPacket >> receivedText;
cout << name << " -> " << receivedText << endl;
return receivedText;
}
void Player::clearScreen()
{
sf::Packet sendPacket;
sendPacket << PacketID::CLEAR_SCREEN;
socket.send(sendPacket);
}
void Player::sendGameOverMessage()
{
sf::Packet sendPacket;
sendPacket << PacketID::GAME_OVER;
socket.send(sendPacket);
}
void Player::outputStats()
{
string pluralCoins = "s", pluralActions = "s", pluralBuys = "s", pluralDeckSize = "s", pluralDiscardPileSize = "s";
if(coins == 1)
pluralCoins = "";
if(actions == 1)
pluralActions = "";
if(buys == 1)
pluralBuys = "";
if(deck.cards.size() == 1)
pluralDeckSize = "";
if(discardPile.cards.size() == 1)
pluralDiscardPileSize = "";
stringstream output;
output << "You have: " << coins << " coin" << pluralCoins << ", " << actions << " action" << pluralActions << ", and " << buys << " buy" << pluralBuys << "." << endl;
output << "You have: " << deck.cards.size() << " card" << pluralDeckSize << " left in your deck and " << discardPile.cards.size() << " card" << pluralDiscardPileSize << " in your discard pile." << endl;
displayMessage(output.str(), false);
}
void Player::outputHand()
{
Decision getDescriptionDecision("To see a description of any of your cards, submit the corresponding number. To continue on, press ENTER. The cards in your hand are:", this);
for(unsigned int ii = 0; ii < hand.cards.size(); ii++)
{
getDescriptionDecision.addOption(hand.cards[ii].name());
}
bool playerIsInterested = true;
while(playerIsInterested)
{
int decisionResult = getDescriptionDecision.makeDecision(true);
if(decisionResult == -1) // Skip description check
{
playerIsInterested = false;
}
else
{
hand.cards[decisionResult].printNameAndDescription(this);
}
}
}
void Player::outputPlayerStatus()
{
outputStats();
outputHand();
}
void Player::playTurn()
{
turnStart();
playActions();
useBuys();
turnEnd();
}
void Player::turnStart()
{
stringstream output;
output << "---| " << name << " |---" << endl;
displayMessage(output.str(), false);
broadcastToOtherPlayers(output.str());
actions = 1;
buys = 1;
coins = 0;
}
void Player::playActions()
{
bool wantsToDoSomething = true;
Board *board = Board::boardGame;
while(actions > 0 && wantsToDoSomething && !board->gameIsOver)
{
vector<Card*> actionCards = hand.getListOfCardsMatchingType(Card::ACTION_TYPE);
if(actionCards.size() == 0) // Has no action cards
{
stringstream output;
output << "You have no ACTION cards, proceeding to the next phase." << endl << endl;
displayMessage(output.str(), false);
wantsToDoSomething = false;
}
else
{
outputPlayerStatus();
Decision cardToPlayDecision("Select an ACTION card to play:", this);
for(unsigned int ii = 0; ii < actionCards.size(); ii++)
{
cardToPlayDecision.addOption(actionCards[ii]->name());
}
cardToPlayDecision.addOption("Do not play any ACTION cards");
cardToPlayDecision.addOption("See player status");
bool actionDecisionOngoing = true;
while(actionDecisionOngoing)
{
unsigned int decisionResult = cardToPlayDecision.makeDecision(false);
if(decisionResult == cardToPlayDecision.choices.size() - 2) // No action
{
stringstream secondaryOutput;
secondaryOutput << "You chose not to play a card, proceeding to the next phase." << endl << endl;
displayMessage(secondaryOutput.str(), false);
actionDecisionOngoing = false;
wantsToDoSomething = false;
}
else if(decisionResult == cardToPlayDecision.choices.size() - 1) // See player status
{
stringstream secondaryOutput;
secondaryOutput << "Showing player status." << endl;
displayMessage(secondaryOutput.str(), false);
outputPlayerStatus();
}
else
{
actions--;
stringstream playerOutput;
playerOutput << "You played the card " << actionCards[decisionResult]->name() << "." << endl;
displayMessage(playerOutput.str(), false);
stringstream globalOutput;
globalOutput << name << " played the card " << acti
8000
onCards[decisionResult]->name() << "." << endl;
broadcastToOtherPlayers(globalOutput.str());
actionCards[decisionResult]->moveToAnotherDeck(cleanupPile);
cleanupPile.cards[cleanupPile.cards.size() - 1].playAction(this, otherPlayers);
actionDecisionOngoing = false;
}
}
}
}
if(actions < 1 && !board->gameIsOver) // No more actions left
{
stringstream output;
output << "You have no more ACTIONS, proceeding to the next phase." << endl << endl;
displayMessage(output.str(), false);
}
}
void Player::useBuys()
{
bool wantsToPlayTreasures = true;
Board *board = Board::boardGame;
while(wantsToPlayTreasures && !board->gameIsOver)
{
outputPlayerStatus();
vector<Card*> treaureCardsInHand = hand.getListOfCardsMatchingType(Card::TREASURE_TYPE);
if(treaureCardsInHand.size() > 0)
{
treaureCardsInHand = hand.getListOfCardsMatchingType(Card::TREASURE_TYPE);
Decision treasureCardToPlay("Select a TREASURE card to play (press ENTER to play all):", this);
for(unsigned int ii = 0; ii < treaureCardsInHand.size(); ii++)
{
treasureCardToPlay.addOption(treaureCardsInHand[ii]->name());
}
treasureCardToPlay.addOption("Play all remaining TREASURE cards");
treasureCardToPlay.addOption("Do not play any more TREASURE cards");
int cardToPlay = treasureCardToPlay.makeDecision(true);
if(cardToPlay == treasureCardToPlay.choices.size() - 2 || cardToPlay == -1) // Play all remaining TREASURE cards
{
for(unsigned int ii = treaureCardsInHand.size() - 1; ii < treaureCardsInHand.size(); ii--)
{
stringstream playerOutput;
playerOutput << "You played the card " << treaureCardsInHand[ii]->name() << "." << endl;
displayMessage(playerOutput.str(), false);
stringstream globalOutput;
globalOutput << name << " played the card " << treaureCardsInHand[ii]->name() << "." << endl;
broadcastToOtherPlayers(globalOutput.str());
treaureCardsInHand[ii]->moveToAnotherDeck(cleanupPile);
cleanupPile.cards[cleanupPile.cards.size() - 1].playTreasure(this, otherPlayers);
}
stringstream playerOutput;
playerOutput << "You have no more TREASURE cards, proceeding to next phase." << endl << endl;
displayMessage(playerOutput.str(), false);
wantsToPlayTreasures = false;
}
else if(cardToPlay == treasureCardToPlay.choices.size() - 1) // Do not play any more cards
{
wantsToPlayTreasures = false;
}
else
{
stringstream playerOutput;
playerOutput << "You played the card " << treaureCardsInHand[cardToPlay]->name() << "." << endl << endl;
displayMessage(playerOutput.str(), false);
stringstream globalOutput;
globalOutput << name << " played the card " << treaureCardsInHand[cardToPlay]->name() << "." << endl;
broadcastToOtherPlayers(globalOutput.str());
treaureCardsInHand[cardToPlay]->moveToAnotherDeck(cleanupPile);
cleanupPile.cards[cleanupPile.cards.size() - 1].playTreasure(this, otherPlayers);
if(treaureCardsInHand.size() == 1)
{
stringstream playerOutputTwo;
playerOutputTwo << "You have no more TREASURE cards, proceeding to next phase." << endl << endl;
displayMessage(playerOutputTwo.str(), false);
wantsToPlayTreasures = false;
}
}
}
else
{
stringstream playerOutput;
playerOutput << "You have no TREASURE cards, proceeding to next phase." << endl << endl;
displayMessage(playerOutput.str(), false);
wantsToPlayTreasures = false;
}
}
OrderedDeck &purchasableCards = board->getPurchasableCards();
bool wantsToBuy = true;
while(buys > 0 && wantsToBuy && !board->gameIsOver)
{
outputStats();
Decision cardToExamineDecision("Select a card to examine by submitting the corresponding number:", this);
for(unsigned int ii = 0; ii < purchasableCards.cards.size(); ii++)
{
string hasEnoughCoinToken = "*";
string pluralCoins = "s";
if(purchasableCards.cards[ii].getPrice(&purchasableCards.cards[ii], this, otherPlayers) > coins)
{
hasEnoughCoinToken = "";
}
if(purchasableCards.cards[ii].getPrice(&purchasableCards.cards[ii], this, otherPlayers) == 1)
{
pluralCoins = "";
}
stringstream formatter;
formatter << "{" << hasEnoughCoinToken << purchasableCards.cards[ii].getPrice(&purchasableCards.cards[ii], this, otherPlayers) << " coin" << pluralCoins << "} " << purchasableCards.cards[ii].name();
cardToExamineDecision.addOption(formatter.str());
}
cardToExamineDecision.addOption("Do not examine any cards.");
bool examineCards = true;
while(examineCards)
{
int decisionResult = cardToExamineDecision.makeDecision(false);
if(decisionResult == cardToExamineDecision.choices.size() - 1) // Chose not to examine anything
{
string pluralBuys = "S";
if(buys == 1)
pluralBuys = "";
stringstream output;
output << "You chose not to use your remaining BUY" << pluralBuys << ". Cleaning up." << endl << endl;
displayMessage(output.str(), false);
examineCards = false;
wantsToBuy = false;
}
else
{
stringstream converter;
converter << "Choose what you would like to do with the card " << purchasableCards.cards[decisionResult].name() << ":";
Decision nextDecision(converter.str(), this);
nextDecision.addOption("See name and description");
nextDecision.addOption("Purchase card");
nextDecision.addOption("Choose another card");
bool lookingAtACard = true;
while(lookingAtACard)
{
int nextDecisionResult = nextDecision.makeDecision(false);
if(nextDecisionResult == 0) // See name and description
{
purchasableCards.cards[decisionResult].printNameAndDescription(this);
}
else if(nextDecisionResult == 1) // Buy the card
{
if(coins >= purchasableCards.cards[decisionResult].getPrice(&purchasableCards.cards[decisionResult], this, otherPlayers)) // Player has enough coins
{
if(purchasableCards.cards[decisionResult].containerDeck->drawCard(discardPile)) // Deck is empty
{
stringstream secondaryOutput;
secondaryOutput << "This deck has recently been depleted so you cannot purchase this card." << endl;
displayMessage(secondaryOutput.str(), false);
}
else
{
coins -= purchasableCards.cards[decisionResult].getPrice(&purchasableCards.cards[decisionResult], this, otherPlayers);
buys--;
lookingAtACard = false;
examineCards = false;
stringstream secondaryOutput;
secondaryOutput << "You purchased the card " << purchasableCards.cards[decisionResult].name() << "." << endl;
displayMessage(secondaryOutput.str(), false);
stringstream globalOutput;
globalOutput << name << " purchased the card " << purchasableCards.cards[decisionResult].name() << "." << endl;
broadcastToOtherPlayers(globalOutput.str());
}
}
else
{
stringstream secondaryOutput;
secondaryOutput << "You do not have enough coins to purchase this card." << endl;
displayMessage(secondaryOutput.str(), false);
}
}
else if(nextDecisionResult == 2) // Choose another card
{
lookingAtACard = false;
}
}
}
}
}
if(buys < 1 && !board->gameIsOver) // No more buys left
{
stringstream output;
output << "You have no more BUYS, cleaning up." << endl;
displayMessage(output.str(), false);
}
}
void Player::turnEnd()
{
if(!Board::boardGame->gameIsOver)
{
discardCleanupPileAndObtainedPile();
drawNewHand();
stringstream firstOutput;
firstOutput << "You have drawn new cards." << endl;
displayMessage(firstOutput.str(), false);
outputHand();
stringstream output;
output << "Press enter to end your turn." << endl;
displayMessage(output.str(), true);
getInput(); // Press enter to continue
}
}
void Player::discardHand()
{
hand.moveContentsToAnotherDeck(discardPile);
}
void Player::discardCleanupPileAndObtainedPile()
{
cleanupPile.moveContentsToAnotherDeck(discardPile);
}
void Player::swapDeckAndDiscardPile()
{
deck.cards.swap(discardPile.cards);
deck.shuffle();
}
bool Player::drawCard()
{
return drawCard(hand);
}
bool Player::drawCard(OrderedDeck &deck)
{
if(this->deck.drawCard(deck)) // No cards left in deck
{
swapDeckAndDiscardPile();
if(this->deck.drawCard(deck)) // All cards in player's hand
{
return true;
}
}
return false;
}
void Player::drawNewHand()
{
discardHand();
for(unsigned int ii = 0; ii < 5; ii++)
{
drawCard();
}
hand.sortByName();
}
bool Player::plusCards(int numberOfCards)
{
for(int ii = 0; ii < numberOfCards; ii++)
{
if(!drawCard()) // Drew a card
{
stringstream output;
output << "You drew the card " << hand.cards[hand.cards.size() - 1].name() << "." << endl;
displayMessage(output.str(), false);
stringstream globalOutput;
globalOutput << name << " drew a card." << endl;
broadcastToOtherPlayers(globalOutput.str());
}
else
{
stringstream output;
output << "You could not draw any more cards." << endl;
displayMessage(output.str(), false);
stringstream globalOutput;
globalOutput << name << " could not draw any more cards." << endl;
broadcastToOtherPlayers(globalOutput.str());
return true;
}
}
return false;
}
void Player::plusActions(int numberOfActions)
{
string pluralActions = "S";
if(numberOfActions == 1)
pluralActions = "";
stringstream output;
output << "You gained " << numberOfActions << " ACTION" << pluralActions << "." << endl;
displayMessage(output.str(), false);
stringstream globalOutput;
globalOutput << name << " gained " << numberOfActions << " ACTION" << pluralActions << "." << endl;
broadcastToOtherPlayers(globalOutput.str());
actions += numberOfActions;
}
void Player::plusBuys(int numberOfBuys)
{
string pluralBuys = "S";
if(numberOfBuys == 1)
pluralBuys = "";
stringstream output;
output << "You gained " << numberOfBuys << " BUY" << pluralBuys << "." << endl;
displayMessage(output.str(), false);
stringstream globalOutput;
globalOutput << name << " gained " << numberOfBuys << " BUY" << pluralBuys << "." << endl;
broadcastToOtherPlayers(globalOutput.str());
buys += numberOfBuys;
}
void Player::plusCoins(int numberOfCoins)
{
string pluralCoins = "S";
if(numberOfCoins == 1)
pluralCoins = "";
stringstream output;
output << "You gained " << numberOfCoins << " COIN" << pluralCoins << "." << endl;
displayMessage(output.str(), false);
stringstream globalOutput;
globalOutput << name << " gained " << numberOfCoins << " COIN" << pluralCoins << "." << endl;
broadcastToOtherPlayers(globalOutput.str());
coins += numberOfCoins;
}
void Player::fixCointainerDeckPointersOfCards()
{
deck.fixContainerDeckPointers();
discardPile.fixContainerDeckPointers();
hand.fixContainerDeckPointers();
cleanupPile.fixContainerDeckPointers();
}
bool Player::preAttack()
{
if(moatReact())
return true;
else
return false;
}
bool Player::moatReact()
{
vector<Card*> moatCards = hand.getListOfCardsMatchingID(Card::MOAT_CARD);
if(moatCards.size() > 0)
{
Decision revealMoat("Reveal your MOAT?", this);
revealMoat.addOption("Yes");
revealMoat.addOption("No");
int decisionResult = revealMoat.makeDecision(false);
if(decisionResult == 0) // Yes
{
stringstream output;
output << "You reacted with the card MOAT." << endl;
displayMessage(output.str(), false);
stringstream globalOutput;
globalOutput << name << " reacted with the card MOAT." << endl;
broadcastToOtherPlayers(globalOutput.str());
return true;
}
else // No
{
stringstream output;
output << "You chose not to react with MOAT." << endl;
displayMessage(output.str(), false);
return false;
}
}
else // No MOAT cards in hand
{
return false;
}
}