-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCard.cpp
190 lines (160 loc) · 4.99 KB
/
Card.cpp
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
//
// Created by Main on 2021-09-29.
//
#include "Card.h"
//creating card constructors
Card::Card(){
cout << "No Type specified please try again" << endl;
};
//basic constructor
Card::Card(string t) : cardType(t){
};
//copy constructor
Card::Card(Card const ©Card) {
cardType = copyCard.cardType;
}
//returns the card's type
string Card::getType() const{
return cardType;
};
//string insertion operators
std::ostream &operator<<(std::ostream &out, const Card &c) {
out << "{ Card Type: " << c.cardType << "}" << std::endl;
return out;
}
//assignment operator
Card& Card::operator =(const Card &c) {
this->cardType = c.cardType;
return *this;
}
//creating deck constructors
Deck::Deck(){
};
//copy constructor
Deck::Deck(const Deck ©Deck) {
cardsHeld = copyDeck.cardsHeld;
};
//creates a deck of cards in relation to the number of card (prof didnt specify the number of cards in a deck, can be modified as needed)
Deck::Deck(int numPlayers){
cardsHeld.clear();
for(int i = 0; i < numPlayers; i++)
{
cardsHeld.push_back(new Card( "Bomb"));
cardsHeld.push_back(new Card( "Reinforcement"));
cardsHeld.push_back(new Card( "Blockade"));
cardsHeld.push_back(new Card( "Airlift"));
cardsHeld.push_back(new Card( "Diplomacy"));
}
};
//destructor for the deck
Deck::~Deck() {
for(int i = 0; i < cardsHeld.size(); i++)
delete cardsHeld.at(i);
};
//adds a card to a deck
void Deck::ReceiveCard(Card *c)
{
cardsHeld.push_back(c);
};
//Will take a random card from the deck and add it to the specified hand
void Deck::Draw(Hand *playerHand)
{
srand((unsigned) time(0));
int deckPosition = (rand() % this->cardsHeld.size());
Card *dealtCard = new Card(*cardsHeld.at(deckPosition));
playerHand->ReceiveCard(dealtCard);
this->cardsHeld.erase(this->cardsHeld.begin() + deckPosition);
};
//string insertion operator
std::ostream &operator<<(std::ostream &out, const Deck &showDeck) {
out << "The deck has" << endl;
for(int i = 0; i < showDeck.cardsHeld.size(); i++)
{
out << "Card " << (i+1) << " is of type " << showDeck.cardsHeld.at(i)->getType() << endl;
}
return out;
}
//assignment operator
Deck& Deck::operator =(const Deck &d) {
this->cardsHeld = d.cardsHeld;
return *this;
}
//creating Hand constructors
Hand::Hand() = default;
//Hand constructor that takes in a vector of cards held
Hand::Hand(vector<Card*> cardsHeld) {
this->cardsHeld = cardsHeld;
}
//copy constructor
Hand::Hand(const Hand ©Hand) {
cardsHeld = copyHand.cardsHeld;
};
//destructor for the deck
Hand::~Hand() {
for(int i = 0; i < cardsHeld.size(); i++)
delete cardsHeld.at(i);
}
//add card to hand
void Hand::ReceiveCard(Card *c) {
this->cardsHeld.push_back(c);
};
//Gets the index of the card based on the type of card passed in
int Hand::getCardIndex(string playerCardType) {
for (int i = 0; i < cardsHeld.size(); i++) {
if (playerCardType.compare(cardsHeld.at(i)->cardType) == 0) {
return i;
}
}
return -1;
}
//Removes the card based on the passed in index
void Hand::removeCard(int index) {
cardsHeld.erase(cardsHeld.begin() + index);
}
//Checks to see if the card is owned by the player
bool Hand::isCardOwned(string playerCardType) {
for (int i = 0; i < this->cardsHeld.size(); ++i) {
if (this->cardsHeld.at(i)->cardType.compare(playerCardType) == 0) {
return true;
}
}
return false;
}
//displays all cards in the hand, user can then choose a card to play, the card is then returned to the deck
OrdersList Hand::Play(Deck *mainDeck, OrdersList *o) {
cout << *this << endl;
cout << "Choose the card you wish to play (enter in the form of a number)";
int chosenCard;
cin >> chosenCard;
chosenCard--;
Card *playedCard = new Card(*this->cardsHeld.at(chosenCard));
mainDeck->ReceiveCard(playedCard);
this->cardsHeld.erase(this->cardsHeld.begin() + chosenCard);
//Orders newOrder;
if(this->cardsHeld.at(chosenCard)->cardType.compare("Bomb")){
o->addBomb(new Bomb());
} else if(this->cardsHeld.at(chosenCard)->cardType.compare("Reinforcement")){
o->addAdvance(new Advance());
} else if(this->cardsHeld.at(chosenCard)->cardType.compare("Blockade")){
o->addBlockade(new Blockade());
} else if(this->cardsHeld.at(chosenCard)->cardType.compare("Airlift")){
o->addAirlift(new Airlift());
} else if(this->cardsHeld.at(chosenCard)->cardType.compare("Diplomacy")){
o->addNegotiate(new Negotiate());
}
return *o;
};
//string insertion operator
std::ostream &operator<<(std::ostream &out, const Hand &showHand) {
cout << "The hand has " << endl;
for(int i = 0; i < showHand.cardsHeld.size(); i++)
{
cout << "Card " << (i+1) << " is of type " << showHand.cardsHeld.at(i)->getType() << endl;
}
return out;
}
//assignment operator
Hand& Hand::operator =(const Hand &h) {
this->cardsHeld = h.cardsHeld;
return *this;
}