Before I proceed, I want you to keep in mind that the type of card game that I programming is stud poker.
So right now I have this card structure that hold four datatype
Code:
struct Card
{
const char *suit;
const char *face;
const int *faceValue; /*This holds the ranking of the face*/
const int *suitValue; /*This holds the ranking of the suit*/
};
Note: In my ranking of suits, club is the lowest(1), then diamond(2), hearts(3) and finally spade(4).
Next I have my deck structure
Code:
struct Deck
{
int numOfCards; /*the current number of cards in a deck*/
struct Card card[MAXCARDS]; /*An array of 52 card structure to represent each unique card.*/
};
Note: Initially ace had a face value of 1 but then I changed it to 14(13 is king basically) because is usually the highest card in any game. Whether it is easier to make the face value of ace a 1 or 14 I simply do not know.
Then finally comes my player structure.
Code:
enum CardRanking
{
HIGHCARD = 1, PAIR, TWOPAIRS, THREEOFKIND, STRAIGHT,
FLUSH, FULLHOUSE, FOUROFKIND, STRAIGHTFLUSH, ROYALFLUSH
};
struct Players
{
int cardRanking; /*A variable that ranks their hand. 10 being a royal flush and 1 being a high card.*/
struct Card *hand; /*A pointer structure that points to the current cards*/
};
These four functions are what I use to rank the player's hand
Code:
int rankHands(struct Card *hand, const int CARDSPERHAND)
{
int numOfPairs = 0, kind = 0, straight = 0, flush = 0;
isFlush(hand, &flush, CARDSPERHAND);
isStraight(hand, &straight, CARDSPERHAND);
if(straight == TRUE && flush == TRUE)
{
return STRAIGHTFLUSH;
}
pairsAndKinds(hand, &numOfPairs, &kind, CARDSPERHAND);
if(kind == FOUROFKIND)
{
return FOUROFKIND;
}
else if(numOfPairs == LENGTHOFPAIR && kind == THREEOFKIND)
{
return FULLHOUSE;
}
else if(flush)
{
return FLUSH;
}
else if(straight)
{
return STRAIGHT;
}
else if(kind == THREEOFKIND)
{
return THREEOFKIND;
}
else if(numOfPairs == LENGTHOFTWOPAIRS)
{
return TWOPAIRS;
}
else if(numOfPairs == LENGTHOFPAIR)
{
return PAIR;
}
else
return HIGHCARD;
}
void isFlush(struct Card *hand, int *flush, const int CARDSPERHAND)
{
int index, isSameSuit = TRUE;
const int *suitRank1 = hand[0].suitValue;
for(index = 1; index < CARDSPERHAND && isSameSuit; index++)
{
const int *suitRank2 = hand[index].suitValue;
if((*suitRank1) != (*suitRank2))
{
isSameSuit = FALSE;
}
}
if(isSameSuit)
{
*flush = TRUE;
}
}
void isStraight(struct Card *hand, int *straight, const int CARDSPERHAND)
{
int index, isConsecutive = TRUE, gap = 1;
const int *faceRank1 = hand[0].faceValue;
for(index = 1; index < CARDSPERHAND && isConsecutive; index++, gap++)
{
const int *faceRank2 = hand[index].faceValue;
if((*faceRank1 + gap) != (*faceRank2))
{
isConsecutive = FALSE;
}
}
if(isConsecutive)
{
*straight = TRUE;
}
}
void pairsAndKinds(struct Card *hand, int *numOfPairs, int *kind, const int CARDSPERHAND)
{
int index, counter = 0;
const int *faceRank1 = hand[0].faceValue;
for(index = 1; index < CARDSPERHAND; index++)
{
const int *faceRank2 = hand[index].faceValue;
if((*faceRank1) == (*faceRank2))
{
counter++;
}
else
{
if(counter == LENGTHOFPAIR)
{
*numOfPairs = *numOfPairs + 1;
}
else if(counter == LENGTHOFTHREE)
{
*kind = THREEOFKIND;
}
else if(counter == LENGTHOFFOUR)
{
*kind = FOUROFKIND;
}
counter = 0;
faceRank1 = hand[index].faceValue;
}
}
}
Now the main problem is with the ace card. Ace has the possibility of being a low card and a high card. So the way that my straight function works is it checks if the current card + 1 == the next card because the array is sorted prior to doing this. The works fine if we have a high straight(10, 11, 12, 13, 14)(Note: 11 is jack, 12 is queen, 13 is king and 14 is ace). This does not work with a low straight 14 2 3 4 5. As you can see, ace is not plus 1 to the next card.
So what can I do to resolve this dilemma?