Guest User

Untitled

a guest
Apr 20th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. void initializeDeck(int deck[]); //
  8. void dumpDeck(const int deck[]);
  9. void shuffle(int deck[]); //
  10. int getRandomNumber(int lowerBound, int upperBound); //
  11. void showCard(int card); //
  12. void showCards(const int cards[], int numCards); //
  13. int cardValue(int card); //
  14. int getTopCard(int deck[]); //
  15.  
  16. void addToHand(int hand[], int cardToAdd); //
  17. int getHandValue(const int hand[]); //
  18. void playOneHand(int deck[]); //
  19.  
  20. const int NUM_CARDS = 52;
  21. const int CARDS_IN_HAND = 10;
  22.  
  23. void main()
  24. {
  25.     int deck[NUM_CARDS];
  26.     int choice;
  27.  
  28.     initializeDeck(deck);
  29.     shuffle(deck);
  30.  
  31.     while((sizeof(deck)/sizeof(int)) >= 15)
  32.     {
  33.         cout << "Do you wanto play a round of 21 (1:yes 0:no)?" << endl;
  34.         cin >> choice;
  35.  
  36.         if(choice)
  37.         {
  38.             playOneHand(deck);
  39.         }
  40.     }
  41.  
  42.     if((sizeof(deck)/sizeof(int)) < 15)
  43.     {
  44.         cout << "Sorry, but there are not enough cards left in the deck for another round!";
  45.     }
  46.  
  47.     cin.ignore();
  48.     cin.get();
  49. }
  50.  
  51. void playOneHand(int deck[])
  52. {
  53.     int playerHand[CARDS_IN_HAND] = {0,0,0,0,0,0,0,0,0,0};
  54.     int dealerHand[CARDS_IN_HAND] = {0,0,0,0,0,0,0,0,0,0};
  55.  
  56.     bool pBust = false;
  57.     bool dBust = false;
  58.     bool error = false;
  59.     int hand = 0;
  60.     while(!pBust && !dBust && !error)
  61.     {
  62.         int choice;
  63.        
  64.  
  65.         if(hand > 0)
  66.         {
  67.             cout << "Hand number: " << hand << endl;
  68.  
  69.             cout << "Your Current hand: ";
  70.             showCards(playerHand, CARDS_IN_HAND);
  71.             cout << endl;
  72.  
  73.             cout << "Dealers Current Hand: ";
  74.             showCards(dealerHand, CARDS_IN_HAND);
  75.             cout << endl;
  76.         }
  77.  
  78.         cout << "Do you wanna draw (1:yes 0:no)?" << endl;
  79.         cin >> choice;
  80.  
  81.         if(choice)
  82.         {
  83.             addToHand(playerHand, getTopCard(deck));
  84.             addToHand(dealerHand, getTopCard(deck));
  85.  
  86.             if(getHandValue(playerHand) >= 21)
  87.                 pBust = true;
  88.             if(getHandValue(dealerHand) >= 21)
  89.                 dBust = true;
  90.         }else
  91.         {
  92.             error = true;
  93.         }
  94.         hand++;
  95.     }
  96.  
  97.     if(pBust)
  98.         cout << "You busted!" << endl;
  99.     else if(dBust && !pBust)
  100.         cout << "The Dealer Busted!" << endl;
  101.     else if(!dBust && !pBust)
  102.     {
  103.         if(getHandValue(playerHand) > getHandValue(dealerHand))
  104.             cout << "Your hand is more valuable, you win!" << endl;
  105.         else if(getHandValue(playerHand) == getHandValue(dealerHand))
  106.             cout << "The dealers hand is more valuable, you loose!" << endl;
  107.         else
  108.             cout << "It's a tie!" << endl;
  109.     }
  110. }
  111.  
  112.  
  113. void initializeDeck(int deck[])
  114. {
  115.     int index = 0;
  116.     for(int suite = 100; suite <= 400; suite+=100)
  117.         for(int card = 1; card <= 13; card++)
  118.             deck[index++] = suite+card;
  119. }
  120.  
  121. void dumpDeck(const int deck[])
  122. {
  123.     for(int i = 0; i < NUM_CARDS; i++)
  124.         cout << deck[i] << "\n";
  125. }
  126.  
  127. void shuffle(int deck[])
  128. {
  129.     for(int i = 0; i <= 500; i++)
  130.     {
  131.         int firstIndex = getRandomNumber(0,NUM_CARDS);
  132.         int secondIndex = getRandomNumber(0,NUM_CARDS);
  133.         int tempCard = deck[firstIndex];
  134.        
  135.         deck[firstIndex] = deck[secondIndex];
  136.         deck[secondIndex] = tempCard;
  137.     }
  138. }
  139.  
  140. int getRandomNumber(int lowerBound, int upperBound)
  141. {
  142.     static bool firstRun = true;
  143.     int tempRand;
  144.  
  145.     if(firstRun) //only seed the random gen once
  146.     {
  147.         srand(time(NULL));
  148.         firstRun = false;
  149.     }
  150.  
  151.     if(upperBound > lowerBound)//swap bounds if needed
  152.         tempRand = rand() % (upperBound - lowerBound) + lowerBound;
  153.     else
  154.         tempRand = rand() % (lowerBound - upperBound) + upperBound;
  155.  
  156.     return tempRand;
  157. }
  158.  
  159. void showCard(int card)
  160. {
  161.     int suite = card / 100;
  162.     int cardType = card % 100;
  163.     string symbols[] = {"","A","2","3","4","5","6","7","8","9","10","J","Q","K"};
  164.  
  165.     char outSuite = static_cast<char>(suite+2);
  166.     cout << outSuite << symbols[cardType] << " ";
  167. }
  168.  
  169. void showCards(const int cards[], int numCards)
  170. {
  171.     for(int i = 0; i < numCards; i++)
  172.         if(cards[i] > 0)
  173.             showCard(cards[i]);
  174. }
  175.  
  176. int cardValue(int card)
  177. {
  178.     int cardType = card % 100;
  179.     int value;
  180.     switch(cardType)
  181.     {
  182.         case 1:
  183.             value = 11;
  184.             break;
  185.         case 11:
  186.         case 12:
  187.         case 13:
  188.             value = 10;
  189.             break;
  190.         default:
  191.             value = cardType;
  192.             break;
  193.     }
  194.     return value;
  195. }
  196.  
  197. int getTopCard(int deck[])
  198. {
  199.     int tempCard;
  200.     for(int i = 0; i <= NUM_CARDS; i++)
  201.     {
  202.         if(deck[i] > 0)
  203.         {
  204.             tempCard = deck[i];
  205.             deck[i] = 0;
  206.             break;
  207.         }
  208.         if(i == NUM_CARDS)
  209.         {
  210.             tempCard = 0;
  211.         }
  212.     }
  213.     return tempCard;
  214. }
  215.  
  216. //might wanna check if busted or not.
  217. void addToHand(int hand[], int cardToAdd)
  218. {
  219.     bool error = false;
  220.     int index = 0;
  221.     while(index < CARDS_IN_HAND && !error)
  222.     {
  223.         if(hand[index] == 0)
  224.         {
  225.             hand[index] = cardToAdd;
  226.             error = true;
  227.         }
  228.         index++;
  229.     }
  230. }
  231.  
  232. int getHandValue(const int hand[])
  233. {
  234.     int totalValue = 0;
  235.     for(int i = 0; i < CARDS_IN_HAND; i++)
  236.         totalValue += cardValue(hand[i]);
  237.     return totalValue;
  238. }
Add Comment
Please, Sign In to add comment