Dawn/src/dawn/poker/Card.cpp

94 lines
1.9 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Card.hpp"
#include "util/Random.hpp"
using namespace Dawn;
void Card::shuffle(std::vector<struct Card> &deck) {
for(uint8_t i = 0; i < deck.size(); i++) {
uint8_t swap = Random::random<uint8_t>(0, deck.size() - 1);
struct Card tmp = deck[i];
deck[i] = deck[swap];
deck[swap] = tmp;
}
}
void Card::fillDeck(std::vector<struct Card> &deck) {
deck.clear();
for(uint8_t i = 0; i < CARD_DECK_SIZE; i++) {
deck.push_back(Card(i));
}
}
int32_t Card::contains(
const std::vector<struct Card> &deck,
const struct Card c
) {
if(deck.size() == 0) return -1;
return std::distance(
deck.begin(),
std::find_if(
deck.begin(),
deck.end(),
[c](struct Card card) {
return card.cardValue == c.cardValue;
}
)
);
}
int32_t Card::containsValue(
const std::vector<struct Card> &deck,
const enum CardValue number
) {
if(deck.size() == 0) return -1;
return std::distance(
deck.begin(),
std::find_if(
deck.begin(),
deck.end(),
[number](struct Card c) {
return c.getValue() == number;
}
)
);
}
std::vector<struct Card> Card::countPairs(
const std::vector<struct Card> &deck,
const enum CardValue val
) {
std::vector<struct Card> pairs;
std::for_each(
deck.begin(),
deck.end(),
[&pairs, val](struct Card c) {
if(c.getValue() == val) pairs.push_back(c);
}
);
return pairs;
}
bool_t Card::cardSorter(struct Card left, struct Card right) {
return left.cardValue < right.cardValue;
}
void Card::sort(std::vector<struct Card> &deck) {
std::sort(deck.begin(), deck.end(), &Card::cardSorter);
}
enum CardValue Card::getValue() {
return (enum CardValue)(cardValue % CARD_COUNT_PER_SUIT);
}
enum CardSuit Card::getSuit() {
return (enum CardSuit)(cardValue / CARD_COUNT_PER_SUIT);
}