1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| #include "stdafx.h"
#include <iostream>
#include <utility>
#include <array>
#include <numeric>
#include <algorithm>
#include <random>
#include <string>
#include <vector>
const auto nbCarte = 52;
const auto nbCarteMain = 5;
const auto nbCarteParSigne = 13;
const auto nbCouleur = 4;
using Carte = std::pair<unsigned, unsigned>;
using Deck = std::array<Carte, nbCarte>;
using Hand = std::vector<Carte>;
Carte& operator++(Carte& p) //Ajoute un operateur ++ pour la fonction std::iota qui est appelé dans la fonction "creationDeck"
{
++p.first;
++p.second;
return p;
}
auto creationDeck() //Creation d'un array de 52 emplacements qui contient chacun une paire de deux unsigned int
{
Deck tab;
std::iota(std::begin(tab), std::end(tab), Carte());
std::transform(std::begin(tab), std::end(tab), std::begin(tab), [](Carte c)
{
c.first %= nbCarteParSigne;
c.second %= nbCouleur;
return c;
});
std::sort(std::begin(tab), std::end(tab));
return tab;
}
void melangerCarte(Deck &tab) //Fonction de mélange aleatoire
{
std::random_device random;
std::shuffle(std::begin(tab), std::end(tab), random);
}
void couperCarte(Deck &tab) //Fonction qui coupe les carte en deux
{
std::rotate(tab.begin(), tab.begin() + 27, tab.end());
}
void afficherCarte(Carte c) //Fonction qui affiche le nom d'une carte
{
static std::array<std::string, nbCarteParSigne> numero = { "Deux","Trois","Quatre","Cinq","Six","Sept","Huit","Neuf","Dix","Valet","Dame","Roi","As" };
static std::array<std::string, nbCouleur> couleur = { "coeur","carreau","pique","trefle" };
std::cout << numero[c.first] << " de " << couleur[c.second] << std::endl;
}
Hand creationMainAvecTri(Deck tab) //Fonction qui créé une main de 5 carte en ordre croissant
{
Hand tab1(nbCarteMain);
std::copy_n(tab.begin(), 5, tab1.begin());
std::sort(tab1.begin(), tab1.end(), [](auto &a,auto &b)
{
return a.first < b.first;
});
return tab1;
}
int scoreMain(Hand tab) // Fonction qui attribue un score en fonction de la main
{
auto quinteFR = 0; // Quint flush royale
auto combiScore = 0; // Score final des combinaisons
if (std::all_of(tab.cbegin(), tab.cend(), [&tab](Carte c) { return c.second == tab[0].second; })) // Si la main contient une couleur
{
combiScore += 5;
quinteFR = +1;
}
auto x = tab[0].first;
if (std::all_of(tab.cbegin(), tab.cend(), [&x](Carte c) {return c.first == x++; })) // Si la main contient une suite
{
combiScore += 4;
if (quinteFR == 1) // Si les deux conditions au dessus sont True, quinte flush!
{
combiScore += 2;
}
if ((quinteFR == 1) && (tab[0].first == 8)) // Si les deux premieres conditions de la fonction sont True et que la premiere case de la main a pour valeur (8,x), quinte flush royale!
{
combiScore += 4;
}
}
std::vector<Hand> splitByRank;
return combiScore;
}
int main()
{
auto jeuDeCarte = creationDeck(); //Crétion du jeu de carte dans un array en appelant la fonction "creationDeck"
melangerCarte(jeuDeCarte); //Melange les cartes aléatoirement en appelant la fonction "melangerCarte"
couperCarte(jeuDeCarte); //Coupe le jeu de carte au milieu en appelant la fonction "couperCarte"
auto main = creationMainAvecTri(jeuDeCarte); //Création d'une main des 5 cartes du debut du tableau "jeuDeCarte" puis la tri
/*Hand main(nbCarteMain); // Ca me sert de test pour tester si mes fonctions sont fonctionnelles
main[0] = std::make_pair(8,0);
main[1] = std::make_pair(9, 0);
main[2] = std::make_pair(10, 0);
main[3] = std::make_pair(11, 0);
main[4] = std::make_pair(12, 0);*/
std::for_each(std::begin(main), std::end(main), [](Carte c) {afficherCarte(c);});
std::cout << scoreMain(main) << std::endl;
return 0;
} |
Partager