Skip to content

Commit

Permalink
Delete all vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Duzzuti committed Dec 26, 2023
1 parent c631ffb commit d942ae7
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 212 deletions.
5 changes: 2 additions & 3 deletions include/deck.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include <iostream>
#include <vector>

struct Card {
u_int8_t rank; // 2-14
Expand All @@ -20,8 +19,8 @@ class Deck {
Card draw();
std::string toString(const std::string sep = "\n") const;

static Card getRandomCardExcept(const std::vector<Card>& cards, const int8_t suit = -1, const std::vector<u_int8_t> ranks = {}) noexcept;
static Card getRandomCardExceptCardsWith(const std::vector<Card>& exceptionCards, const int8_t suit = -1, const int8_t rank = -1) noexcept;
static Card getRandomCardExcept(const Card cards[], const u_int8_t cardsLen, const int8_t suit = -1, const u_int8_t ranks[] = {}, const u_int8_t rankLen = 0) noexcept;
static Card getRandomCardExceptCardsWith(const Card exceptionCards[], const u_int8_t cardsLen, const int8_t suit = -1, const int8_t rank = -1) noexcept;

friend bool operator==(const Deck& lhs, const Deck& rhs) noexcept {
if (lhs.len != rhs.len) return false;
Expand Down
5 changes: 3 additions & 2 deletions include/hand_strengths.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ class HandStrengths {
u_int32_t rankStrength;

HandStrengths(HandKinds handkind, u_int32_t rankStrength) noexcept : handkind(handkind), rankStrength(rankStrength){};
HandStrengths() noexcept : handkind(HandKinds::NO_HAND), rankStrength(0){};

static std::vector<HandStrengths> getHandStrengths(Player* const players[], const Data& data) noexcept;
static void getHandStrengths(Player* const players[], const Data& data, HandStrengths result[]) noexcept;
static HandStrengths getHandStrength(const std::pair<Card, Card>& hand, const Card community[]) noexcept;
static u_int32_t getRankStrength(const std::vector<Card> sortedCards, const u_int8_t num) noexcept;
static u_int32_t getRankStrength(const Card sortedCards[], const u_int8_t num) noexcept;

friend bool operator>(const HandStrengths& lhs, const HandStrengths& rhs) noexcept;
friend bool operator==(const HandStrengths& lhs, const HandStrengths& rhs) noexcept;
Expand Down
8 changes: 4 additions & 4 deletions src/deck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@ std::string Deck::toString(const std::string sep) const {
return str;
}

Card Deck::getRandomCardExcept(const std::vector<Card>& cards, const int8_t suit, const std::vector<u_int8_t> ranks) noexcept {
Card Deck::getRandomCardExcept(const Card cards[], const u_int8_t cardsLen, const int8_t suit, const u_int8_t ranks[], const u_int8_t rankLen) noexcept {
// get random card from deck except cards in vector
// or with suit if suit != -1
// or with ranks if ranks.size() > 0
while (true) {
Card card{.rank = (u_int8_t)((std::rand() % 13) + 2), .suit = (u_int8_t)(std::rand() % 4)};
if (std::find(cards.begin(), cards.end(), card) == cards.end() && (suit == -1 || card.suit != suit) && (ranks.size() == 0 || std::find(ranks.begin(), ranks.end(), card.rank) == ranks.end())) {
if (std::find(cards, &cards[cardsLen], card) == &cards[cardsLen] && (suit == -1 || card.suit != suit) && (!rankLen || std::find(ranks, &ranks[rankLen], card.rank) == &ranks[rankLen])) {
return card;
}
}
}

Card Deck::getRandomCardExceptCardsWith(const std::vector<Card>& exceptionCards, const int8_t suit, const int8_t rank) noexcept {
Card Deck::getRandomCardExceptCardsWith(const Card exceptionCards[], const u_int8_t cardsLen, const int8_t suit, const int8_t rank) noexcept {
// get random card from deck with suit if suit != -1 and rank if rank != -1

Card card;
Expand All @@ -115,6 +115,6 @@ Card Deck::getRandomCardExceptCardsWith(const std::vector<Card>& exceptionCards,
card = Card{.rank = (u_int8_t)((std::rand() % 13) + 2), .suit = (u_int8_t)suit};
else // get card with suit and rank
card = Card{.rank = (u_int8_t)rank, .suit = (u_int8_t)suit};
} while (std::find(exceptionCards.begin(), exceptionCards.end(), card) != exceptionCards.end());
} while (std::find(exceptionCards, &exceptionCards[cardsLen], card) != &exceptionCards[cardsLen]);
return card;
}
16 changes: 9 additions & 7 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,30 @@ void Game::run() {
PLOG_DEBUG << "SHOWDOWN!!! Community cards: " << this->data.roundData.communityCards[0].toString() << " " << this->data.roundData.communityCards[1].toString() << " "
<< this->data.roundData.communityCards[2].toString() << " " << this->data.roundData.communityCards[3].toString() << " " << this->data.roundData.communityCards[4].toString();
// get hand strength for each player
std::vector<HandStrengths> handStrengths = HandStrengths::getHandStrengths(this->players, this->data);
HandStrengths handStrengths[data.numPlayers];
HandStrengths::getHandStrengths(this->players, this->data, handStrengths);
// get winner

HandStrengths strongestHand = HandStrengths(HandKinds::NO_HAND, 0);
std::vector<u_int8_t> winners;
u_int8_t winners[this->data.numPlayers];
u_int8_t numWinners = 0;

for (u_int8_t i = 0; i < this->data.numPlayers; i++) {
if (this->data.gameData.playerOut[i] || this->data.roundData.playerFolded[i]) continue;
PLOG_DEBUG << "Player " << +i << " has hand " << this->players[i]->getHand().first.toString() << " " << this->players[i]->getHand().second.toString() << " and hand strength "
<< handStrengths[i].handkind << " " << handStrengths[i].rankStrength;
if (handStrengths[i] > strongestHand) {
strongestHand = handStrengths[i];
winners.clear();
winners.push_back(i);
numWinners = 1;
winners[0] = i;
} else if (handStrengths[i] == strongestHand) {
winners.push_back(i);
winners[numWinners++] = i;
}
}

// distribute pot
u_int64_t potPerWinner = this->data.roundData.pot / winners.size();
for (u_int8_t i = 0; i < (u_int8_t)winners.size(); i++) {
u_int64_t potPerWinner = this->data.roundData.pot / numWinners;
for (u_int8_t i = 0; i < numWinners; i++) {
this->data.gameData.playerChips[winners[i]] += potPerWinner;
}
}
Expand Down
38 changes: 15 additions & 23 deletions src/hand_strengths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

#include <algorithm>

std::vector<HandStrengths> HandStrengths::getHandStrengths(Player* const players[], const Data& data) noexcept {
std::vector<HandStrengths> handStrengths;
handStrengths.reserve(data.numPlayers);

void HandStrengths::getHandStrengths(Player* const players[], const Data& data, HandStrengths result[]) noexcept {
for (u_int8_t i = 0; i < data.numPlayers; i++) {
if (data.gameData.playerOut[i] || data.roundData.playerFolded[i]) {
handStrengths.emplace_back(HandKinds::NO_HAND, 0);
continue;
if (!(data.gameData.playerOut[i] || data.roundData.playerFolded[i])) {
result[i] = getHandStrength(players[i]->getHand(), data.roundData.communityCards);
}
handStrengths.push_back(getHandStrength(players[i]->getHand(), data.roundData.communityCards));
}
return handStrengths;
}

HandStrengths HandStrengths::getHandStrength(const std::pair<Card, Card>& hand, const Card community[]) noexcept {
Expand Down Expand Up @@ -92,21 +86,19 @@ HandStrengths HandStrengths::getHandStrength(const std::pair<Card, Card>& hand,
if (flush_suit != -1) {
// flush
// available hands: flush, full house, four of a kind, straight flush, royal flush
std::vector<Card> flush_cards;
flush_cards.reserve(7);
if (hand.first.suit == flush_suit) {
flush_cards.push_back(hand.first);
}
if (hand.second.suit == flush_suit) {
flush_cards.push_back(hand.second);
}
Card flush_cards[7];
u_int8_t flush_length = 0;

if (hand.first.suit == flush_suit)
flush_cards[flush_length++] = hand.first;
if (hand.second.suit == flush_suit)
flush_cards[flush_length++] = hand.second;

for (u_int8_t j = 0; j < 5; j++) {
if (community[j].suit == flush_suit) {
flush_cards.push_back(community[j]);
}
if (community[j].suit == flush_suit)
flush_cards[flush_length++] = community[j];
}
std::sort(flush_cards.begin(), flush_cards.end(), [](const Card& card1, const Card& card2) { return card1.rank > card2.rank; });
u_int8_t flush_length = flush_cards.size();
std::sort(flush_cards, &flush_cards[flush_length], [](const Card& card1, const Card& card2) { return card1.rank > card2.rank; });
for (u_int8_t j = 0; j < flush_length - 4; j++) {
if (flush_cards[j].rank == flush_cards[j + 4].rank + 4) {
if (flush_cards[j].rank == 14) {
Expand Down Expand Up @@ -162,7 +154,7 @@ HandStrengths HandStrengths::getHandStrength(const std::pair<Card, Card>& hand,
}
}

u_int32_t HandStrengths::getRankStrength(const std::vector<Card> sortedCards, const u_int8_t num) noexcept {
u_int32_t HandStrengths::getRankStrength(const Card sortedCards[], const u_int8_t num) noexcept {
u_int32_t rankStrength = 0;
for (u_int8_t i = 0; i < num; i++) {
rankStrength |= sortedCards[i].rank << 4 * (num - i - 1);
Expand Down
18 changes: 8 additions & 10 deletions tests/deck_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

TEST(Deck, Constructor) {
Deck deck;
std::vector<Card> cards;
Card cards[52];
for (u_int8_t suit = 0; suit < 4; suit++) {
for (u_int8_t rank = 2; rank < 15; rank++) {
cards.push_back(Card{rank, suit});
cards[suit*13 + rank-2] = Card{rank, suit};
}
}
std::reverse(cards.begin(), cards.end());
EXPECT_EQ(cards.size(), 52);
for (u_int8_t ind = 0; ind < cards.size(); ind++) {
std::reverse(cards, &cards[52]);
for (u_int8_t ind = 0; ind < 52; ind++) {
Card card = deck.draw();
EXPECT_EQ(card.rank, cards[ind].rank);
EXPECT_EQ(card.suit, cards[ind].suit);
Expand All @@ -28,13 +27,12 @@ TEST(Deck, Constructor) {

TEST(Deck, getRandomCardExcept) {
Deck deck;
std::vector<Card> cards;
Card cards[52];
for (u_int8_t suit = 0; suit < 4; suit++) {
for (u_int8_t rank = 2; rank < 15; rank++) {
cards.push_back(Card{rank, suit});
cards[suit*13 + rank-2] = Card{rank, suit};
}
}
EXPECT_EQ(cards.size(), 52);
}

TEST(Deck, shuffle) {
Expand Down Expand Up @@ -108,12 +106,12 @@ TEST(Deck, Perfdraw) {

TEST(Deck, PerfrandomCardExcept) {
for (u_int64_t ind = 0; ind < 1000000; ind++) {
Deck::getRandomCardExcept({}, -1, {});
Deck::getRandomCardExcept({}, 0);
}
}

TEST(Deck, PerfrandomCardExceptCardsWith) {
for (u_int64_t ind = 0; ind < 1000000; ind++) {
Deck::getRandomCardExceptCardsWith({}, -1, -1);
Deck::getRandomCardExceptCardsWith({}, 0);
}
}
Loading

0 comments on commit d942ae7

Please sign in to comment.