Card game and simulation library and tools. Part of the OneJoker project.
Project maintained by Lee Daniel Crocker
Lee's blog is etceterology.
C programs use CardLib by interacting with these object types:
Type | Use |
---|---|
oj_card |
Integral type used for individual cards. |
oj_rank |
Enumeration of card ranks from deuce (lowest) to ace (highest). |
oj_suit |
Enumeration of card suits: club, diamond, heart, spade. |
oj_decktype |
Enumeration of different kinds of card decks. |
oj_cardlist |
Structure for managing a sequence of cards, such as a deck, hand, discard pile, etc. |
oj_combiner |
Structure for iterating over combinations of cards. |
oj_pokerhand |
Structure representing detailed information about a poker hand. |
oj_card
, oj_rank
, oj_suit
CardLib represents cards as small integers, in order shown below (for why I chose this representation, see Representing playing cards in software). The two-character strings shown here are only used for input and output of cards as text (see text functions).
Similarly, card ranks and card suits are represented as integers. These values are also wrapped in types oj_card
, oj_rank
, and oj_suit
for the purpose of better error checking. But the integer values are fixed and may be used for comparisons, table indices, and so on. The enumeration names and values for ranks and suits are as follows:
Some convenient constants and macros are defined as shown below. The OJ_CARD(r,s)
macro takes a rank and suit as arguments and returns the card value. Likewise, OJ_RANK(c)
takes a card and returns a rank, and OJ_SUIT(c)
returns the suit of the given card. These macros will evaluate to constants at compile time if their arguments are constants (e.g., OJ_CARD(OJR_TEN, OJR_CLUB)
will evaluate to the constant value 33
.)
oj_decktype
This is an enumeration of the kinds of card decks used by various games. These are used by functions that initialize new decks of cards (e.g., see ojl_fill
in card list functions).
oj_cardlist
The oj_cardlist
structure keeps an array-like list of cards. These lists are used for decks, hands, discard piles, or any other set of cards that might be needed for a game.
Access to the cards and other features of the list is accomplished through various card list functions. It is strongly recommended that these functions be used rather than accessing the structure or its contained array directly, because these functions do error checking and maintain the consistency of the structure. But such direct access can be used for maximum efficiency if done with care.
oj_combiner
An oj_combiner
object represents all subsets of k cards from a larger set of n cards. For example, all possible poker hands can be represented as the subsets of k = 5 cards out of n = 52, for a total of 2,598,960 different combinations. To create one of these objects, you must first create an oj_cardlist
which contains the n cards from which the combinations will be drawn, and another of at least size k into which the combinations will be written. These are then passed to the initialization function ojc_new()
. For example:
The final argument to ojc_new()
is a 64-bit count. If this value is 0 (as it is above), this tells the combiner to produce every combination exactly once in colex order. If the number is nonzero, this is the total number of combinations that will be produced. This latter option is usually used for Monte Carlo simulations that will produce combinations randomly rather than in order.
See combiner functions for full details.
oj_pokerhand
The oj_pokerhand
structure contains information about a poker hand useful for displaying to humans. See poker functions for full details.