Deck Quick Start Guide - aaronjsutton/deck GitHub Wiki

Installation

Install deck by first building the library with:

make

Then install it with:

make install

Depending on your OS, you may need to be root to run make install

Basics

Creating a Deck

The deck structure is the basic represenation of a playing card deck. To create a new card deck, you must first allocate space for a deck in your program. To do this, use the deck_alloc function.

deck *my_deck;
my_deck = deck_alloc();

Once a deck has been allocated, it can be filled with cards. The function deck_fill fills a deck with a standard 52 card deck.

deck_fill(my_deck);

Deck Operations

There are several actions that can be done to a deck of cards.

Shuffling

First and foremost, you will likely want to shuffle your deck.

To simulate a standard card shuffle, you can use the deck_riffle_shuffle function.

deck_riffle_shuffle(my_deck);

Tip: For a deck that has just been filled, call this method multiple times for best results.

A list of avalible algorithms, see shuffling algorithms.

Cutting

A deck can be cut in half using the deck_cut function. This function returns a *deck to one half, while modifying the pass in *deck in place.

deck *other_half;
// Cut our existing deck
other_half = deck_cut(my_deck);

my_deck and other_half now contain an equal amount of cards.

Picking and Drawing Cards

Playing cards are represented using the card structure. When you pick a card from a deck, you obtain a *card without modifying the deck. Drawing a card from the deck returns a *card, but also removes the card from the deck.

Both drawing and picking provide methods that choose from the top of the deck, as well a random card from the deck.

// Pick a card
card *a_card;
a_card = deck_pick(my_deck);

a_card is now the top card of my_deck. my_deck still contains all of its original cards.

To pick a random card from the deck, use deck_pick_rand.

Drawing cards works in a similar fashion:

// Draw a card
card *drawn_card;
drawn_card = deck_draw(my_deck);

drawn_card is now the top card in my_deck. my_deck no longer contains drawn_card.

Adding Cards

Cards can be added to a deck using deck_add.

card c = {clubs, RANK_ACE};
deck_add(my_deck, &c);

my_deck now contains c, an ace of clubs.

See deck.h for more information on suit enums and RANK macros.

Freeing

Decks are freed using deck_free

deck_free(my_deck);
// All done with my_deck

Card Descriptions

You can obtain printable strings that describe a card using card_ascii and card_unicode.

card_ascii returns a char* in a purely ASCII format: [rank] of [suit] Examples:

  • Ace of spades
  • 5 of clubs
  • Jack of diamonds

card_unicode is similar except the char* it returns contains Unicode code points. Examples:

  • ♠ Ace
  • ♣ 5
  • ♦ Jack

Examples

A commented example program that creates a deck, shuffles it, then "deals" out cards can be found in deal.c.