#ifndef CARDDECK_HH_ # define CARDDECK_HH_ # include <string> static const std::string colors[4] = {"Trefle", "Carreau", "Pique", "Coeur"}; static const std::string faces[13] = {"Deux", "Trois", "Quatre", "Cinq", "Six", "Sept", "Huit", "Neuf", "Dix", "Valet", "Dame", "Roi", "As"}; static const int values[13] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11}; struct Card { std::string color; std::string face; int value; }; class CardDeck { public: CardDeck(); void shuffle(); bool isEmpty() const; Card pick(); private: Card _cards[52]; bool _picked[52]; }; #endif /* !CARDDECK_HH_ */
#include "CardDeck.hh" #include #include <cassert> CardDeck::CardDeck() { int x = 0; for (int i = 0; i < 4; ++i) { for (int j = 0; j < 13; ++j) { _cards[x].color = colors[i]; _cards[x].face = faces[j]; _cards[x].value = values[j]; ++x; } } shuffle(); } void CardDeck::shuffle() { for (int i = 0; i < 52; ++i) _picked[i] = false; for (int i = 0; i < 100; ++i) { const int index1 = rand() % 52; const int index2 = rand() % 52; if (index1 != index2) std::swap(_cards[index1], _cards[index2]); } } bool CardDeck::isEmpty() const { for (int i = 0; i < 52; ++i) if (!_picked[i]) return false; return true; } Card CardDeck::pick() { assert(!isEmpty()); int index = 0; do { index = rand() % 52; } while (_picked[index]); _picked[index] = true; return _cards[index]; }
#include "CardDeck.hh" #include #include <ctime> #include <cstdlib> #include <string> namespace { void displayTable(const std::string& title, const std::string& sentence, int score) { std::cout << " _ _ _ _ _ _ _ _ _ _ _ _\n" << "| |\n" << "| " << title << " |\n" << "| |\n" << "| _ _ _ _ _ _ _ _ _ _ _ |\n" << sentence << " (score: " << score << ")" << std::endl; } void pickCardForPlayer(CardDeck& cards, const std::string& name, int& score, bool show) { Card card = cards.pick(); score += card.value; if (show) std::cout << name << ": " << card.face << " de " << card.color << std::endl; } bool askYesNo(const std::string& question) { while (true) { std::cout << question << " (oui/non)" << std::endl; std::string word; std::cin >> word; if (word == "oui") return true; if (word == "non") return false; std::cerr << word << " n'est pas une réponse valide !" << std::endl; } } void play(CardDeck& cards) { bool stop = false; int scorePlayer = 0; int scoreBank = 0; cards.shuffle(); while (!stop) { pickCardForPlayer(cards, "Joueur", scorePlayer, true); if (scorePlayer > 21) { displayTable("PERDU", "Vous avez depassé 21.", scorePlayer); return; } if (scoreBank < 21) pickCardForPlayer(cards, "Banque", scoreBank, false); if (scoreBank > 21) { displayTable("GAGNE", "La banque a depassé 21.", scoreBank); return; } if (!askYesNo("Piocher une autre carte ?")) stop = true; } while (scoreBank < scorePlayer && scoreBank < 21) { pickCardForPlayer(cards, "Banque", scoreBank, true); if (scoreBank > 21) { displayTable("GAGNE", "La banque a depassé 21.", scoreBank); return; } } if (scoreBank == scorePlayer) displayTable("EGALITE", "Egalité avec la banque.", scorePlayer); else displayTable("PERDU", "La banque a un meilleur pot.", scoreBank); } } // namespace int main() { srand(time(0)); bool stop = false; CardDeck cards; while (!stop) { play(cards); if (!askYesNo("Voulez-vous rejouer ?")) { std::cout << "Au revoir."<< std::endl; stop = true; } } return 0; }
g++ -W -Wall -pedantic main.cc CardDeck.cc
le point d'exclamation avant d'apeller la fonction sert a quoi ?
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionJ'ai pas vu sa, j'ai peur de dire une connerie mais c'est comme si que tu code sur plusieurs pages et que ensuite tu les regroupent sur la compilation ?
une autre question si j'ai bien compris une classe c'est une fonction avec des variables local?
g++ -W -Wall -pedantic main.cc CardDeck.cc