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 <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
struct Carte
{
int valeur = 0;
std::string nom;
std::string sorte;
bool donne = false;
};
constexpr std::size_t PAQUET_MAX = 54; // 13 * 4 + 2 jokers
Carte* createStdPaquet(Carte paquet[], std::size_t size);
void displayPaquet(Carte paquet[], std::size_t size);
Carte* shufflePaquet(Carte paquet[], std::size_t size);
bool savePaquetOnFile(const std::string& fname, Carte paquet[], std::size_t size);
void waitForEnter();
int main()
{
Carte paquet[PAQUET_MAX];
createStdPaquet(paquet, PAQUET_MAX);
std::cout << "Standard deck:\n";
displayPaquet(paquet, PAQUET_MAX);
waitForEnter();
std::srand (std::time(NULL)); // initialize srand() only once
shufflePaquet(paquet, PAQUET_MAX);
std::cout << "Shuffled deck:\n";
displayPaquet(paquet, PAQUET_MAX);
waitForEnter();
if(savePaquetOnFile("fichierpokerhasard.txt", paquet, PAQUET_MAX)) {
std::cout << "Shuffled deck saved on file.\n";
}
waitForEnter();
return 0;
}
/** createStdPaquet() creates a standard card deck inside the passed array
* paquet: the array to be filled with cards
* size: paquet dimension
* return: the filled array
*
* The array will contain: 13 cards from 1 to 13 (from "AS" to "R") in this
* sequence: pique, coeur, trefle, carreau, plus 2 jokers. Jokers sorte is
* left empty.
* Note on procedure: first will be created 13 cards with values and names
* in order; then they are copied three times; then all the sortes are
* assigned; finally, jokers are added.
*/
Carte* createStdPaquet(Carte paquet[], std::size_t size)
{
// J --> joker
const std::string names[] { "AS", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "V", "D", "R", "J" };
const std::string sortes[] { "pique", "coeur", "trefle", "carreau" };
size_t cards = (size - 2) / 4; // 13
for(std::size_t i=0; i<cards; ++i) {
paquet[i].valeur = i + 1;
paquet[i].nom = names[i];
}
// Multiply x 4:
for(int i=1; i<4; ++i) { std::copy(paquet, paquet+cards, paquet+cards*i); }
// Add 4 x 13 sortes
for(int i=0; i<4; ++i) {
for(std::size_t j=0; j<cards; ++j) { paquet[j+cards*i].sorte = sortes[i]; }
}
// Add jokers. Jokers valeur is 0.
paquet[size-2].nom = "J"; // paquet[52]
paquet[size-1].nom = "J";
return paquet;
}
void displayPaquet(Carte paquet[], std::size_t size)
{
for(std::size_t i=0; i<size; ++i) {
std::cout << "valeur: " << std::setw(2) << paquet[i].valeur
<< "; nom: " << std::setw(2) << paquet[i].nom
<< "; sorte: " << std::setw(7) << paquet[i].sorte
<< "; donne: " << std::boolalpha << paquet[i].donne
<< '\n';
}
}
Carte* shufflePaquet(Carte paquet[], std::size_t size)
{
std::random_shuffle(paquet, paquet+size);
return paquet;
}
bool savePaquetOnFile(const std::string& fname, Carte paquet[], std::size_t size)
{
std::ofstream fout(fname);
if(!fout) {
std::cout << "Can't create " << fname << ".\n";
return false;
}
for(std::size_t i=0; i<size; ++i) {
fout << "valeur: " << std::setw(2) << paquet[i].valeur
<< "; nom: " << std::setw(2) << paquet[i].nom
<< "; sorte: " << std::setw(7) << paquet[i].sorte
<< "; donne: " << std::boolalpha << paquet[i].donne
<< '\n';
}
fout.close();
return true;
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
| |