Help with sorting hand of playing cards
Hi Guys,
I have all my code working except sort function .
I need to sort the cards in hand......and I am having hard time with that....
Any help would be really helpful ;)
#include <algorithm>
#include <iostream>
#include <string>
#include <stdexcept>
#include <exception>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cstdlib>
using namespace std;
struct acard{
public:
int suit;
int value;
string str;
unsigned int index;
bool operator <(const acard& rhs)const{
return index < rhs.index;
}
};
class hand{
private:
string theSuits; // contains suits in order
string theValues; // contains values in order
int numcards;
public:
unsigned size;
acard* handcards; // an array of cards
hand();
~hand();
void sortit( unsigned size);
void show();
void shuffle();
};
hand::hand():
//theSuits("\x05\x04\x03\x06")
theSuits("\C\D\H\S")
,theValues("23456789TJKQA")
,numcards(size)
,handcards(new acard[numcards])
{
}
hand::~hand(){
delete [] handcards;
}
class deck{
private:
string theSuits; // contains suits in order
string theValues; // contains values in order
public:
int numcards;
acard* deckofcard; // an array of cards
deck();
~deck();
void show();
void hands(hand h, unsigned size);
void shuffle();
};
deck::deck():
//theSuits("\x05\x04\x03\x06") //c, d, h, s
theSuits("\C\D\H\S")
,theValues("23456789TJKQA")
,numcards(theSuits.length() * theValues.length())
,deckofcard(new acard[numcards])
{
int cardnum(0);
for(int i = 0; i < theSuits.length(); ++i){
for(int j = 0; j < theValues.length(); ++j){
cardnum = (i * theValues.length()) + j;
deckofcard[cardnum].suit = i;
deckofcard[cardnum].value = j;
deckofcard[cardnum].str = theValues.substr(j,1) +
theSuits.substr(i,1); }
}
}
deck::~deck(){
delete [] deckofcard;
}
void deck::show(){
for(int i=0; i < numcards; ++i){
cout << (i % 13 ? ' ' : '\n') << deckofcard[i].str ;
}
cout << endl;
cout << endl;
}
void deck::shuffle(){
for(int i=0; i < numcards; ++i){
deckofcard[i].index = rand();
}
sort(deckofcard, deckofcard + numcards); }
void deck::hands(hand h, unsigned size){
cout << endl;
for ( int i=0; i < size; ++i){
numcards--;
h.handcards[i].str = deckofcard[numcards].str;
cout << h.handcards[i].str << " " ; }
//sort(h.handcards, h.handcards + size);
cout << endl;
h.sortit( size);
}
void hand::sortit(unsigned size) {
sort(handcards, handcards + size);
for ( int i=0; i < size; ++i){
cout << h.handcards[i].str<< " " ; }
cout << endl;
}
int main()
{
int cardnum(0);
string h[5];
srand(time(0));
deck mydeck;
mydeck.show();
mydeck.shuffle();
mydeck.show();
hand myhand1;
hand myhand2;
hand myhand3;
hand myhand4;
mydeck.hands(myhand1, 6);
//myhand1.sortit(6);
mydeck.hands(myhand2, 6);
mydeck.hands(myhand3, 6);
mydeck.hands(myhand4, 6);
system("pause");
return 0;
}