Your situation requires a different approach than using strings to capture the suits and cards.
Your sorting order for the suits, "Hearts" > "Diamonds" > "Spades" >
"Clubs" is easy to implement using numbers but require unnecessary complication
when using strings.
Similarly for the cards.
My suggestion is to create enums that represent the suits and cards. Store those enums
for all computations. When it's time for printing messages, have a function that returns a string given a card.
enum Suit { HEARTS, DIAMONDS, SPADES, CLUBS};
enum Card { ACE, KING, QUEEN, JACK, TEN, NINE, EIGHT, SEVEN, SIX, FIVE, FOUR, THREE, TWO};
struct DeckCard
{
Suit suit;
Card card;
// Implement the < operator to make it easy to sort a deck of cards.
bool operator<(DeckCard const& rhs) const
{
if ( this->suit != rhs.suit )
{
return (this->suit < rhs.suit);
}
return (this->card < rhs.card);
}
std::string toString()
{
return suiteToString() + "-" + cardToString();
}
std::string suiteToString()
{
switch ( suit )
{
case HEARTS:
return "Hearts";
case DIAMONDS:
return "Diamonds";
case SPADES:
return "Spades";
case CLUBS:
return "Clubs";
default:
}
return "Unknown Suit";
}
std::string cardToString()
{
switch ( card )
{
case ACE:
return "Ace";
case KING:
return "King";
case QUEEN:
return "Queen";
case JACK:
return "Jack";
case TEN:
return "10";
case NINE:
return "9";
case EIGHT:
return "8";
case SEVEN:
return "7";
case SIX:
return "6";
case FIVE:
return "5";
case FOUR:
return "4";
case THREE:
return "3";
case TWO:
return "2";
}
return "Unknwon Card";
}
};
DeckCard deck[4*13];
for (int i = 0; i<4; i++)
{
for (int j=0; j<13 ; j++)
{
deck[i*13+j].suit = type[i];
deck[i*13+j].card = cards[j];
cout << deck[i*13+j].toString() << " ";
}
cout << endl;
}
// Sort the cards.
std::sort(deck[0], deck[51]);
You can also shuffle your cards using std::shuffle