Card game and simulation library and tools. Part of the OneJoker project.

Project maintained by Lee Daniel Crocker
Lee's blog is etceterology.

onejoker
CC-0: To the extent possible under law, I, Lee Daniel Crocker waive all copyright and related or neighboring rights to all creative works original to me.

Using the library in your C code

Poker functions

Poker hands are given a numeric value which is the rank of the equivalence class of the hand. This is the number 1 for a royal flush, 2 for a king-high straight flush, etc., all the way down to 7,462 for no pair 7-5-4-3-2. This number can be used to compare hands: a hand with a lower value defeats a hand with a higher value. Hands with the same value are tied.

int ojp_eval5(oj_cardlist *p)

Returns the rank of the given hand. This function only evaluates hands of exactly 5 cards, with no duplicate cards. It is highly opimized for speed and does no error checking.

int ojp_eval7(oj_cardlist *p)

Returns the same equivalence class number for the best 5-card hand out of the given 7-card hand. This is optimized for exactly 7 cards, a common case. Does not tell you which 5 cards made the best hand, only what the best hand evaluates to.

int ojp_best5(oj_cardlist *p, oj_cardlist *best)

Given the cardlist p of any size, copies into best the 5 cards that make up the best 5-card poker hand from p , and returns its value.

int ojp_hand_info(oj_poker_hand_info *pi, oj_cardlist *p, int val)

Fills the given oj_poker_hand_info structure with information about the hand useful for describing it and displaying it to humans. You can pass in the value of the hand in the argument val if you have already calculated it, or you can pass the value -1 to tell the function to calculate the value for you. Also re-orders the hand passed in for display, which must be writable.

Field Use
val The equivalence-class rank of the hand as described above.
group A number from 1 to 9 indicating the hand group. 1 is straight flush, 2 quads, 3 full house, etc., down to 9 for no pair.
nranks Number of ranks needed to compare hands within group. For example, straights need only 1 rank (the top card), flushes all 5, full houses 2.
ranks[] The actual ranks in order of significance.

If you pass the hand (Ks 4h 4d Kc 4s) to this function, for example, val will be set to 288, group will be 3, nranks 2, ranks[] will be OJR_FOUR, OJR_KING, and the hand itself will be reordered to (4c 4d 4h Kc Ks).

char *ojp_hand_description(oj_poker_hand_info *pi, char *buf, int size)

Given an oj_poker_hand_info structure filled in as above, fills buf (up to size characters) with a text description of the hand, such as “Fours Full of Kings”, “Seven-high Straight”, or “Three Sixes, Jack, Nine”. As currently implemented, the longest string is 41 characters (“No Pair, Queen, Eight, Seven, Trey, Deuce”), but you might want to allocate buffers a bit larger in case of change.

Next: Blackjack functions