Jan 95 Challenge
Volume Number: 11
Issue Number: 1
Column Tag: Programmer’s Challenge
Programmer’s Challenge 
By Mike Scanlin, Mountain View, CA
Note: Source code files accompanying article are located on MacTech CD-ROM orsource code disks.
Poker Hand Evaluator
This month’s challenge was suggested by Chris Derossi (Mountain View, CA). The
goal is to compare two poker hands and determine which is higher. Your routine will be
given two hands of 7 cards each. It will have to make the best 5 card hand it can from
each and return the two 5-card hands as well as which is higher.
Here is how poker hands rank (from lowest to highest, with an example of each in
parentheses):
one pair (5, 5, *, *, *)
two pair (5, 5, 8, 8, *)
three of a kind (5, 5, 5, *, *)
straight (5, 6, 7, 8, 9)
flush (club, club, club, club, club)
full house (5, 5, 5, 8, 8)
four of a kind (5, 5, 5, 5, *)
straight flush (5, 6, 7, 8, 9; all clubs)
five of a kind (5, 5, 5, 5, wildCard)
The prototype of the function you write is:
typedef unsigned char Card;
typedef SevenCardHand {
Card cards[7];
} SevenCardHand;
typedef FiveCardHand {
Card cards[5];
} FiveCardHand;
short
ComparePokerHands(hand1Ptr, hand2Ptr,
best1Ptr, best2Ptr,
wildCardAllowed, wildCard,
straightsAndFlushesValid,
privateDataPtr)
SevenCardHand *hand1Ptr;
SevenCardHand *hand2Ptr;
FiveCardHand *best1Ptr;
FiveCardHand *best2Ptr;
Boolean wildCardAllowed;
Card wildCard;
Boolean straightsAndFlushesValid;
void *privateDataPtr;
A Card is a byte value (unsigned char) from 0 to 51 where 0 represents the 2 of
clubs, 9 is the jack of clubs, 12 is the ace of clubs, 13 is the 2 of diamonds, 26 is the
2 of hearts, 39 is the 2 of spades and 51 is the ace of spades.
The inputs are two SevenCardHands (from the same deck; you won’t get duplicate
Cards). Your routine should make the highest hand possible with 5 of the 7 cards and
store the resulting hand in the two FiveCardHands. It should then return one of the
following values: -1 if hand 1 is higher than hand 2, 0 if the hands are tied and 1 if
hand 2 is higher than hand 1. Hands can be tied because suit counts for nothing when
ranking hands. Aces can be high or low (whichever makes the resulting hand better).
WildCardAllowed is true if wild cards are allowed and false if not. If they are
allowed then wildCard will be the card that is wild, from 0 to 12. All suits of that care
are wild. For example, if wildCard is 4 then all 6’s are wild (Card values 4, 17, 30
and 43).
StraightsAndFlushesValid is true if straights and flushes are to be counted in the
ranking. If it is false then straights and flushes do not count for anything (they are low
hands).
PrivateDataPtr is the value returned by your Init routine, which is not timed,
whose prototype is:
void *
ComparePokerHandsInit(wildCardAllowed, wildCard,
straightsAndFlushesValid)
Boolean wildCardAllowed;
Card wildCard;
Boolean straightsAndFlushesValid;
You can allocate up to 1MB of memory in your Init routine (in case you want to
generate some lookup tables). The pointer you return will be passed to your
ComparePokerHands routine.
E-mail me if you have any questions. Have fun.
Two Months Ago Winner
I had to disqualify two of the eight entries I received for the Huffman Decoding
challenge because of incorrect results. Congratulations to Challenge Champion Bob
Boonstra (Westford, MA) for earning his fifth win. The top four entrants each
optimized their solutions for those cases where there was extra memory available.
Greg McKaskle (Austin, TX) had a very strong showing for the extra memory case
but his very-little-extra-memory case code came in 3rd place, preventing him from
winning overall.
Here are the times and code sizes for each entry. Numbers in parens after a
person’s name indicate how many times that person has finished in the top 5 places of
all previous Programmer Challenges, not including this one:
Name 256K time 8K time code
Bob Boonstra (12) 12 42 2308
Greg McKaskle 11 113 2012
John Schlack (1) 28 55 1470
Wolfgang Thaller (age 13) 40 929 1090
Allen Stenger (7) 103 103 440
Peter Hance 1211 1211 188
From reading the winning code you may notice that even a master such as Bob has
picked up at least one trick from studying previous Challenge winners. He chose to
borrow the ‘switch-do-while’ idea from Bill Karsh’s SwapBytes entry (a neat trick,
indeed). Glad to see it. After all, this column is meant to be educational (by teaching
tricks by example) as much as it is a contest.
I’ve been getting more requests than usual to have access to the current Challenge