forked from AA789-ai/WarzoneGame-COMP345
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCards.cpp
More file actions
204 lines (176 loc) · 5.56 KB
/
Cards.cpp
File metadata and controls
204 lines (176 loc) · 5.56 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
#include "Cards.h"
#include "Orders.h"
#include <cstdlib>
//---------------------------------------------------CARD---------------------------------------------------------------------------
// Constructor
Card::Card(CardType type, Deck* deck) : type(type), deck(deck) {}
// Copy constructor (same deck object on purpose)
Card::Card(Card& other) : type(other.type){
Deck* newDeck = other.deck;
this->deck = newDeck;
}
// Assignment operator (shallow on purpose)
Card& Card::operator=(Card& other) {
if (this != &other) {
type = other.type;
Deck* newDeck = other.deck;
this->deck = newDeck;
}
return *this;
}
// Type getter
CardType Card::getType() {
return type;
}
// Stream insertion operator
std::ostream& operator<<(std::ostream& os, Card& card) {
switch (card.getType()) {
case CardType::Bomb:
os << "Card Type: " << "Bomb";
break;
case CardType::Reinforcement:
os << "Card Type: " << "Reinforcement";
break;
case CardType::Blockade:
os << "Card Type: " << "Blockade";
break;
case CardType::Airlift:
os << "Card Type: " << "Airlift";
break;
case CardType::Diplomacy:
os << "Card Type: " << "Diplomacy";
break;
default:
os << "Card Type: " << "Unknown" << std::endl;
break;
}
return os;
}
// Creates order and adds it to orderlist. Card is then put back into the deck.
void Card::play(Hand& hand, int index, OrdersList& ordersList) {
if (index >= 0 && index < hand.cards.size()) {
Card* playedCard = hand.cards[index];//put card back in deck before removing from hand from
if (deck != nullptr) {
deck->cards.push_back(playedCard);
}
switch (playedCard->getType()) {//create order and put into orderlist
case CardType::Bomb: {
Bomb *newOrder = new Bomb();
ordersList.orders.push_back(newOrder);
break;
}
case CardType::Reinforcement: {
//newOrder = new Reinforcement();
//ordersList.orders.push_back(newOrder);
break;
}
case CardType::Blockade: {
Blockade *newOrder = new Blockade();
ordersList.orders.push_back(newOrder);
break;
}
case CardType::Airlift: {
Airlift *newOrder = new Airlift();
ordersList.orders.push_back(newOrder);
break;
}
case CardType::Diplomacy: {
//newOrder = new Diplomacy();
//ordersList.orders.push_back(newOrder);
break;
}
default:
break;
}
std::cout << *playedCard << " was played." << std::endl;
hand.cards[index] = nullptr;
hand.cards.erase(hand.cards.begin() + index);
}
else {
std::cout << "Invalid index." << std::endl;
}
}
//---------------------------------------------------DECK---------------------------------------------------------------------------
// Constructor
Deck::Deck() {
for (int i = 0; i < 20; i++) {
CardType randomType = static_cast<CardType>(rand() % static_cast<int>(CardType::Diplomacy) + 1); // random CardType
Card* newCard = new Card(randomType, this); // create the card
cards.push_back(newCard); // add card to deck
}
}
// Copy constructor
Deck::Deck(Deck& other) {
for (Card* card : other.cards) {
this->cards.push_back(new Card(*card));
}
}
// Assignment operator
Deck& Deck::operator=(Deck & other) {
if (this == &other) {
return *this;
}
// Clear cards
for (Card* card : cards) {
delete card;
}
cards.clear();
// Deep copy of the cards
for (Card* card : other.cards) {
this->cards.push_back(new Card(*card));
}
return *this;
}
// Stream insertion operator
std::ostream& operator<<(std::ostream& os, Deck& deck) {
os << "Deck Contents: \n";
for (Card* card : deck.cards) {
os << *card << std::endl;
}
return os;
}
// Put card from deck to hand
void Deck::draw(Hand& hand) {
if (cards.empty()) {//deck is empty
std::cout << "Deck is empty" << std::endl;
return;
}
Card* drawn = new Card(*cards.front());
hand.cards.push_back(drawn);//add card to hand before removing from deck
cards[0] = nullptr;
cards.erase(cards.begin());
}
//-----------------------------------------------------HAND-------------------------------------------------------------------------
// Constructor
Hand::Hand() {
}
// Copy Constructor
Hand::Hand(Hand& other) {
for (Card* card : other.cards) {
this->cards.push_back(new Card(*card));
}
}
// Assignment operator
Hand& Hand::operator=(Hand& other) {
if (this == &other) {
return *this;
}
// Clear hand cards
for (Card* card : cards) {
delete card;
}
cards.clear();
// Deep copy
for (Card* card : other.cards) {
this->cards.push_back(new Card(*card));
}
return *this;
}
// Stream insertion operator
std::ostream& operator<<(std::ostream& os, Hand& hand) {
os << "Hand Contents: \n";
for (Card* card : hand.cards) {
os << *card << std::endl;
}
return os;
}