George Vellaringattu / Mbed 2 deprecated BlackJack

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Hand.h Source File

Hand.h

00001 using namespace std;
00002 class Hand
00003 {
00004 private:
00005     std::vector <std::string> card_types;   // the card names in the hand 
00006     std::vector <int> card_values;   //  the card values in the hand
00007     std::string hand_holder;  // either 'Player' or 'Dealer'
00008     int aceCount;  //  the number of aces, useful for calculating hand value
00009 
00010 public:
00011     explicit Hand(std::string);  // constructor, sets hand_holder and calls addCard twice
00012     void addCard();  //  adds a randomly chosen card to the hand
00013     void printHand();  //  prints the card_types vector
00014     void printFirstCard();  // prints only the first card in the hand (used for dealer)
00015     int handValue();  // calculates the hand's value
00016     int getCardValues(int i);
00017 };
00018 
00019 Hand::Hand(string player)
00020 {
00021     hand_holder = player;
00022     aceCount = 0;
00023     card_types.clear();
00024     card_values.clear();
00025     for(int i = 0; i<2; i++)
00026     {
00027         addCard();
00028     }
00029     
00030 }
00031 void Hand::addCard()
00032 {
00033     //card selection was changed
00034     int cardSelection = 1 + rand() % 13;
00035     int cardValue = 0;
00036     string cardType;
00037 
00038     switch (cardSelection)
00039     {
00040     case 1:
00041         cardType = "Ace";
00042         cardValue = 11;
00043         aceCount = aceCount + 1;
00044         break;
00045 
00046     case 2:
00047         cardType = "King";
00048         cardValue = 10;
00049         break;
00050 
00051     case 3:
00052         cardType = "Queen";
00053         cardValue = 10;
00054         break;
00055 
00056     case 4:
00057         cardType = "Jack";
00058         cardValue = 10;
00059         break;
00060 
00061     case 5:
00062         cardType = "Ten";
00063         cardValue = 10;
00064         break;
00065 
00066     case 6:
00067         cardType = "Two";
00068         cardValue = 2;
00069         break;
00070 
00071     case 7:
00072         cardType = "Three";
00073         cardValue = 3;
00074         break;
00075 
00076     case 8:
00077         cardType = "Four";
00078         cardValue = 4;
00079         break;
00080 
00081     case 9:
00082         cardType = "Five";
00083         cardValue = 5;
00084         break;
00085 
00086     case 10:
00087         cardType = "Six";
00088         cardValue = 6;
00089         break;
00090 
00091     case 11:
00092         cardType = "Seven";
00093         cardValue = 7;
00094         break;
00095 
00096     case 12:
00097         cardType = "Eight";
00098         cardValue = 8;
00099         break;
00100 
00101     case 13:
00102         cardType = "Nine";
00103         cardValue = 9;
00104         break;
00105     }
00106     card_values.push_back(cardValue);
00107     card_types.push_back(cardType);
00108 }
00109 
00110 int Hand::handValue()
00111 {
00112     int sumOfCards = 0;
00113 
00114     for (int i = 0; i < card_values.size(); i++)
00115     {
00116         sumOfCards = card_values[i] + sumOfCards;
00117     }
00118 
00119     int numAces = aceCount;
00120 
00121     while ((sumOfCards > 21) && (numAces > 0))
00122     {
00123         sumOfCards = sumOfCards - 10;
00124         numAces--;
00125     }
00126 
00127     return sumOfCards;
00128 }
00129 
00130 void Hand::printHand()
00131 {
00132     for (int i = 0; i < card_values.size(); i++)
00133     {
00134         cout << card_values[i] << " ";
00135     }
00136 }
00137 
00138 int Hand::getCardValues(int i)
00139 {
00140      return card_values[i];   
00141 }
00142 
00143 void Hand::printFirstCard()
00144 {
00145     cout << card_values[0];
00146 }