151 lines
3.7 KiB
C++
151 lines
3.7 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 class CardSuit : uint8_t {
|
|
Clubs = 0,
|
|
Diamonds = 1,
|
|
Hearts = 2,
|
|
Spades = 3,
|
|
};
|
|
|
|
enum class CardValue : uint8_t {
|
|
Two = 0,
|
|
Three = 1,
|
|
Four = 2,
|
|
Five = 3,
|
|
Six = 4,
|
|
Seven = 5,
|
|
Eight = 6,
|
|
Nine = 7,
|
|
Ten = 8,
|
|
Jack = 9,
|
|
Queen = 10,
|
|
King = 11,
|
|
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(
|
|
const std::vector<struct Card> &deck,
|
|
const 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(
|
|
const std::vector<struct Card> &deck,
|
|
const 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(
|
|
const std::vector<struct Card> &deck,
|
|
const enum CardValue val
|
|
);
|
|
|
|
/**
|
|
* Sorter for the cardSorter function.
|
|
*
|
|
* @param left Left card to compare.
|
|
* @param right Right card to compare.
|
|
* @returns True if left is less than right.
|
|
*/
|
|
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);
|
|
|
|
/**
|
|
* Constructor for the Card class.
|
|
*
|
|
* @param suit Suit of the card.
|
|
* @param num Number of the card.
|
|
*/
|
|
Card(const CardSuit suit, const CardValue num) :
|
|
Card(
|
|
((uint8_t)suit * CARD_COUNT_PER_SUIT) + (uint8_t)num
|
|
)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Constructor for the Card class.
|
|
*
|
|
* @param cv Card value.
|
|
*/
|
|
Card(const uint8_t cv) : cardValue(cv) {
|
|
assertTrue(cv < CARD_DECK_SIZE, "Card value out of range");
|
|
}
|
|
|
|
/**
|
|
* Returns the number of a given card.
|
|
* @returns The card number.
|
|
*/
|
|
enum CardValue getValue();
|
|
|
|
/**
|
|
* Returns the suit of a given card.
|
|
* @returns The suit.
|
|
*/
|
|
enum CardSuit getSuit();
|
|
};
|
|
|
|
} |