Jacks or Better Program

Started by
19 comments, last by JohnBolton 18 years ago
Quote:Original post by Danikar
Yeah if u want to send that to me that would be cool. I always like looking over other peoples code.


I PM'd it to ya. hope it helps.
Regards,nomichi
Advertisement
Quote:Original post by Lothia
Tan that confuses the hell out of me. How does that prove they are flush if 5 is the highest? Could it not be you have, Ace, ace, Queen, Jack, and 10. And that 10 would make it a 5. Or maybe i just get the concept. It seems that you are doing a check high and low, but the low is really the high (ace) and ur high is really your low (two) Maybe it's the enum.. I have never heard of it.


that's checking the SUITS array, not the RANKS array. if you get 5 cards of the same suit, it MUST be a flush or a straight flush, and the ranks array does not matter, as it will have no number higher than 1 in it, provided a single deck.

also, i set the initial value for the high value to the lowest value and the initial value for the low value to the highest value because it guarantees that it will be set to the correct value while the cards are being evaluated.

i'm going to be going home shortly, and I'll write up code that'll do this....

Get off my lawn!

Thank you Tan. That would be great to see the code, and now I actually get that part. I guess I was getting confused for something really trivial.
It'll compile, but it hasn't been tested. Provided as-is, to be use by whomever for whatever. Corrections for deficiencies welcome.

enum rank_t{	RANK_TWO,	RANK_THREE,	RANK_FOUR,	RANK_FIVE,	RANK_SIX,	RANK_SEVEN,	RANK_EIGHT,	RANK_NINE,	RANK_TEN,	RANK_JACK,	RANK_QUEEN,	RANK_KING,	RANK_ACE,	RANK_COUNT,	RANK_FIRST=RANK_TWO};enum suit_t{	SUIT_CLUBS,	SUIT_HEARTS,	SUIT_SPADES,	SUIT_DIAMONDS,	SUIT_COUNT,	SUIT_FIRST=SUIT_CLUBS};enum handvalue_t{	HANDVALUE_INVALID,	HANDVALUE_JUNK,	HANDVALUE_ONEPAIR,	HANDVALUE_ONEPAIRJACKSORBETTER,	HANDVALUE_TWOPAIR,	HANDVALUE_THREEOFAKIND,	HANDVALUE_STRAIGHT,	HANDVALUE_FLUSH,	HANDVALUE_FULLHOUSE,	HANDVALUE_FOUROFAKIND,	HANDVALUE_STRAIGHTFLUSH,	HANDVALUE_ROYALFLUSH,};enum{	HAND_CARDCOUNT = 5};struct card_t{	rank_t rank;	suit_t suit;};struct hand_t{	card_t cards[HAND_CARDCOUNT];};handvalue_t getHandValue(const hand_t& theHand){	handvalue_t result=HANDVALUE_INVALID;		int cardRanks[RANK_COUNT]={0};	int cardSuits[SUIT_COUNT]={0};	rank_t rankHigh=RANK_TWO;	rank_t rankLow=RANK_ACE;	int validCards=0;	for(int cardIndex=0;cardIndex<HAND_CARDCOUNT;++cardIndex)	{		bool cardIsValid=false;		if((theHand.cards[cardIndex].rank<RANK_COUNT)&&(theHand.cards[cardIndex].suit<SUIT_COUNT))		{			bool foundDuplicate=false;			for(int otherCardIndex=cardIndex+1;otherCardIndex<HAND_CARDCOUNT;++otherCardIndex)			{				if((theHand.cards[cardIndex].rank==theHand.cards[otherCardIndex].rank)&&(theHand.cards[cardIndex].suit==theHand.cards[otherCardIndex].suit))				{					foundDuplicate=true;				}				if(!foundDuplicate)				{					cardIsValid=true;				}			}		}		if(cardIsValid)		{			validCards++;			cardRanks[theHand.cards[cardIndex].rank]++;			cardSuits[theHand.cards[cardIndex].suit]++;			if(theHand.cards[cardIndex].rank>rankHigh)			{				rankHigh=theHand.cards[cardIndex].rank;			}			if(theHand.cards[cardIndex].rank<rankLow)			{				rankLow=theHand.cards[cardIndex].rank;			}		}	}	if(HAND_CARDCOUNT==validCards)	{		for(int suitIndex=SUIT_FIRST;(result==HANDVALUE_INVALID)&&(suitIndex<SUIT_COUNT);++suitIndex)		{			if(HAND_CARDCOUNT==cardSuits[suitIndex])			{				result=HANDVALUE_FLUSH;			}		}		if(HANDVALUE_FLUSH==result)		{			if((HAND_CARDCOUNT-1)==(rankHigh-rankLow))			{				if(cardRanks[RANK_ACE])				{					result=HANDVALUE_ROYALFLUSH;				}				else				{					result=HANDVALUE_STRAIGHTFLUSH;				}			}			else if(cardRanks[RANK_TWO]&&cardRanks[RANK_THREE]&&cardRanks[RANK_FOUR]&&cardRanks[RANK_FIVE]&&cardRanks[RANK_ACE])			{				result=HANDVALUE_STRAIGHTFLUSH;			}		}		else		{			bool fourOfAKind=false;			bool threeOfAKind=false;			int pairCount=0;			bool jacksOrBetter=false;			for(int rankIndex=0;(!fourOfAKind)&&(rankIndex<RANK_COUNT);++rankIndex)			{				switch(cardRanks[rankIndex])				{				case 4:					{						fourOfAKind=true;					}break;				case 3:					{						threeOfAKind=true;					}break;				case 2:					{						pairCount++;						if(rankIndex>=RANK_JACK)						{							jacksOrBetter=true;						}					}break;				}			}			if(fourOfAKind)			{				result=HANDVALUE_FOUROFAKIND;			}			else if(threeOfAKind)			{				if(pairCount)				{					result=HANDVALUE_FULLHOUSE;				}				else				{					result=HANDVALUE_THREEOFAKIND;				}			}			else			{				switch(pairCount)				{				case 2:					{						result=HANDVALUE_TWOPAIR;					}break;				case 1:					{						if(jacksOrBetter)						{							result=HANDVALUE_ONEPAIRJACKSORBETTER;						}						else						{							result=HANDVALUE_ONEPAIR;						}					}break;				case 0:					{						if((HAND_CARDCOUNT-1)==(rankHigh-rankLow))						{							result=HANDVALUE_STRAIGHT;						}						else if(cardRanks[RANK_TWO]&&cardRanks[RANK_THREE]&&cardRanks[RANK_FOUR]&&cardRanks[RANK_FIVE]&&cardRanks[RANK_ACE])						{							result=HANDVALUE_STRAIGHT;						}						else						{							result=HANDVALUE_JUNK;						}					}break;				}			}		}	}	return(result);}

Get off my lawn!

just wondering why do you check if each card is valid first? wouldnt the deal function assure that the player is only handed valid cards?
Always assume invalid input until you are certain otherwise...

Get off my lawn!

Can you explain what a enum is? It seems to me its a struct.
Heres a little thing you should try which you might find interesting

for(char i = 3; i<7; i++)
cout << i;


whoa wasnt that cool? Now to make it easy for you

string typestr = string(char(3));

Quote:Original post by Lothia
Can you explain what a enum is? It seems to me its a struct.


enum types in C++

Get off my lawn!

here's another good enum description/tutorial cplusplus.com. I use this site whenever I want to look up anything related to c++.

This topic is closed to new replies.

Advertisement