0% found this document useful (0 votes)
14 views5 pages

Section C - Poker

Marcooooooo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Section C - Poker

Marcooooooo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

​ oici une implémentation en C++ avec les fichiers `game.h` et `game.

cpp` en prenant en compte le code


V
précédent :

`game.h`
```cpp
#ifndef GAME_H
#define GAME_H

#include <string>
#include <vector>

class Game {
public:
static std::string evaluateHand(const std::vector<std::string>& cards);
static std::pair<std::vector<std::string>, std::string> determineWinner(const
std::vector<std::vector<std::string>>& hands);

private:
static bool isRoyalFlush(const std::vector<std::string>& cards);
static bool isStraightFlush(const std::vector<std::string>& cards);
static bool isFourOfAKind(const std::vector<std::string>& cards);
static bool isFullHouse(const std::vector<std::string>& cards);
static bool isFlush(const std::vector<std::string>& cards);
static bool isStraight(const std::vector<std::string>& cards);
static bool isThreeOfAKind(const std::vector<std::string>& cards);
static bool isTwoPair(const std::vector<std::string>& cards);
static bool isPair(const std::vector<std::string>& cards);

static std::vector<std::string> RANKS;


static std::vector<std::string> SUITS;
static std::vector<std::pair<std::string, int>> COMBINATIONS;
};

#endif // GAME_H
```

`game.cpp`
```cpp
#include "game.h"
#include <algorithm>

std::vector<std::string> Game::RANKS = {"2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
std::vector<std::string> Game::SUITS = {" ♥", "♦", "♣", "♠"};
std::vector<std::pair<std::string, int>> Game::COMBINATIONS = {
{"Royal Flush", 9},
{"Straight Flush", 8},
{"Four of a Kind", 7},
{"Full House", 6},
{"Flush", 5},
{"Straight", 4},
{"Three of a Kind", 3},
{"Two Pair", 2},
{"Pair", 1},
{"High Card", 0}
};

std::string Game::evaluateHand(const std::vector<std::string>& cards) {


std::vector<std::string> sortedCards = cards;
std::sort(sortedCards.begin(), sortedCards.end(), [this](const std::string& a, const std::string& b) {
return std::find(RANKS.begin(), RANKS.end(), a.substr(0, a.size() - 1)) <
std::find(RANKS.begin(), RANKS.end(), b.substr(0, b.size() - 1));
});

if (isRoyalFlush(sortedCards)) return COMBINATIONS[0].first;


if (isStraightFlush(sortedCards)) return COMBINATIONS[1].first;
if (isFourOfAKind(sortedCards)) return COMBINATIONS[2].first;
if (isFullHouse(sortedCards)) return COMBINATIONS[3].first;
if (isFlush(sortedCards)) return COMBINATIONS[4].first;
if (isStraight(sortedCards)) return COMBINATIONS[5].first;
if (isThreeOfAKind(sortedCards)) return COMBINATIONS[6].first;
if (isTwoPair(sortedCards)) return COMBINATIONS[7].first;
if (isPair(sortedCards)) return COMBINATIONS[8].first;
return COMBINATIONS[9].first;
}

std::pair<std::vector<std::string>, std::string> Game::determineWinner(const


std::vector<std::vector<std::string>>& hands) {
std::vector<std::string> winningHand;
std::string winningCombination;
int maxScore = -1;

for (const auto& hand : hands) {


std::string combination = evaluateHand(hand);
int score = std::find_if(COMBINATIONS.begin(), COMBINATIONS.end(), [&](const std::pair<std::string, int>&
c) {
return c.first == combination;
})->second;

if (score > maxScore) {


winningHand = hand;
winningCombination = combination;
maxScore = score;
} else if (score == maxScore) {
// En cas d'égalité, comparer les valeurs des cartes
if (isThreeOfAKind(hand) || isFourOfAKind(hand)) {
int handRank = std::find(RANKS.begin(), RANKS.end(), hand[0].substr(0, hand[0].size() - 1)) -
RANKS.begin();
int winningRank = std::find(RANKS.begin(), RANKS.end(), winningHand[0].substr(0,
winningHand[0].size() - 1)) - RANKS.begin();
if (handRank > winningRank) {
winningHand = hand;
winningCombination = combination;
}
} else if (isTwoPair(hand)) {
int handRank1 = std::find(RANKS.begin(), RANKS.end(), hand[0].substr(0, hand[0].size() - 1)) -
RANKS.begin();
int handRank2 = std::find(RANKS.begin(), RANKS.end(), hand[2].substr(0, hand[2].size() - 1)) -
RANKS.begin();
int winningRank1 = std::find(RANKS.begin(), RANKS.end(), winningHand[0].substr(0,
winningHand[0].size() - 1)) - RANKS.begin();
int winningRank2 = std::find(RANKS.begin(), RANKS.end(), winningHand[2].substr(0,
winningHand[2].size() - 1)) - RANKS.begin();
if (handRank1 > winningRank1 || (handRank1 == winningRank1 && handRank2 > winningRank2)) {
winningHand = hand;
winningCombination = combination;
}
} else if (isStraight(hand) || isFlush(hand) || isStraightFlush(hand)) {
int handRank = std::find(RANKS.begin(), RANKS.end(), hand.back().substr(0, hand.back().size() - 1)) -
RANKS.begin();
int winningRank = std::find(RANKS.begin(), RANKS.end(), winningHand.back().substr(0,
winningHand.back().size() - 1)) - RANKS.begin();
if (handRank > winningRank) {
winningHand = hand;
winningCombination = combination;
}
}
}
}

return std::make_pair(winningHand, winningCombination);


}

bool Game::isRoyalFlush(const std::vector<std::string>& cards) {


return isStraightFlush(cards) && cards.back().substr(0, cards.back().size() - 1) == "A";
}

bool Game::isStraightFlush(const std::vector<std::string>& cards) {


return isFlush(cards) && isStraight(cards);
}
bool Game::isFourOfAKind(const std::vector<std::string>& cards) {
for (size_t i = 0; i < cards.size() - 3; i++) {
if (cards[i].substr(0, cards[i].size() - 1) == cards[i + 1].substr(0, cards[i + 1].size() - 1) &&
cards[i].substr(0, cards[i].size() - 1) == cards[i + 2].substr(0, cards[i + 2].size() - 1) &&
cards[i].substr(0, cards[i].size() - 1) == cards[i + 3].substr(0, cards[i + 3].size() - 1)) {
return true;
}
}
return false;
}

bool Game::isFullHouse(const std::vector<std::string>& cards) {


return isThreeOfAKind(cards) && isPair(cards);
}

bool Game::isFlush(const std::vector<std::string>& cards) {


std::string suit = cards[0].substr(cards[0].size() - 1);
for (const auto& card : cards) {
if (card.substr(card.size() - 1) != suit) {
return false;
}
}
return true;
}

bool Game::isStraight(const std::vector<std::string>& cards) {


for (size_t i = 0; i < cards.size() - 4; i++) {
int rank1 = std::find(RANKS.begin(), RANKS.end(), cards[i].substr(0, cards[i].size() - 1)) - RANKS.begin();
int rank2 = std::find(RANKS.begin(), RANKS.end(), cards[i + 1].substr(0, cards[i + 1].size() - 1)) -
RANKS.begin();
int rank3 = std::find(RANKS.begin(), RANKS.end(), cards[i + 2].substr(0, cards[i + 2].size() - 1)) -
RANKS.begin();
int rank4 = std::find(RANKS.begin(), RANKS.end(), cards[i + 3].substr(0, cards[i + 3].size() - 1)) -
RANKS.begin();
int rank5 = std::find(RANKS.begin(), RANKS.end(), cards[i + 4].substr(0, cards[i + 4].size() - 1)) -
RANKS.begin();
if (rank2 == rank1 + 1 && rank3 == rank1 + 2 && rank4 == rank1 + 3 && rank5 == rank1 + 4) {
return true;
}
}
return false;
}

bool Game::isThreeOfAKind(const std::vector<std::string>& cards) {


for (size_t i = 0; i < cards.size() - 2; i++) {
if (cards[i].substr(0, cards[i].size() - 1) == cards[i + 1].substr(0, cards[i + 1].size() - 1) &&
cards[i].substr(0, cards[i].size() - 1) == cards[i + 2].substr(0, cards[i + 2].size() - 1)) {
return true;
}
}
return false;
}

bool Game::isTwoPair(const std::vector<std::string>& cards) {


int pairCount = 0;
for (size_t i = 0; i < cards.size() - 1; i++) {
if (cards[i].substr(0, cards[i].size() - 1) == cards[i + 1].substr(0, cards[i + 1].size() - 1)) {
pairCount++;
i++;
}
}
return pairCount == 2;
}

bool Game::isPair(const std::vector<std::string>& cards) {


for (size_t i = 0; i < cards.size() - 1; i++) {
if (cards[i].substr(0, cards[i].size() - 1) == cards[i + 1].substr(0, cards[i + 1].size() - 1)) {
return true;
}
}
return false;
}
```

Ce code implémente les mêmes fonctionnalités que le code précédent, mais en utilisant C++ et en séparant le
code dans deux fichiers : `game.h` et `game.cpp`.

La classe `Game` contient les fonctions statiques nécessaires pour évaluer une main de poker et déterminer le
gagnant parmi plusieurs mains. Les fonctions privées `isRoyalFlush()`, `isStraightFlush()`, etc. implémentent
les différentes combinaisons de poker.

Vous pouvez utiliser cette implémentation dans votre système de jeu de poker en C++.

You might also like