blackjack cc blackjack cc
play

blackjack.cc blackjack.cc nov 05, 13 11:33 Page 1/7 nov 05, 13 - PowerPoint PPT Presentation

Printed by Klas Arvidsson blackjack.cc blackjack.cc nov 05, 13 11:33 Page 1/7 nov 05, 13 11:33 Page 2/7


  1. Printed by Klas Arvidsson blackjack.cc blackjack.cc nov 05, 13 11:33 Page 1/7 nov 05, 13 11:33 Page 2/7 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− class Player // Card, Deck and Player classes // This is in header (*.h) file(s) { public : // Remember how header file guard look? #ifndef BLACKJACK_CLASSES_H Player(std::string n); #define BLACKJACK_CLASSES_H // default values in interface specification (header file) #include <iostream> void draw(Deck& d, int count = 1); #include <vector> bool decideToHit(); #include <random> bool blackjack() const { return sum == 21; } bool stands() const { return stopped; } bool busted() const { return sum > 21; } //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− class Card // const: no member variable is modified int getScore() const { return sum; } { public : std::string getName() const { return name; } std::ostream& print(std::ostream& os); private : // "getter" functions access private parts safe int getValue() const { return value; } std::string name; std::string getSuit() const { return suit; } int sum; int getBlackjackValue() const ; bool stopped; }; private : friend class Deck; #endif // end of header guard Card(int v, std::string s); int value; // 1 − 13 std::string suit; // hearts, diamonds, clubs, spades }; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− class Deck { public : Deck(); void shuffle(); Card top(); private : std::vector<Card> deck; // Create only one instance of random_device in your program, // creating several, or recreating one, will possibly yield // not−very−random numbers. // static: one for all instances of class static std::random_device rand; }; tisdag november 05, 2013 blackjack.cc 1/4

  2. Printed by Klas Arvidsson blackjack.cc blackjack.cc nov 05, 13 11:33 Page 3/7 nov 05, 13 11:33 Page 4/7 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // Player member functions // Card, Deck and Player implementation //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− // This is in implementation (*.cc) file(s) Player::Player(string n) // Would #include "blackjack_classes.h" : name(n), sum(0), stopped(false) //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− { // initializer list preferred over this using namespace std; // name = n; // sum = 0; // Card member functions // stopped = false; //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− } Card::Card(int v, string s) : value(v), suit(s) { bool Player::decideToHit() // initializer list preferred over this { // value = v; string answer; // suit = s; cout << name << " , your score is " << sum << " . " << endl; } cout << " Do you want to take a hit? (Y/n) "; getline(cin, answer); // A long way toward operator<< ostream& Card::print(ostream& os) bool hit = (answer.size() == 0 || answer == " y " || answer == " yes "); { stopped = !hit; return os << value << " of " << suit; return hit; } } int Card::getBlackjackValue() const void Player::draw(Deck& d, int count) { { return min(value, 10); for (int i = 0; i < count; ++i) } { Card card = d.top(); // Deck member functions and members //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− cout << name << " draws "; card.print(cout) << endl; // static member must be created once! if (card.getBlackjackValue() == 1 && (sum + 11) <= 21) random_device Deck::rand; sum += 11; else Deck::Deck() { sum += card.getBlackjackValue(); for (int v = 1; v <= 13; ++v) if (sum > 21) { deck.push_back(Card(v, " hearts ")); { deck.push_back(Card(v, " diamonds ")); cout << name << " busted. " << endl; deck.push_back(Card(v, " spades ")); } deck.push_back(Card(v, " clubs ")); } } } } // std::shuffle would work instead void Deck::shuffle() { for (int i = 0; i < 52; ++i) { swap(deck.at(i), deck.at( rand() % 52 )); } } Card Deck::top() { Card c = deck.back(); deck.pop_back(); return c; } tisdag november 05, 2013 blackjack.cc 2/4

  3. Printed by Klas Arvidsson blackjack.cc blackjack.cc nov 05, 13 11:33 Page 5/7 nov 05, 13 11:33 Page 6/7 // A simplified implementation of the card game Blackjack // Play the game... while ( done < v.size() ) // see wikipedia for full information of the game { #include <iostream> cout << endl << " Next round −−−−−−−−−−−−−−−− " << endl; #include <vector> done = 0; #include <algorithm> // sort for ( Player& player : v) { using namespace std; if ( player.busted() ) { //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− cout << player.getName() << " had gone bust! " << endl; // main program in it’s own implementation file ++done; // Would #include "blackjack_classes.h" } else if ( player.blackjack() ) //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− int main() { { cout << player.getName() << " have Blackjack! " << endl; // discuss what happen when variables are declared ++done; Deck my_deck; } else if ( player.stands() ) // discuss how call to member function works { my_deck.shuffle(); cout << player.getName() << " stands. " << endl; ++done; cout << " Welcome to the game of Blackjack! " << endl; } else if ( player.decideToHit() ) vector<Player> v{ { Player(" One "), player.draw(my_deck); Player(" Two "), } else // Player choose to stand Player(" Three "), Player(" Four "), { Player(" Five ") ++done; }; } } unsigned int done = 0; } // Start by drawing two cards... for ( Player& player : v) { player.draw(my_deck); player.draw(my_deck); } tisdag november 05, 2013 blackjack.cc 3/4

  4. Printed by Klas Arvidsson blackjack.cc nov 05, 13 11:33 Page 7/7 vector<Player>::iterator end; end = remove_if(v.begin(), v.end(), [](Player const & p)−>bool { return p.getScore() > 21; }); v.erase(end, v.end()); sort(v.begin(), v.end(), [](Player const & a, Player const & b)−>bool { return a.getScore() > b.getScore(); }); if ( v.size() == 0 ) { cout << " No winner! " << endl; } else { int winning_score = v.at(0).getScore(); unsigned int i{0}; while ( i < v.size() && v.at(i).getScore() == winning_score ) { cout << v.at(i).getName() << " wins! (score " << winning_score << " ) " << endl; ++i; } } return 0; } tisdag november 05, 2013 blackjack.cc 4/4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend