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
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
enum c_rank{ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE,
TEN, JACK, QUEEN, KING, ENDRANK};
enum suit{ CLUBS, DIAMONDS, HEARTS, SPADES, ENDSUIT }; // there's got be a better way to iterate
// through these enums
c_rank operator++(c_rank& r, int){ // allow iteration through c_rank enums
int i;
if (r == ENDRANK)
return r;
i = r;
i++;
return r = static_cast<c_rank>(i);
}
suit operator++(suit& s, int){ // allow iteration through suit enums
int i;
if (s == ENDSUIT)
return s;
i = s;
i++;
return s = static_cast<suit>(i);
}
class Card{
int m_value;
c_rank m_Rank; // I had to use c_rank instead of rank
// If I used rank, it would show as "ambiguous"
// down here.
suit m_Suit;
//char m_name; // This isn't needed
public:
Card(c_rank r, suit s);
friend ostream& operator<<(ostream& os, const Card& aCard);
};
Card::Card(c_rank r, suit s) : m_Rank(r), m_Suit(s){
m_value = static_cast<int>(r);
m_value = m_value > 9 ? 10 : m_value;
}
class Shoe{
public:
vector<Card>m_Shoe;
Shoe(int numdecks = 6);
void shauffleShoe();
void displayShoe();
};
Shoe::Shoe( int numdecks){
for (int i = 0; i < numdecks;i++)
for (c_rank r = ACE; r < ENDRANK; r++)
for (suit s = CLUBS; s <ENDSUIT; s++){
Card curcard(r, s);
m_Shoe.push_back(curcard);
}
}
void Shoe::displayShoe(){
int i = 0;
for (vector<Card>::iterator iter = m_Shoe.begin();
iter != m_Shoe.end(); iter++){
cout << *iter << " ";
if (i++ == 25){
cout << '\n';
i = 0;
}
}
}
void Shoe::shauffleShoe(){
random_shuffle(m_Shoe.begin(), m_Shoe.end());
}
ostream& operator<<(ostream& os, const Card& aCard){
char name[] = { 'X', 'A', '2', '3', '4', '5', '6', '7', '8',
'9', 'T', 'J', 'Q', 'K' }; // quick and dirty avoid indice math
char suit[] = { 'C', 'D', 'H', 'S' };
os << name[aCard.m_Rank]<< suit[aCard.m_Suit];
return os;
}
int main(){
Shoe myshoe;
myshoe.displayShoe();
myshoe.shauffleShoe();
cout << '\n';
myshoe.displayShoe();
return 0;
}
|