-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCards.cpp
More file actions
198 lines (160 loc) · 5.3 KB
/
Cards.cpp
File metadata and controls
198 lines (160 loc) · 5.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
#include "Cards.h"
#include "Player.h"
#include "Orders.h"
#include <cstdlib>
#include <ctime>
#include <algorithm>
// ---------------------- Card ----------------------------
Card::Card(CardType type) : type(type) {}
Card::Card(const Card& other) : type(other.type) {}
Card& Card::operator=(const Card& other) {
if (this != &other)
type = other.type;
return *this;
}
CardType Card::getType() const {
return type;
}
string Card::getTypeAsString() const {
if (this == nullptr)
{
return "NULLCardType";
}
switch (type) {
case CardType::Bomb: return "Bomb";
case CardType::Reinforcement: return "Reinforcement";
case CardType::Blockade: return "Blockade";
case CardType::Airlift: return "Airlift";
case CardType::Diplomacy: return "Diplomacy";
default: return "UnknownCardType";
}
}
void Card::play(Player* player, Deck* deck, Hand* hand, const CardPlayContext& context) {
cout << "[Card] Played: " << getTypeAsString() << endl;
cout << "Generated ";
switch (type) {
case CardType::Bomb:
player->issueOrder(new Bomb(player, context.target));
cout << "Bomb";
break;
case CardType::Reinforcement:
player->issueOrder( new Deploy(player, context.target, context.armies));
cout << "Reinforcement";
break;
case CardType::Blockade:
player->issueOrder(new Blockade(player, context.target));
cout << "Blockade";
break;
case CardType::Airlift:
player->issueOrder(new Airlift(player, context.source, context.target, context.armies));
cout << "Airlift";
break;
case CardType::Diplomacy:
player->issueOrder(new Negotiate(player, context.targetPlayer));
cout << "Diplomacy";
break;
default:
cout << "Unknown";
break;
}
cout << " Order and added it to " + player->getName() + "'s OrdersList.\n\n";
// Capture type before removing this from the hand
CardType currentType = this->getType();
hand->removeCard(this);
deck->addCard(make_unique<Card>(currentType));
}
ostream& operator<<(ostream& os, const Card& card) {
os << "Card(" << card.getTypeAsString() << ")";
return os;
}
// ---------------------- Deck ----------------------------
Deck::Deck() {
srand(static_cast<unsigned>(time(nullptr))); // Randomness for draw()
}
Deck::Deck(const Deck& other) {
for (const auto& card : other.cards)
cards.push_back(make_unique<Card>(*card));
}
Deck& Deck::operator=(const Deck& other) {
if (this != &other) {
cards.clear();
for (const auto& card : other.cards)
cards.push_back(make_unique<Card>(*card));
}
return *this;
}
void Deck::addCard(unique_ptr<Card> card) {
cards.push_back(move(card));
}
unique_ptr<Card> Deck::draw() {
if (cards.empty()) return nullptr;
int i = rand() % static_cast<int>(cards.size());
auto selected = move(cards[i]);
cards.erase(cards.begin() + i);
return selected;
}
int Deck::size() const {
return static_cast<int>(cards.size());
}
ostream& operator<<(ostream& os, const Deck& deck) {
os << "Deck[" << deck.size() << "]: ";
for (const auto& card : deck.cards)
os << *card << " ";
return os;
}
bool Deck::isEmpty() const {
return cards.empty();
}
// ---------------------- Hand ----------------------------
Hand::Hand(const Hand& other) {
for (const auto& card : other.cards)
cards.push_back(make_unique<Card>(*card));
}
Hand& Hand::operator=(const Hand& other) {
if (this != &other) {
cards.clear();
for (const auto& card : other.cards)
cards.push_back(make_unique<Card>(*card));
}
return *this;
}
void Hand::addCard(unique_ptr<Card> card) {
cards.push_back(move(card));
}
void Hand::removeCard(const Card* card) {
cards.erase(remove_if(cards.begin(), cards.end(),
[card](const unique_ptr<Card>& c) { return c.get() == card; }), cards.end());
}
void Hand::playAll(Player* player, Deck* deck, const vector<CardPlayContext>& contexts) {
vector<Card*> toPlay;
toPlay.reserve(cards.size());
for (const auto& c : cards)
toPlay.push_back(c.get());
for (size_t i = 0; i < toPlay.size() && i < contexts.size(); ++i) {
cout << "[Hand] Playing card #" << i + 1 << ": " << toPlay[i]->getTypeAsString() << endl;
toPlay[i]->play(player, deck, this, contexts[i]);
}
}
const vector<unique_ptr<Card>>& Hand::getCards() const {
return cards;
}
ostream& operator<<(ostream& os, const Hand& hand) {
os << "Hand[" << hand.cards.size() << "]: ";
for (const auto& card : hand.cards)
os << *card << " ";
return os;
}
bool operator==(const Hand& playerOneHand, const Hand& playerTwoHand) {
const auto& cards1 = playerOneHand.getCards();
const auto& cards2 = playerTwoHand.getCards();
if (cards1.size() != cards2.size())
return false;
for (size_t i = 0; i < cards1.size(); ++i) {
if (cards1[i]->getType() != cards2[i]->getType())
return false;
}
return true;
}
bool operator!=(const Hand& playerOneHand, const Hand& playerTwoHand) {
return !(playerOneHand == playerTwoHand);
}