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

Examples

Macros

For card, rank, and suit constant comparisons, use the OJ definitions when possible as these are compile-time constants. The macros OJ_CARD(), OJ_RANK() and OJ_SUIT() will also evaluate to constants if their arguments are, though they can be used with variables as well.

    OJ_RANK(c) == OJR_QUEEN             // Is the card a queen?
    OJ_RANK(c) < OJR_TEN                // Is the card 2 through 9?
    OJ_SUIT(c) == OJS_DIAMOND           // Is the card a diamond?
    OJ_RANK(c) == OJR_JOKER             // Is card (either) joker?
    c == OJ_JOKER                       // Is card black joker?
    c = OJ_CARD(OJR_NINE, OJS_SPADE)    // c is now 32 (9 of spades)

Note that there is both an OJR_JOKER, which is the rank of both jokers, and an OJ_JOKER, which is the card value of the single (or black) joker. Note also that because card values are ordered the way they are, you can often compare ranks without having to calculate OJ_RANK() at all. For example:

    c < OJ_CARD(OJR_TEN, OJS_CLUB)      // Is card < 10?
    c >= OJ_CARD(OJR_ACE, OJS_CLUB)     // Is card ace (or joker)?

Here’s a basic “Hello, world.” app using the C API:

#include <stdio.h>
#include <stdlib.h>

#include "ojcardlib.h"

int main(int argc, char *argv[]) {
    oj_card db[52], hb[5];
    oj_cardlist_t deck, hand;

    ojl_new(&deck, db, 52);
    ojl_fill(&deck, 52, OJD_STANDARD);
    ojl_shuffle(&deck);

    ojl_new(&hand, hb, 5);
    for (int i = 0; i < 5; ++i) ojl_append(&hand, ojl_pop(&deck));

    char text[20];
    ojl_text(&hand, text, sizeof(text));
    printf("%s\n", text);

    return 0;
}