134 lines
3.3 KiB
C++
134 lines
3.3 KiB
C++
// Copyright (c) 2022 Dominic Masters
|
|
//
|
|
// This software is released under the MIT License.
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
#pragma once
|
|
#include "dawnlibs.hpp"
|
|
#include "assert/assert.hpp"
|
|
|
|
namespace Dawn {
|
|
enum CardSuit {
|
|
CARD_CLUBS = 0,
|
|
CARD_DIAMONDS = 1,
|
|
CARD_HEARTS = 2,
|
|
CARD_SPADES = 3
|
|
};
|
|
|
|
enum CardValue {
|
|
CARD_TWO = 0,
|
|
CARD_THREE = 1,
|
|
CARD_FOUR = 2,
|
|
CARD_FIVE = 3,
|
|
CARD_SIX = 4,
|
|
CARD_SEVEN = 5,
|
|
CARD_EIGHT = 6,
|
|
CARD_NINE = 7,
|
|
CARD_TEN = 8,
|
|
CARD_JACK = 9,
|
|
CARD_QUEEN = 10,
|
|
CARD_KING = 11,
|
|
CARD_ACE = 12
|
|
};
|
|
|
|
/** 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 CARD_COUNT_PER_SUIT*CARD_SUIT_COUNT
|
|
|
|
struct Card {
|
|
public:
|
|
uint8_t cardValue;
|
|
|
|
/**
|
|
* Shuffles a hand / deck
|
|
*
|
|
* @param deck Array of cards to shuffle.
|
|
*/
|
|
static void shuffle(std::vector<struct Card> *deck);
|
|
|
|
/**
|
|
* Fills a vector with all of the cards in a deck, in order.
|
|
*
|
|
* @param deck Deck to fill.
|
|
*/
|
|
static void fillDeck(std::vector<struct Card> *deck);
|
|
|
|
/**
|
|
* Check if an array of cards contains a specific card.
|
|
*
|
|
* @param deck Deck/Hand/Array of cards to check.
|
|
* @param card Card to look for
|
|
* @returns The index within the array that the card is. -1 if not found.
|
|
*/
|
|
static int32_t contains(std::vector<struct Card> *deck, struct Card card);
|
|
|
|
/**
|
|
* Check if the array of cards contains a specific number.
|
|
*
|
|
* @param deck Array of cards to check
|
|
* @param number The number to look for.
|
|
* @returns The index that the first card is. -1 if not found.
|
|
*/
|
|
static int32_t containsNumber(
|
|
std::vector<struct Card> *deck,
|
|
enum CardValue number
|
|
);
|
|
|
|
/**
|
|
* Counts the amount of times a card's number appears within the given
|
|
* hand.
|
|
*
|
|
* @param deck The hand to check
|
|
* @param val Value of pairs to find.
|
|
* @return Card pairs in the deck.
|
|
*/
|
|
static std::vector<struct Card> countPairs(
|
|
std::vector<struct Card> *deck,
|
|
enum CardValue val
|
|
);
|
|
|
|
static bool_t cardSorter(struct Card left, struct Card right);
|
|
|
|
/**
|
|
* 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 deck Hand of cards to sort.
|
|
*/
|
|
static void sort(std::vector<struct Card> *deck);
|
|
|
|
Card(CardSuit suit, CardValue num) :
|
|
cardValue((suit * CARD_COUNT_PER_SUIT) + num)
|
|
{
|
|
assertTrue(suit < CARD_SUIT_COUNT);
|
|
assertTrue(num < CARD_COUNT_PER_SUIT);
|
|
}
|
|
|
|
Card(uint8_t cv) : cardValue(cv) {
|
|
assertTrue(cv < CARD_DECK_SIZE);
|
|
}
|
|
|
|
/**
|
|
* Returns the number of a given card.
|
|
* @returns The card number.
|
|
*/
|
|
CardValue getValue() {
|
|
return (CardValue)(cardValue % CARD_COUNT_PER_SUIT);
|
|
}
|
|
|
|
/**
|
|
* Returns the suit of a given card.
|
|
* @returns The suit.
|
|
*/
|
|
CardSuit getSuit() {
|
|
return (CardSuit)(cardValue / CARD_COUNT_PER_SUIT);
|
|
}
|
|
};
|
|
|
|
} |