Advertisement
dannye33

poker

Apr 3rd, 2015
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 19.46 KB | None | 0 0
  1. // Poker Game
  2. // Copyright 2015 Daniel Harding
  3. // Licensed under the DBAD Public License
  4. // http://www.dbad-license.org/
  5.  
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <ctime>
  9. #include <Windows.h>
  10.  
  11. const HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
  12.  
  13. const int NUM_VALUES  = 13;
  14. const int NUM_SUITS   = 4;
  15. const int NUM_CARDS   = 5;
  16. const int NUM_DISCARD = 3;
  17.  
  18. const COORD DEALER_C = { 8,  1 };
  19. const COORD PLAYER_C = { 8,  7 };
  20. const COORD DONE_C   = { 9, 11 };
  21. const COORD WINNER_C = { 6, 13 };
  22. const COORD AGAIN_C  = { 0, 16 };
  23.  
  24. enum VALUE {
  25.     TWO,
  26.     THREE,
  27.     FOUR,
  28.     FIVE,
  29.     SIX,
  30.     SEVEN,
  31.     EIGHT,
  32.     NINE,
  33.     TEN,
  34.     JACK,
  35.     QUEEN,
  36.     KING,
  37.     ACE
  38. };
  39.  
  40. enum SUIT {
  41.     CLUBS,
  42.     DIAMONDS,
  43.     HEARTS,
  44.     SPADES
  45. };
  46.  
  47. enum RANK {
  48.     HIGH,
  49.     PAIR,
  50.     TWO_PAIR,
  51.     THREE_KIND,
  52.     STRAIGHT,
  53.     FLUSH,
  54.     FULL_HOUSE,
  55.     FOUR_KIND,
  56.     STRAIGHT_FLUSH,
  57.     ROYAL_FLUSH
  58. };
  59.  
  60. enum WINNER {
  61.     PLAYER,
  62.     DEALER,
  63.     TIE
  64. };
  65.  
  66. VALUE& operator++(VALUE &v)
  67. {
  68.     if (v < ACE)
  69.     {
  70.         v = (VALUE)((int)v + 1);
  71.     }
  72.     else
  73.     {
  74.         v = TWO;
  75.     }
  76.     return v;
  77. }
  78.  
  79. VALUE& operator--(VALUE &v)
  80. {
  81.     if (v > TWO)
  82.     {
  83.         v = (VALUE)((int)v - 1);
  84.     }
  85.     else
  86.     {
  87.         v = ACE;
  88.     }
  89.     return v;
  90. }
  91.  
  92. SUIT& operator++(SUIT &s)
  93. {
  94.     if (s < SPADES)
  95.     {
  96.         s = (SUIT)((int)s + 1);
  97.     }
  98.     else
  99.     {
  100.         s = CLUBS;
  101.     }
  102.     return s;
  103. }
  104.  
  105. SUIT& operator--(SUIT &s)
  106. {
  107.     if (s > CLUBS)
  108.     {
  109.         s = (SUIT)((int)s - 1);
  110.     }
  111.     else
  112.     {
  113.         s = SPADES;
  114.     }
  115.     return s;
  116. }
  117.  
  118. class DECK;
  119.  
  120. class HAND {
  121.     friend class DECK;
  122.     friend void PickHand(HAND&, int);
  123.     friend void DisplayHand(HAND&, int);
  124.     friend void DisplayRank(HAND&, int);
  125.     friend WINNER DetermineWinner(HAND&, HAND&);
  126.  
  127. public:
  128.     HAND();
  129.     void Sort();
  130.     void DetermineRank();
  131.  
  132. private:
  133.     VALUE values[NUM_CARDS];
  134.     VALUE values_sorted[NUM_CARDS];
  135.     SUIT suits[NUM_CARDS];
  136.     RANK rank;
  137.     VALUE primary;
  138.     VALUE secondary;
  139.  
  140.     bool IsRoyalFlush();
  141.     bool IsStraightFlush();
  142.     bool IsFourKind();
  143.     bool IsFullHouse();
  144.     bool IsFlush();
  145.     bool IsStraight();
  146.     bool IsThreeKind();
  147.     bool IsTwoPair();
  148.     bool IsPair();
  149. };
  150.  
  151. class DECK {
  152. public:
  153.     DECK();
  154.     void DealHand(HAND&);
  155.     void DrawCard(HAND&, int);
  156.  
  157. private:
  158.     bool available[NUM_VALUES][NUM_SUITS];
  159. };
  160.  
  161. void HideCursor();
  162. void PlayGame();
  163. bool PlayAgain();
  164. bool Debug();
  165. void PickHand(HAND&, int);
  166. void DiscardPlayerCards(DECK&, HAND&);
  167. void DiscardDealerCards(DECK&, HAND&);
  168. void DisplayHands(HAND&, HAND&, bool);
  169. void DisplayHand(HAND&, int);
  170. void DisplayWinner(WINNER, HAND&, HAND&);
  171. void DisplayRank(HAND&, int);
  172. WINNER DetermineWinner(HAND&, HAND&);
  173.  
  174. int main()
  175. {
  176.     srand((int)time(NULL));
  177.     HideCursor();
  178.     do
  179.     {
  180.         PlayGame();
  181.     } while (PlayAgain());
  182.     return 0;
  183. }
  184.  
  185. void HideCursor()
  186. {
  187.     CONSOLE_CURSOR_INFO cursor;
  188.     GetConsoleCursorInfo(handle, &cursor);
  189.     cursor.bVisible = false;
  190.     SetConsoleCursorInfo(handle, &cursor);
  191. }
  192.  
  193. void PlayGame()
  194. {
  195.     DECK deck;
  196.     HAND player;
  197.     HAND dealer;
  198.     WINNER winner;
  199.     system("CLS");
  200.     if (Debug())
  201.     {
  202.         DisplayHands(player, dealer, true);
  203.         PickHand(dealer, DEALER_C.Y + 1);
  204.         PickHand(player, PLAYER_C.Y + 1);
  205.     }
  206.     else
  207.     {
  208.         deck.DealHand(player);
  209.         deck.DealHand(dealer);
  210.         DisplayHands(player, dealer, false);
  211.         DiscardPlayerCards(deck, player);
  212.         DiscardDealerCards(deck, dealer);
  213.     }
  214.     player.Sort();
  215.     dealer.Sort();
  216.     player.DetermineRank();
  217.     dealer.DetermineRank();
  218.     winner = DetermineWinner(player, dealer);
  219.     DisplayHands(player, dealer, true);
  220.     DisplayWinner(winner, player, dealer);
  221. }
  222.  
  223. bool PlayAgain()
  224. {
  225.     COORD cursor;
  226.     cursor.Y = AGAIN_C.Y;
  227.     cursor.X = AGAIN_C.X;
  228.     SetConsoleCursorPosition(handle, cursor);
  229.     printf("WOULD YOU LIKE TO PLAY AGAIN?");
  230.  
  231.     cursor.Y = AGAIN_C.Y + 1;
  232.     SetConsoleCursorPosition(handle, cursor);
  233.     printf("  YES  NO");
  234.  
  235.     cursor.X = AGAIN_C.X + 1;
  236.     SetConsoleCursorPosition(handle, cursor);
  237.     printf("%c", 0xAF);
  238.  
  239.     char key;
  240.     int x = 0;
  241.     const int X_MAX = 1;
  242.     bool done = false;
  243.     do
  244.     {
  245.         key = tolower(_getch());
  246.         switch (key)
  247.         {
  248.         case ('a'):
  249.         case ('d'):
  250.         {
  251.             x += key == 'a' ? -1 : 1;
  252.             if (x < 0)
  253.             {
  254.                 x = X_MAX;
  255.             }
  256.             else if (x > X_MAX)
  257.             {
  258.                 x = 0;
  259.             }
  260.             SetConsoleCursorPosition(handle, cursor);
  261.             printf(" ");
  262.             cursor.X = 1 + x * 5;
  263.             SetConsoleCursorPosition(handle, cursor);
  264.             printf("%c", 0xAF);
  265.             break;
  266.         }
  267.         case ('\r'):
  268.         {
  269.             done = true;
  270.             break;
  271.         }
  272.         default:
  273.         {
  274.             break;
  275.         }
  276.         }
  277.     } while (!done);
  278.     if (x == 0)
  279.     {
  280.         return true;
  281.     }
  282.     return false;
  283. }
  284.  
  285. bool Debug()
  286. {
  287.     COORD cursor;
  288.     cursor.Y = 0;
  289.     cursor.X = 0;
  290.     SetConsoleCursorPosition(handle, cursor);
  291.     printf("DEBUG?");
  292.  
  293.     cursor.Y = 1;
  294.     SetConsoleCursorPosition(handle, cursor);
  295.     printf("  YES  NO");
  296.  
  297.     cursor.X = 1;
  298.     SetConsoleCursorPosition(handle, cursor);
  299.     printf("%c", 0xAF);
  300.  
  301.     char key;
  302.     int x = 0;
  303.     const int X_MAX = 1;
  304.     bool done = false;
  305.     do
  306.     {
  307.         key = tolower(_getch());
  308.         switch (key)
  309.         {
  310.         case ('a'):
  311.         case ('d'):
  312.         {
  313.             x += key == 'a' ? -1 : 1;
  314.             if (x < 0)
  315.             {
  316.                 x = X_MAX;
  317.             }
  318.             else if (x > X_MAX)
  319.             {
  320.                 x = 0;
  321.             }
  322.             SetConsoleCursorPosition(handle, cursor);
  323.             printf(" ");
  324.             cursor.X = 1 + x * 5;
  325.             SetConsoleCursorPosition(handle, cursor);
  326.             printf("%c", 0xAF);
  327.             break;
  328.         }
  329.         case ('\r'):
  330.         {
  331.             done = true;
  332.             break;
  333.         }
  334.         default:
  335.         {
  336.             break;
  337.         }
  338.         }
  339.     } while (!done);
  340.     cursor.Y = 0;
  341.     cursor.X = 0;
  342.     SetConsoleCursorPosition(handle, cursor);
  343.     printf("      ");
  344.     cursor.Y = 1;
  345.     SetConsoleCursorPosition(handle, cursor);
  346.     printf("         ");
  347.     if (x == 0)
  348.     {
  349.         return true;
  350.     }
  351.     return false;
  352. }
  353.  
  354. void PickHand(HAND &hand, int baseY)
  355. {
  356.     COORD cursor;
  357.     cursor.Y = baseY;
  358.     cursor.X = 2;
  359.     SetConsoleCursorPosition(handle, cursor);
  360.     printf("^");
  361.     cursor.Y += 2;
  362.     SetConsoleCursorPosition(handle, cursor);
  363.     printf("v");
  364.     cursor.Y -= 2;
  365.  
  366.     char key;
  367.     int x = 0;
  368.     const int X_MAX = NUM_CARDS * 2 - 1;
  369.     bool done = false;
  370.     do
  371.     {
  372.         key = tolower(_getch());
  373.         switch (key)
  374.         {
  375.         case ('a'):
  376.         case ('d'):
  377.         {
  378.             x += key == 'a' ? -1 : 1;
  379.             if (x < 0)
  380.             {
  381.                 x = X_MAX;
  382.             }
  383.             else if (x > X_MAX)
  384.             {
  385.                 x = 0;
  386.             }
  387.             SetConsoleCursorPosition(handle, cursor);
  388.             printf(" ");
  389.             cursor.Y += 2;
  390.             SetConsoleCursorPosition(handle, cursor);
  391.             printf(" ");
  392.             cursor.Y -= 2;
  393.  
  394.             cursor.X = 2 + x * 2 - x % 2;
  395.             SetConsoleCursorPosition(handle, cursor);
  396.             printf("^");
  397.             cursor.Y += 2;
  398.             SetConsoleCursorPosition(handle, cursor);
  399.             printf("v");
  400.             cursor.Y -= 2;
  401.             break;
  402.         }
  403.         case ('w'):
  404.         {
  405.             int card = x / 2;
  406.             if (x % 2)
  407.             {
  408.                 ++hand.suits[card];
  409.             }
  410.             else
  411.             {
  412.                 ++hand.values[card];
  413.             }
  414.             DisplayHand(hand, baseY + 1);
  415.             break;
  416.         }
  417.         case ('s'):
  418.         {
  419.             int card = x / 2;
  420.             if (x % 2)
  421.             {
  422.                 --hand.suits[card];
  423.             }
  424.             else
  425.             {
  426.                 --hand.values[card];
  427.             }
  428.             DisplayHand(hand, baseY + 1);
  429.             break;
  430.         }
  431.         case ('\r'):
  432.         {
  433.             done = true;
  434.             SetConsoleCursorPosition(handle, cursor);
  435.             printf(" ");
  436.             cursor.Y += 2;
  437.             SetConsoleCursorPosition(handle, cursor);
  438.             printf(" ");
  439.             cursor.Y -= 2;
  440.             break;
  441.         }
  442.         default:
  443.         {
  444.             break;
  445.         }
  446.         }
  447.     } while (!done);
  448. }
  449.  
  450. void DiscardPlayerCards(DECK &deck, HAND &hand)
  451. {
  452.     if (NUM_DISCARD)
  453.     {
  454.         COORD cursor;
  455.  
  456.         SetConsoleCursorPosition(handle, DONE_C);
  457.         printf("DONE");
  458.  
  459.         cursor.Y = PLAYER_C.Y + 2;
  460.         cursor.X = 1;
  461.         SetConsoleCursorPosition(handle, cursor);
  462.         printf("%c", 0xAF);
  463.  
  464.         char key;
  465.         int x = 0, y = 0;
  466.         const int X_MAX = NUM_CARDS - 1;
  467.         const int Y_MAX = 1;
  468.         int numDiscards = 0;
  469.         bool discards[NUM_CARDS];
  470.         bool done = false;
  471.         for (int card = 0; card < NUM_CARDS; ++card)
  472.         {
  473.             discards[card] = false;
  474.         }
  475.         do
  476.         {
  477.             key = tolower(_getch());
  478.             switch (key)
  479.             {
  480.             case ('a'):
  481.             case ('d'):
  482.             {
  483.                 if (y == 0)
  484.                 {
  485.                     x += key == 'a' ? -1 : 1;
  486.                     if (x < 0)
  487.                     {
  488.                         x = X_MAX;
  489.                     }
  490.                     else if (x > X_MAX)
  491.                     {
  492.                         x = 0;
  493.                     }
  494.                     SetConsoleCursorPosition(handle, cursor);
  495.                     printf(" ");
  496.                     cursor.X = 1 + x * 4;
  497.                     SetConsoleCursorPosition(handle, cursor);
  498.                     printf("%c", 0xAF);
  499.                 }
  500.                 break;
  501.             }
  502.             case ('w'):
  503.             case ('s'):
  504.             {
  505.                 y += key == 'w' ? -1 : 1;
  506.                 if (y < 0)
  507.                 {
  508.                     y = Y_MAX;
  509.                 }
  510.                 else if (y > Y_MAX)
  511.                 {
  512.                     y = 0;
  513.                 }
  514.                 SetConsoleCursorPosition(handle, cursor);
  515.                 printf(" ");
  516.                 if (y == Y_MAX)
  517.                 {
  518.                     cursor.Y = DONE_C.Y;
  519.                     cursor.X = DONE_C.X - 1;
  520.                 }
  521.                 else
  522.                 {
  523.                     cursor.Y = PLAYER_C.Y + 2;
  524.                     cursor.X = 1 + x * 4;
  525.                 }
  526.                 SetConsoleCursorPosition(handle, cursor);
  527.                 printf("%c", 0xAF);
  528.                 break;
  529.             }
  530.             case ('\r'):
  531.             {
  532.                 if (y == 0)
  533.                 {
  534.                     if (discards[x])
  535.                     {
  536.                         --numDiscards;
  537.                         discards[x] = false;
  538.                         ++cursor.Y;
  539.                         ++cursor.X;
  540.                         SetConsoleCursorPosition(handle, cursor);
  541.                         printf("  ");
  542.                         --cursor.Y;
  543.                         --cursor.X;
  544.                     }
  545.                     else if (numDiscards < NUM_DISCARD)
  546.                     {
  547.                         ++numDiscards;
  548.                         discards[x] = true;
  549.                         ++cursor.Y;
  550.                         ++cursor.X;
  551.                         SetConsoleCursorPosition(handle, cursor);
  552.                         printf("%c%c", 0xC4, 0xC4);
  553.                         --cursor.Y;
  554.                         --cursor.X;
  555.                     }
  556.                 }
  557.                 else
  558.                 {
  559.                     done = true;
  560.                     cursor.Y = PLAYER_C.Y + 3;
  561.                     cursor.X = 0;
  562.                     SetConsoleCursorPosition(handle, cursor);
  563.                     for (int card = 0; card < NUM_CARDS; ++card)
  564.                     {
  565.                         printf("    ");
  566.                     }
  567.                     cursor.Y = DONE_C.Y;
  568.                     cursor.X = DONE_C.X - 1;
  569.                     SetConsoleCursorPosition(handle, cursor);
  570.                     printf("     ");
  571.                 }
  572.                 break;
  573.             }
  574.             default:
  575.             {
  576.                 break;
  577.             }
  578.             }
  579.         } while (!done);
  580.         for (int card = 0; card < NUM_CARDS; ++card)
  581.         {
  582.             if (discards[card])
  583.             {
  584.                 deck.DrawCard(hand, card);
  585.             }
  586.         }
  587.     }
  588. }
  589.  
  590. void DiscardDealerCards(DECK &deck, HAND &hand)
  591. {
  592.     int numDiscard = rand() % (NUM_DISCARD + 1);
  593.     bool discards[NUM_CARDS];
  594.     int discard;
  595.     for (int card = 0; card < NUM_CARDS; ++card)
  596.     {
  597.         discards[card] = false;
  598.     }
  599.     for (int card = 0; card < numDiscard; ++card)
  600.     {
  601.         do
  602.         {
  603.             discard = rand() % NUM_CARDS;
  604.         } while (discards[discard]);
  605.         discards[discard] = true;
  606.     }
  607.     for (int card = 0; card < NUM_CARDS; ++card)
  608.     {
  609.         if (discards[card])
  610.         {
  611.             deck.DrawCard(hand, card);
  612.         }
  613.     }
  614. }
  615.  
  616. void DisplayHands(HAND &hand1, HAND &hand2, bool showDealer)
  617. {
  618.     SetConsoleCursorPosition(handle, DEALER_C);
  619.     printf("DEALER");
  620.     SetConsoleCursorPosition(handle, PLAYER_C);
  621.     printf("PLAYER");
  622.     if (showDealer)
  623.     {
  624.         DisplayHand(hand2, DEALER_C.Y + 2);
  625.     }
  626.     else
  627.     {
  628.         COORD cursor;
  629.         cursor.Y = DEALER_C.Y + 2;
  630.         cursor.X = 2;
  631.         SetConsoleCursorPosition(handle, cursor);
  632.         for (int card = 0; card < NUM_CARDS; ++card)
  633.         {
  634.             printf("XX  ");
  635.         }
  636.     }
  637.     DisplayHand(hand1, PLAYER_C.Y + 2);
  638. }
  639.  
  640. void DisplayHand(HAND &hand, int baseY)
  641. {
  642.     COORD cursor;
  643.     cursor.Y = baseY;
  644.     cursor.X = 2;
  645.     SetConsoleCursorPosition(handle, cursor);
  646.     for (int card = 0; card < NUM_CARDS; ++card)
  647.     {
  648.         VALUE value = hand.values[card];
  649.         SUIT suit = hand.suits[card];
  650.         char v, s;
  651.         switch (value)
  652.         {
  653.         case (TWO):
  654.         {
  655.             v = '2';
  656.             break;
  657.         }
  658.         case (THREE):
  659.         {
  660.             v = '3';
  661.             break;
  662.         }
  663.         case (FOUR):
  664.         {
  665.             v = '4';
  666.             break;
  667.         }
  668.         case (FIVE):
  669.         {
  670.             v = '5';
  671.             break;
  672.         }
  673.         case (SIX):
  674.         {
  675.             v = '6';
  676.             break;
  677.         }
  678.         case (SEVEN):
  679.         {
  680.             v = '7';
  681.             break;
  682.         }
  683.         case (EIGHT):
  684.         {
  685.             v = '8';
  686.             break;
  687.         }
  688.         case (NINE):
  689.         {
  690.             v = '9';
  691.             break;
  692.         }
  693.         case (TEN):
  694.         {
  695.             v = 'T';
  696.             break;
  697.         }
  698.         case (JACK):
  699.         {
  700.             v = 'J';
  701.             break;
  702.         }
  703.         case (QUEEN):
  704.         {
  705.             v = 'Q';
  706.             break;
  707.         }
  708.         case (KING):
  709.         {
  710.             v = 'K';
  711.             break;
  712.         }
  713.         case (ACE):
  714.         {
  715.             v = 'A';
  716.             break;
  717.         }
  718.         default:
  719.         {
  720.             v = 'X';
  721.             break;
  722.         }
  723.         }
  724.         switch (suit)
  725.         {
  726.         case (CLUBS):
  727.         {
  728.             s = 'C';
  729.             break;
  730.         }
  731.         case (DIAMONDS):
  732.         {
  733.             s = 'D';
  734.             break;
  735.         }
  736.         case (HEARTS):
  737.         {
  738.             s = 'H';
  739.             break;
  740.         }
  741.         case (SPADES):
  742.         {
  743.             s = 'S';
  744.             break;
  745.         }
  746.         default:
  747.         {
  748.             s = 'X';
  749.             break;
  750.         }
  751.         }
  752.         printf("%c%c  ", v, s);
  753.     }
  754. }
  755.  
  756. void DisplayWinner(WINNER winner, HAND &hand1, HAND &hand2)
  757. {
  758.     DisplayRank(hand2, DEALER_C.Y + 3);
  759.     DisplayRank(hand1, PLAYER_C.Y + 3);
  760.  
  761.     SetConsoleCursorPosition(handle, WINNER_C);
  762.     switch (winner)
  763.     {
  764.     case (PLAYER):
  765.     {
  766.         printf("  YOU WIN");
  767.         break;
  768.     }
  769.     case (DEALER):
  770.     {
  771.         printf("DEALER WINS");
  772.         break;
  773.     }
  774.     case (TIE):
  775.     {
  776.         printf("   TIE");
  777.         break;
  778.     }
  779.     default:
  780.     {
  781.         printf("  ERROR");
  782.         break;
  783.     }
  784.     }
  785. }
  786.  
  787. void DisplayRank(HAND &hand, int baseY)
  788. {
  789.     COORD cursor;
  790.     cursor.Y = baseY;
  791.     cursor.X = 4;
  792.     SetConsoleCursorPosition(handle, cursor);
  793.     switch (hand.rank)
  794.     {
  795.     case (ROYAL_FLUSH):
  796.     {
  797.         printf(" ROYAL FLUSH");
  798.         break;
  799.     }
  800.     case (STRAIGHT_FLUSH):
  801.     {
  802.         printf("STRAIGHT FLUSH");
  803.         break;
  804.     }
  805.     case (FOUR_KIND):
  806.     {
  807.         printf("FOUR OF A KIND");
  808.         break;
  809.     }
  810.     case (FULL_HOUSE):
  811.     {
  812.         printf("  FULL HOUSE");
  813.         break;
  814.     }
  815.     case (FLUSH):
  816.     {
  817.         printf("    FLUSH");
  818.         break;
  819.     }
  820.     case (STRAIGHT):
  821.     {
  822.         printf("   STRAIGHT");
  823.         break;
  824.     }
  825.     case (THREE_KIND):
  826.     {
  827.         printf("THREE OF A KIND");
  828.         break;
  829.     }
  830.     case (TWO_PAIR):
  831.     {
  832.         printf("   TWO PAIR");
  833.         break;
  834.     }
  835.     case (PAIR):
  836.     {
  837.         printf("     PAIR");
  838.         break;
  839.     }
  840.     case (HIGH):
  841.     {
  842.         printf("  HIGH CARD");
  843.         break;
  844.     }
  845.     default:
  846.     {
  847.         printf("     ERROR");
  848.         break;
  849.     }
  850.     }
  851. }
  852.  
  853. WINNER DetermineWinner(HAND &hand1, HAND &hand2)
  854. {
  855.     if (hand1.rank > hand2.rank)
  856.     {
  857.         return PLAYER;
  858.     }
  859.     else if (hand1.rank < hand2.rank)
  860.     {
  861.         return DEALER;
  862.     }
  863.     else if (hand1.primary > hand2.primary)
  864.     {
  865.         return PLAYER;
  866.     }
  867.     else if (hand1.primary < hand2.primary)
  868.     {
  869.         return DEALER;
  870.     }
  871.     else if (hand1.secondary > hand2.secondary)
  872.     {
  873.         return PLAYER;
  874.     }
  875.     else if (hand1.secondary < hand2.secondary)
  876.     {
  877.         return DEALER;
  878.     }
  879.     for (int card = 0; card < NUM_CARDS; ++card)
  880.     {
  881.         if (hand1.values_sorted[card] > hand2.values_sorted[card])
  882.         {
  883.             return PLAYER;
  884.         }
  885.         else if (hand1.values_sorted[card] < hand2.values_sorted[card])
  886.         {
  887.             return DEALER;
  888.         }
  889.     }
  890.     return TIE;
  891. }
  892.  
  893. HAND::HAND()
  894. {
  895.     for (int card = 0; card < NUM_CARDS; ++card)
  896.     {
  897.         values[card] = TWO;
  898.         values_sorted[card] = TWO;
  899.         suits[card] = CLUBS;
  900.     }
  901.     rank = HIGH;
  902.     primary = TWO;
  903.     secondary = TWO;
  904. }
  905.  
  906. void HAND::Sort()
  907. {
  908.     bool ignore[NUM_CARDS];
  909.     for (int card = 0; card < NUM_CARDS; ++card)
  910.     {
  911.         ignore[card] = false;
  912.     }
  913.     for (int card = 0; card < NUM_CARDS; ++card)
  914.     {
  915.         VALUE high;
  916.         int i;
  917.         for (int c = 0; c < NUM_CARDS; ++c)
  918.         {
  919.             if (!ignore[c])
  920.             {
  921.                 high = values[c];
  922.                 i = c;
  923.                 break;
  924.             }
  925.         }
  926.         for (int c = 0; c < NUM_CARDS; ++c)
  927.         {
  928.             if (values[c] > high && !ignore[c])
  929.             {
  930.                 high = values[c];
  931.                 i = c;
  932.             }
  933.         }
  934.         ignore[i] = true;
  935.         values_sorted[card] = high;
  936.     }
  937. }
  938.  
  939. void HAND::DetermineRank()
  940. {
  941.     if (IsRoyalFlush())
  942.     {
  943.         rank = ROYAL_FLUSH;
  944.     }
  945.     else if (IsStraightFlush())
  946.     {
  947.         rank = STRAIGHT_FLUSH;
  948.     }
  949.     else if (IsFourKind())
  950.     {
  951.         rank = FOUR_KIND;
  952.     }
  953.     else if (IsFullHouse())
  954.     {
  955.         rank = FULL_HOUSE;
  956.     }
  957.     else if (IsFlush())
  958.     {
  959.         rank = FLUSH;
  960.     }
  961.     else if (IsStraight())
  962.     {
  963.         rank = STRAIGHT;
  964.     }
  965.     else if (IsThreeKind())
  966.     {
  967.         rank = THREE_KIND;
  968.     }
  969.     else if (IsTwoPair())
  970.     {
  971.         rank = TWO_PAIR;
  972.     }
  973.     else if (IsPair())
  974.     {
  975.         rank = PAIR;
  976.     }
  977.     else
  978.     {
  979.         rank = HIGH;
  980.     }
  981. }
  982.  
  983. bool HAND::IsRoyalFlush()
  984. {
  985.     if (IsStraightFlush() && primary == ACE)
  986.     {
  987.         return true;
  988.     }
  989.     return false;
  990. }
  991.  
  992. bool HAND::IsStraightFlush()
  993. {
  994.     if (IsStraight() && IsFlush())
  995.     {
  996.         return true;
  997.     }
  998.     return false;
  999. }
  1000.  
  1001. bool HAND::IsFourKind()
  1002. {
  1003.     for (int card1 = 0; card1 < NUM_CARDS; ++card1)
  1004.     {
  1005.         for (int card2 = 0; card2 < NUM_CARDS; ++card2)
  1006.         {
  1007.             if (values[card1] == values[card2] && card1 != card2)
  1008.             {
  1009.                 for (int card3 = 0; card3 < NUM_CARDS; ++card3)
  1010.                 {
  1011.                     if (values[card3] == values[card1] &&
  1012.                         card3 != card1 && card3 != card2)
  1013.                     {
  1014.                         for (int card4 = 0; card4 < NUM_CARDS; ++card4)
  1015.                         {
  1016.                             if (values[card4] == values[card1] &&
  1017.                                 card4 != card1 && card4 != card2 && card4 != card3)
  1018.                             {
  1019.                                 primary = values[card1];
  1020.                                 return true;
  1021.                             }
  1022.                         }
  1023.                     }
  1024.                 }
  1025.             }
  1026.         }
  1027.     }
  1028.     return false;
  1029. }
  1030.  
  1031. bool HAND::IsFullHouse()
  1032. {
  1033.     for (int card1 = 0; card1 < NUM_CARDS; ++card1)
  1034.     {
  1035.         for (int card2 = 0; card2 < NUM_CARDS; ++card2)
  1036.         {
  1037.             if (values[card1] == values[card2] && card1 != card2)
  1038.             {
  1039.                 for (int card3 = 0; card3 < NUM_CARDS; ++card3)
  1040.                 {
  1041.                     if (values[card3] == values[card1] &&
  1042.                         card3 != card1 && card3 != card2)
  1043.                     {
  1044.                         for (int card4 = 0; card4 < NUM_CARDS; ++card4)
  1045.                         {
  1046.                             for (int card5 = 0; card5 < NUM_CARDS; ++card5)
  1047.                             {
  1048.                                 if (values[card4] == values[card5] && card4 != card5 &&
  1049.                                     card4 != card1 && card4 != card2 && card4 != card3 &&
  1050.                                     card5 != card1 && card5 != card2 && card5 != card3)
  1051.                                 {
  1052.                                     primary = values[card1];
  1053.                                     secondary = values[card4];
  1054.                                     return true;
  1055.                                 }
  1056.                             }
  1057.                         }
  1058.                     }
  1059.                 }
  1060.             }
  1061.         }
  1062.     }
  1063.     return false;
  1064. }
  1065.  
  1066. bool HAND::IsFlush()
  1067. {
  1068.     for (int card = 0; card < NUM_CARDS - 1; ++card)
  1069.     {
  1070.         if (suits[card] != suits[card + 1])
  1071.         {
  1072.             return false;
  1073.         }
  1074.     }
  1075.     return true;
  1076. }
  1077.  
  1078. bool HAND::IsStraight()
  1079. {
  1080.     if (values_sorted[0] == ACE &&
  1081.         values_sorted[1] == FIVE &&
  1082.         values_sorted[2] == FOUR &&
  1083.         values_sorted[3] == THREE &&
  1084.         values_sorted[4] == TWO)
  1085.     {
  1086.         primary = FIVE;
  1087.     }
  1088.     else
  1089.     {
  1090.         for (int card = 0; card < NUM_CARDS - 1; ++card)
  1091.         {
  1092.             if (values_sorted[card] != values_sorted[card + 1] + 1)
  1093.             {
  1094.                 return false;
  1095.             }
  1096.         }
  1097.         primary = values_sorted[0];
  1098.     }
  1099.     return true;
  1100. }
  1101.  
  1102. bool HAND::IsThreeKind()
  1103. {
  1104.     for (int card1 = 0; card1 < NUM_CARDS; ++card1)
  1105.     {
  1106.         for (int card2 = 0; card2 < NUM_CARDS; ++card2)
  1107.         {
  1108.             if (values[card1] == values[card2] && card1 != card2)
  1109.             {
  1110.                 for (int card3 = 0; card3 < NUM_CARDS; ++card3)
  1111.                 {
  1112.                     if (values[card3] == values[card1] &&
  1113.                         card3 != card1 && card3 != card2)
  1114.                     {
  1115.                         primary = values[card1];
  1116.                         return true;
  1117.                     }
  1118.                 }
  1119.             }
  1120.         }
  1121.     }
  1122.     return false;
  1123. }
  1124.  
  1125. bool HAND::IsTwoPair()
  1126. {
  1127.     for (int card1 = 0; card1 < NUM_CARDS; ++card1)
  1128.     {
  1129.         for (int card2 = 0; card2 < NUM_CARDS; ++card2)
  1130.         {
  1131.             if (values[card1] == values[card2] && card1 != card2)
  1132.             {
  1133.                 for (int card3 = 0; card3 < NUM_CARDS; ++card3)
  1134.                 {
  1135.                     for (int card4 = 0; card4 < NUM_CARDS; ++card4)
  1136.                     {
  1137.                         if (values[card3] == values[card4] && card3 != card4 &&
  1138.                             card3 != card1 && card3 != card2 &&
  1139.                             card4 != card1 && card4 != card2)
  1140.                         {
  1141.                             if (values[card1] > values[card3])
  1142.                             {
  1143.                                 primary = values[card1];
  1144.                                 secondary = values[card3];
  1145.                             }
  1146.                             else
  1147.                             {
  1148.                                 primary = values[card3];
  1149.                                 secondary = values[card1];
  1150.                             }
  1151.                             return true;
  1152.                         }
  1153.                     }
  1154.                 }
  1155.             }
  1156.         }
  1157.     }
  1158.     return false;
  1159. }
  1160.  
  1161. bool HAND::IsPair()
  1162. {
  1163.     for (int card1 = 0; card1 < NUM_CARDS; ++card1)
  1164.     {
  1165.         for (int card2 = 0; card2 < NUM_CARDS; ++card2)
  1166.         {
  1167.             if (values[card1] == values[card2] && card1 != card2)
  1168.             {
  1169.                 primary = values[card1];
  1170.                 return true;
  1171.             }
  1172.         }
  1173.     }
  1174.     return false;
  1175. }
  1176.  
  1177. DECK::DECK()
  1178. {
  1179.     for (int value = 0; value < NUM_VALUES; ++value)
  1180.     {
  1181.         for (int suit = 0; suit < NUM_SUITS; ++suit)
  1182.         {
  1183.             available[value][suit] = true;
  1184.         }
  1185.     }
  1186. }
  1187.  
  1188. void DECK::DealHand(HAND &hand)
  1189. {
  1190.     for (int card = 0; card < NUM_CARDS; ++card)
  1191.     {
  1192.         DrawCard(hand, card);
  1193.     }
  1194. }
  1195.  
  1196. void DECK::DrawCard(HAND &hand, int card)
  1197. {
  1198.     VALUE value;
  1199.     SUIT suit;
  1200.     do
  1201.     {
  1202.         value = (VALUE)(rand() % NUM_VALUES);
  1203.         suit = (SUIT)(rand() % NUM_SUITS);
  1204.     } while (available[value][suit] == false);
  1205.     available[value][suit] = false;
  1206.     hand.values[card] = value;
  1207.     hand.suits[card] = suit;
  1208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement