/** * Copyright (c) 2021 Dominic Masters * * This software is released under the MIT License. * https://opensource.org/licenses/MIT */ #pragma once #include "../libs.h" #include "../util/array.h" //////////////////////////////////////////////////////////////////////////////// // Cards //////////////////////////////////////////////////////////////////////////////// // Aces #define CARD_CLUBS_TWO 0x00 #define CARD_CLUBS_THREE 0x01 #define CARD_CLUBS_FOUR 0x02 #define CARD_CLUBS_FIVE 0x03 #define CARD_CLUBS_SIX 0x04 #define CARD_CLUBS_SEVEN 0x05 #define CARD_CLUBS_EIGHT 0x06 #define CARD_CLUBS_NINE 0x07 #define CARD_CLUBS_TEN 0x08 #define CARD_CLUBS_JACK 0x09 #define CARD_CLUBS_QUEEN 0x0A #define CARD_CLUBS_KING 0x0B #define CARD_CLUBS_ACE 0x0C // Diamonds #define CARD_DIAMONDS_TWO 0x0D #define CARD_DIAMONDS_THREE 0x0E #define CARD_DIAMONDS_FOUR 0x0F #define CARD_DIAMONDS_FIVE 0x10 #define CARD_DIAMONDS_SIX 0x11 #define CARD_DIAMONDS_SEVEN 0x12 #define CARD_DIAMONDS_EIGHT 0x13 #define CARD_DIAMONDS_NINE 0x14 #define CARD_DIAMONDS_TEN 0x15 #define CARD_DIAMONDS_JACK 0x16 #define CARD_DIAMONDS_QUEEN 0x17 #define CARD_DIAMONDS_KING 0x18 #define CARD_DIAMONDS_ACE 0x19 // Hearts #define CARD_HEARTS_TWO 0x1A #define CARD_HEARTS_THREE 0x1B #define CARD_HEARTS_FOUR 0x1C #define CARD_HEARTS_FIVE 0x1D #define CARD_HEARTS_SIX 0x1E #define CARD_HEARTS_SEVEN 0x1F #define CARD_HEARTS_EIGHT 0x20 #define CARD_HEARTS_NINE 0x21 #define CARD_HEARTS_TEN 0x22 #define CARD_HEARTS_JACK 0x23 #define CARD_HEARTS_QUEEN 0x24 #define CARD_HEARTS_KING 0x25 #define CARD_HEARTS_ACE 0x26 // Spades #define CARD_SPADES_TWO 0x27 #define CARD_SPADES_THREE 0x28 #define CARD_SPADES_FOUR 0x29 #define CARD_SPADES_FIVE 0x2A #define CARD_SPADES_SIX 0x2B #define CARD_SPADES_SEVEN 0x2C #define CARD_SPADES_EIGHT 0x2D #define CARD_SPADES_NINE 0x2E #define CARD_SPADES_TEN 0x2F #define CARD_SPADES_JACK 0x30 #define CARD_SPADES_QUEEN 0x31 #define CARD_SPADES_KING 0x32 #define CARD_SPADES_ACE 0x33 //////////////////////////////////////////////////////////////////////////////// // Suits //////////////////////////////////////////////////////////////////////////////// #define CARD_SUIT_CLUBS 0x00 #define CARD_SUIT_DIAMONDS 0x01 #define CARD_SUIT_HEARTS 0x02 #define CARD_SUIT_SPADES 0x03 //////////////////////////////////////////////////////////////////////////////// // Card numbers //////////////////////////////////////////////////////////////////////////////// #define CARD_TWO 0x00 #define CARD_THREE 0x01 #define CARD_FOUR 0x02 #define CARD_FIVE 0x03 #define CARD_SIX 0x04 #define CARD_SEVEN 0x05 #define CARD_EIGHT 0x06 #define CARD_NINE 0x07 #define CARD_TEN 0x08 #define CARD_JACK 0x09 #define CARD_QUEEN 0x0A #define CARD_KING 0x0B #define CARD_ACE 0x0C /** Count of cards in each suit */ #define CARD_COUNT_PER_SUIT 13 /** Count of suits */ #define CARD_SUIT_COUNT 4 /** Standard Card Deck Size */ #define CARD_DECK_SIZE 52 /** Type Representing a card's id */ typedef uint8_t card_t; /** * Returns the suit of a given card. * @param card to get the suit for. * @returns The suit. */ #define cardGetSuit(card) (card / CARD_COUNT_PER_SUIT) /** * Returns the number of a given card. * @param card The card to get the number for. * @returns The card number. */ #define cardGetNumber(card) (card % CARD_COUNT_PER_SUIT) /** * Returns the card number for a given suit. * @param number The number to get. * @param suit The suit to get. * @returns The card for this suit and number combo. */ #define cardGet(number, suit) ((suit * CARD_COUNT_PER_SUIT) + number) /** * Deals a card from an array source to an array destination. Pointers to array * lengths will be updated. * * @param source Array source. * @param sourceSize Pointer to where the count of cards is being stored. * @param dest Array destination. * @param destSize Pointer to the size of the destination array. */ void cardDeal(card_t *source, uint8_t *sourceSize, card_t *dest, uint8_t *destSize ); /** * Sort a hand of cards. Cards are ordered in descending weight, aces are high. * Cards will be grouped by their suits, e.g. CARD_CLUBS_TWO will appear before * CARD_DIAMONDS_KING. * * @param cards Hand of cards to sort. * @param length Length of the array of cards. */ void cardHandSort(card_t *cards, uint8_t length); int32_t _cardSorter(const void* left, const void* right); /** * Check if an array of cards contains a specific card. * * @param hand Hand/Array of cards to check. * @param length Length of the array of cards. * @param card Card to look for * @returns The index within the array that the card is. -1 if not found. */ int32_t cardContains(card_t *hand, uint8_t length, card_t card); /** * Check if the array of cards contains a specific number. * * @param hand Array of cards to check * @param length Length of the array. * @param number The number to look for. * @returns The index within the array that the first card is. -1 if not found. */ int32_t cardContainsNumber(card_t *hand, uint8_t length, uint8_t number); /** * Counts the amount of times a card's number appears within the given hand. * * @param in The hand to check * @param inCount How big the hand being checked is * @param number The number to look for. * @param out The indexes within the hand array for each card matching. * @return How many cards in the pair, e.g. 3 for Three of a kind, 4 for full. */ uint8_t cardCountPairs( card_t *in, uint8_t inCount, uint8_t number, int32_t out[4] ); /** * Shuffles a hand / deck * * @param hand Hand to shuffle. * @param length Length of the hand to shuffle. */ void cardShuffle(card_t *hand, uint8_t length); /** * Write an entire deck of cards (in order) to a given hand. * * @param hand Hand to write the deck to. * @return The count of cards in the deck. */ uint8_t cardWriteDeck(card_t *hand); /** * Get the highest card from a hand of cards. * * @param cards Array of cards. * @param cardCount Length of the array. * @return The highest card in the array. */ card_t cardGetHighestCard(card_t *cards, uint8_t cardCount);