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
|
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
class Card {
public:
Card();
void init(int suite, int card);
int getSuit() { return suite; }
int getCard() { return card; }
void clean() { delete this; }
private:
int suite;
int card;
};
Card::Card() {}
void Card::init(int suite, int card) {
Card::suite = suite;
Card::card = card;
}
int main()
{
int suites[4] = {1,2,3,4};
int cards[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
vector<Card*> deck;
vector<Card*> shuffledDeck;
for(int i = 0; i <= 3; i++) {
for(int j = 0; j <= 12; j++) {
Card* newCard;
newCard = new Card();
newCard->init(suites[i], cards[j]);
deck.push_back(newCard);
}
}
cout << " Unshuffled deck: " << endl;
for(unsigned int i = 0; i < deck.size(); i++) {
cout << deck[i]->getSuit() << " " << deck[i]->getCard() << endl;
}
cout << " Shuffle deck " << endl;
bool shuffling = true;
int newCard;
srand(time(NULL));
while(shuffling) {
newCard = rand() % 52;// 0 - 51
//if(deck[newCard] == NULL) continue;
bool cardExists = false;
if(!shuffledDeck.empty()) {
for(int i = 0; i <= shuffledDeck.size(); i++) {
if(shuffledDeck[i] == deck[newCard]) {
cardExists = true;
break;
}
}
}
if(cardExists) continue;
//cout << "attempting to push back deck[" << newCard << "] " << deck[newCard] << endl;
shuffledDeck.push_back(deck[newCard]);
if(shuffledDeck.size() == deck.size())
shuffling = false;
}
cout << endl << " sizes: " << deck.size() << " " << shuffledDeck.size() << endl;
cout << endl << "Show Shuffled Deck " << endl;
for(unsigned int i = 0; i <= shuffledDeck.size(); i++) {
if(shuffledDeck[i] == NULL) continue;
cout << shuffledDeck[i]->getSuit() << " " << shuffledDeck[i]->getCard() << endl;
}
while(!shuffledDeck.empty()) {
shuffledDeck.back()->clean();
shuffledDeck.pop_back();
}
while(!deck.empty()) {
deck.back()->clean();
deck.pop_back();
}
return 0;
}
|