Well... here I am, making a Card class, thinking I'll make yet another useless poker game or something. Making a card class is easy. Just put a couple of variables in there, some accessors, and throw in some comparison methods or something.
What's hard is using it to make a poker game. My thinking was: "Okay, I'll make a Card class, then I'll rank hands in a simple top-down order or something." You know, check for a royal flush first, then 4 of a kind, etc. Sounds simple enough, right?
Wrong. At least, it's not as simple as I thought it would be. Maybe I'm just too lazy to get down and dirty. Here's my file, card.h (actually card.0.1.h because I added a couple of things). I made a few efforts for my top-down checking method, but none of them work. Any ideas?
Code:
#include <string.h>
#define ASCII char
class Card;
void GetCardName (ASCII *, Card*);
class Card{
public:
Card();
Card(int, int);
~Card() { }
int GetSuit() const { return itsSuit;}
int GetValue() const { return itsValue; }
int Compare(const Card&) const;
static int cCompare(const Card*, const Card*);
private:
int itsSuit;
int itsValue;
};
Card::Card():
itsSuit(1),
itsValue(1)
{ }
Card::Card(int suit, int value):
itsSuit(suit),
itsValue(value)
{ }
int Card::Compare(const Card& rhs) const{
int thisValue = this->GetValue();
int thatValue = rhs.GetValue();
if ( (thisValue == 1) && (thatValue != 1) )
return 1;
if ( thisValue > thatValue )
return 1;
if ( thisValue < thatValue )
return 2;
if ( thisValue == thatValue )
return 0;
return -1;
}
int Card::cCompare(const Card* lhs, const Card* rhs){
int thisValue = lhs->GetValue();
int thatValue = rhs->GetValue();
if ( (thisValue == 1) && (thatValue != 1) )
return 1;
if( thisValue > thatValue )
return 1;
if ( thisValue < thatValue )
return 2;
if ( thisValue == thatValue )
return 0;
return -1;
}
void GetCardName (ASCII * retVal, Card* theCard){
switch (theCard->GetValue()) {
case 1: strcat(retVal, "Ace "); break;
case 2: strcat(retVal, "Two "); break;
case 3: strcat(retVal, "Three "); break;
case 4: strcat(retVal, "Four "); break;
case 5: strcat(retVal, "Five "); break;
case 6: strcat(retVal, "Six "); break;
case 7: strcat(retVal, "Seven "); break;
case 8: strcat(retVal, "Eight "); break;
case 9: strcat(retVal, "Nine "); break;
case 10: strcat(retVal, "Ten "); break;
case 11: strcat(retVal, "Jack "); break;
case 12: strcat(retVal, "Queen "); break;
case 13: strcat(retVal, "King "); break;
default: strcat(retVal, "Ace "); break;
}
strcat(retVal, "of ");
switch (theCard->GetSuit()) {
case 1: strcat(retVal, "Spades"); break;
case 2: strcat(retVal, "Clubs"); break;
case 3: strcat(retVal, "Diamonds"); break;
case 4: strcat(retVal, "Hearts"); break;
}
}
Quick edit: Oh, yeah, I suppose it would be helpful to know the Comparison methods are meant to return a zero if the cards are ranked as equal, 1 if the first card (either this or the lhs) is greater than rhs, 2 if rhs is greater than the first card (either this or the lhs), or -1 if none of these conditions are met (which shouldn't happen).