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
|
#include<iostream>
using namespace std;
#include<vector>;
class CardDeck
{
public:
CardDeck(); // default constructor
~CardDeck(); // destructor
/*The constructor should create 52 cards individually (no loops)
using the new operator (dynamic memory) and add them to the deck vector*/
CardDeck::CardDeck(){
//HEART suit
deck.push_back(new Card(ACE, HEARTS));
deck.push_back(new Card(TWO, HEARTS));
deck.push_back(new Card(THREE, HEARTS));
deck.push_back(new Card(FOUR, HEARTS));
deck.push_back(new Card(FIVE, HEARTS));
deck.push_back(new Card(SIX, HEARTS));
deck.push_back(new Card(SEVEN, HEARTS));
deck.push_back(new Card(EIGHT, HEARTS));
deck.push_back(new Card(NINE, HEARTS));
deck.push_back(new Card(TEN, HEARTS));
deck.push_back(new Card(JACK, HEARTS));
deck.push_back(new Card(QUEEN, HEARTS));
deck.push_back(new Card(KING, HEARTS));
//CLUBS suit
deck.push_back(new Card(ACE, CLUBS));
deck.push_back(new Card(TWO, CLUBS));
deck.push_back(new Card(THREE, CLUBS));
deck.push_back(new Card(FOUR, CLUBS));
deck.push_back(new Card(FIVE, CLUBS));
deck.push_back(new Card(SIX, CLUBS));
deck.push_back(new Card(SEVEN, CLUBS));
deck.push_back(new Card(EIGHT, CLUBS));
deck.push_back(new Card(NINE, CLUBS));
deck.push_back(new Card(TEN, CLUBS));
deck.push_back(new Card(JACK, CLUBS));
deck.push_back(new Card(QUEEN, CLUBS));
deck.push_back(new Card(KING, CLUBS));
//SPADES suit
deck.push_back(new Card(ACE, SPADES));
deck.push_back(new Card(TWO, SPADES));
deck.push_back(new Card(THREE, SPADES));
deck.push_back(new Card(FOUR, SPADES));
deck.push_back(new Card(FIVE, SPADES));
deck.push_back(new Card(SIX, SPADES));
deck.push_back(new Card(SEVEN, SPADES));
deck.push_back(new Card(EIGHT, SPADES));
deck.push_back(new Card(NINE, SPADES));
deck.push_back(new Card(TEN, SPADES));
deck.push_back(new Card(JACK, SPADES));
deck.push_back(new Card(QUEEN, SPADES));
deck.push_back(new Card(KING, SPADES));
//DIAMONDS suit
deck.push_back(new Card(ACE, DIAMONDS));
deck.push_back(new Card(TWO, DIAMONDS));
deck.push_back(new Card(THREE, DIAMONDS));
deck.push_back(new Card(FOUR, DIAMONDS));
deck.push_back(new Card(FIVE, DIAMONDS));
deck.push_back(new Card(SIX, DIAMONDS));
deck.push_back(new Card(SEVEN, DIAMONDS));
deck.push_back(new Card(EIGHT, DIAMONDS));
deck.push_back(new Card(NINE, DIAMONDS));
deck.push_back(new Card(TEN, DIAMONDS));
deck.push_back(new Card(JACK, DIAMONDS));
deck.push_back(new Card(QUEEN, DIAMONDS));
deck.push_back(new Card(KING, DIAMONDS));
}
CardDeck::~CardDeck(){
//while deck is not empty
while(deck.back > 0)
{ delete Card;
}
//call delete on the last pointer in the deck to free the card
//remove that pointer from the deck vector
}
/* Returns the Card pointer at the back of the vector or NULL if the deck is empty. */
Card* getTopCard();
Card*CardDeck::getTopCard();
//need a local card* variable to hold the pointer to the top of the deck call it top
//set it to null
//if deck is not empty
//store last card* in deck into top
//remove that pointer from deck vector
//return top
int * top = deck.front;
/* Prints each card, one per line, using the Card's toString() member function. */
void print()const;
void CardDeck::print()const
{
/*loop through the deck
for each element of deck
dereference card pointer
print out ots tostring()
*/
//loop to dereference card pointer.
}
/* Shuffles the deck of cards by swapping two random elements of the deck. */
void shuffle();
void CardDeck::shuffle()
{
/*1000 times
swap 2 random elements of vector
*/
}
private:
vector<Card*> deck;
/* Swaps the contents of the elements of deck at the given indices. */
void swapCards(int index1, int index2);
};
//The Card class has already been defined for you:
enum Suit {HEARTS, CLUBS, SPADES, DIAMONDS};
enum Rank {ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
class Card{
public:
Card(Rank r, Suit s);
Rank getRank()const;
Suit getSuit()const;
string toString()const;
private:
Suit suit;
Rank rank;
};
Card::Card(Rank r, Suit s){
suit = s;
rank = r;
}
Rank Card::getRank()const{
return rank;
}
Suit Card::getSuit()const{
return suit;
}
string Card::toString()const{
string description = "";
switch(rank) {
case ACE:
description += "Ace";
break;
case TWO:
description += "Two";
break;
case THREE:
description += "Three";
break;
case FOUR:
description += "Four";
break;
case FIVE:
description += "Five";
break;
case SIX:
description += "Six";
break;
case SEVEN:
description += "Seven";
break;
case EIGHT:
description += "Eight";
break;
case NINE:
description += "Nine";
break;
case TEN:
description += "Ten";
break;
case JACK:
description += "Jack";
break;
case QUEEN:
description += "Queen";
break;
case KING:
description += "King";
break;
}
description += " of ";
switch(suit) {
case HEARTS:
description += "Hearts";
break;
case CLUBS:
description += "Clubs";
break;
case SPADES:
description += "Spades";
break;
case DIAMONDS:
description += "Diamonds";
break;
}
return description;
}
int main(){
CardDeck d;
d.print();
cout << "Shuffling..." << endl << endl;
d.shuffle();
d.print();
cout << endl << endl;
return 0;
}
|