Dawn/src/dawnpoker/poker/Card.cpp

71 lines
1.5 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "Card.hpp"
using namespace Dawn;
void Card::fillDeck(std::vector<struct Card> *deck) {
assertNotNull(deck);
for(uint8_t i = 0; i < CARD_DECK_SIZE; i++) {
deck->push_back(Card(i));
}
}
int32_t Card::contains(std::vector<struct Card> *deck, struct Card c) {
assertNotNull(deck);
auto it = deck->begin();
while(it != deck->end()) {
if(it->cardValue == c.cardValue) return (int32_t)(it - deck->begin());
++it;
}
return -1;
}
int32_t Card::containsNumber(
std::vector<struct Card> *deck,
enum CardValue number
) {
assertNotNull(deck);
assertTrue(number < CARD_COUNT_PER_SUIT);
auto it = deck->begin();
while(it != deck->end()) {
if(it->getValue() == number) return (int32_t)(it - deck->begin());
++it;
}
return -1;
}
std::vector<struct Card> Card::countPairs(
std::vector<struct Card> *deck,
enum CardValue val
) {
std::vector<struct Card> pairs;
assertNotNull(deck);
assertTrue(deck->size() > 0);
auto it = deck->begin();
while(it != deck->end()) {
if(it->getValue() == val) pairs.push_back(*it);
++it;
}
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) {
assertNotNull(deck);
assertTrue(deck->size() > 1);
std::sort(deck->begin(), deck->end(), &Card::cardSorter);
}