Advertisement
Slowhand-VI

TUT: blackjack_shoe.fos

Jan 19th, 2016
228
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**<
  2.  *  Shoe class for Blackjack.
  3.  */
  4.  
  5. #include "blackjack_h.fos"
  6. #include "blackjack_deck.fos"
  7. #include "_macros.fos"
  8.  
  9. class CShoe
  10. {
  11.     private uint numberOfDecks;        //  Number of card decks (packs) used.
  12.     private array<CCard@> cards;       //  Cards in the shoe.
  13.     private uint currentCardIndex;
  14.  
  15.     bool IsEmpty()
  16.     {
  17.         if (currentCardIndex >= cards.length())
  18.             return true;
  19.         else
  20.             return false;
  21.     }
  22.     uint GetCurrentCardIndex()
  23.     {
  24.         return currentCardIndex;
  25.     }
  26.     uint GetTotalCards()
  27.     {
  28.         return cards.length();
  29.     }
  30.     uint GetNumberOfDecks()
  31.     {
  32.         return numberOfDecks;
  33.     }
  34.  
  35.     CShoe(uint decks)
  36.     {
  37.         numberOfDecks = CLAMP(decks, BLACKJACK_MIN_DECKS_IN_SHOE, BLACKJACK_MAX_DECKS_IN_SHOE);
  38.         Init();
  39.     }
  40.  
  41.     void Init()
  42.     {
  43.         currentCardIndex = 0;
  44.         CDeck@ deck = CDeck();
  45.         for (uint i = 0; i < numberOfDecks; i++)
  46.         {
  47.             for (uint j = 0; j < deck.cards.length(); j++)
  48.             {
  49.                 cards.insertLast(deck.cards[j]);
  50.             }
  51.         }
  52.     }
  53.  
  54.     void Shuffle()
  55.     {
  56.         for (int i = 0; i < 10; i++)
  57.         {
  58.             if (Random(0, 1) == 0)
  59.                 HinduShuffle();
  60.             if (Random(0, 1) == 0)
  61.                 SplitShuffle();
  62.             if (Random(0, 1) == 0)
  63.                 Cut();
  64.         }
  65.     }
  66.  
  67.     //  If you don't know how Hindu shuffle is done, check it on youtube.
  68.     void HinduShuffle()
  69.     {
  70.         int minTake = 5;
  71.         int maxTake = 15;
  72.         int maxIndex = cards.length();
  73.         int index = Random(minTake, maxTake);
  74.         array<CCard@> tempShoe;
  75.         int lastIndex = 0;
  76.         while (index < maxIndex - maxTake)
  77.         {
  78.             for (int i = index - 1; i >= lastIndex; i--)
  79.             {
  80.                 tempShoe.insertLast(cards[i]);
  81.             }
  82.             lastIndex = index;
  83.             index += Random(minTake, maxTake);
  84.         }
  85.         for (int i = maxIndex - 1; i >= lastIndex; i--)
  86.         {
  87.             tempShoe.insertLast(cards[i]);
  88.         }
  89.         array<CCard@> tmp;
  90.         cards = tmp;
  91.         for (int i = 0; i < maxIndex; i++)
  92.         {
  93.             cards.insertLast(tempShoe[maxIndex - i - 1]);
  94.         }
  95.     }
  96.  
  97.     //  Also known as Pharao shuffle, split in half, and mix them up.
  98.     void SplitShuffle()
  99.     {
  100.         uint index = Random(cards.length()*4/10, cards.length()*6/10);
  101.         array<CCard@> tempCards;
  102.         uint i = 0;
  103.         uint j = index;
  104.         while (i + j - index < cards.length())
  105.         {
  106.             if (i < index)
  107.             {
  108.                 int randomIndex = Random(0, 1);
  109.                 for (int ri = 0; ri < randomIndex; ri++)
  110.                 {
  111.                     tempCards.insertLast(cards[i + ri]);
  112.                 }
  113.                 i += randomIndex;
  114.             }
  115.             if (j < cards.length())
  116.             {
  117.                 int randomIndex = Random(0, 1);
  118.                 for (int ri = 0; ri < randomIndex; ri++)
  119.                 {
  120.                     tempCards.insertLast(cards[j + ri]);
  121.                 }
  122.                 j += randomIndex;
  123.             }
  124.         }
  125.         cards = tempCards;
  126.     }
  127.  
  128.     //  If you don't know what a cut is, get off the Blackjack table.
  129.     void Cut()
  130.     {
  131.         int index = Random(cards.length()/4, cards.length()*3/4);
  132.         array<CCard@> tempCards;
  133.         for (uint i = index; i < cards.length(); i++)
  134.         {
  135.             tempCards.insertLast(cards[i]);
  136.         }
  137.         for (int i = 0; i < index; i++)
  138.         {
  139.             tempCards.insertLast(cards[i]);
  140.         }
  141.         cards = tempCards;
  142.     }
  143.  
  144.     //  Used for debugging only
  145.     void List()
  146.     {
  147.         Log("Listing cards in shoe:");
  148.         for (uint i = 0; i < cards.length(); i++)
  149.         {
  150.             string log = " - ";
  151.             for (int j = 0; j < cards[i].Value(); j++)
  152.             {
  153.                 log += "  ";
  154.             }
  155.             Log(log + cards[i].GetCardNotation());
  156.         }
  157.         Log("Total of " + cards.length() + " cards." );
  158.     }
  159.  
  160.     //  Takes next card, returning it and incrementing the index.
  161.     CCard@ TakeNextCard()
  162.     {
  163.         if (currentCardIndex < cards.length())
  164.         {
  165.             CCard@ currentCard = cards[currentCardIndex];
  166.             currentCardIndex += 1;
  167.             return currentCard;
  168.         }
  169.         else
  170.         {
  171.             Shuffle();
  172.             CCard@ currentCard = cards[0];
  173.             currentCardIndex = 1;
  174.             return currentCard;
  175.         }
  176.     }
  177.  
  178.     /**< For testing purposes */
  179.  
  180.     //  Even money option shall not be offered.
  181.     void InsertTestValuesForPlayerBlackjackAndDealerNotAce()
  182.     {
  183.         //  dealer
  184.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  185.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  186.         //  player
  187.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  188.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  189.     }
  190.  
  191.     void InsertTestValuesForPlayerBlackjackAndDealerIs21()
  192.     {
  193.         //  dealer
  194.         cards.insertFirst(CCard(CARD_TYPE_SEVEN, CARD_SUIT_SPADES));
  195.         cards.insertFirst(CCard(CARD_TYPE_THREE, CARD_SUIT_SPADES));
  196.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  197.         //  player
  198.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  199.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  200.     }
  201.  
  202.     void InsertTestValuesForPlayerBlackjackAndDealerBlackjack()
  203.     {
  204.         //  dealer
  205.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  206.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  207.         //  player
  208.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  209.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  210.     }
  211.  
  212.     void InsertTestValuesForPlayerBlackjackAndDealerStandOn17()
  213.     {
  214.         //  dealer
  215.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  216.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  217.         //  player
  218.         cards.insertFirst(CCard(CARD_TYPE_SIX, CARD_SUIT_SPADES));
  219.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  220.     }
  221.  
  222.     void InsertTestValuesForPlayerDoubleDown20AndDealerBlackjack()
  223.     {
  224.         //  player
  225.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  226.         //  dealer start hand
  227.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  228.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  229.         //  player start hand
  230.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  231.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  232.     }
  233.  
  234.     void InsertTestValuesForPlayerDoubleDown20AndDealer18()
  235.     {
  236.         //  player
  237.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  238.         //  dealer start hand
  239.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  240.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_SPADES));
  241.         //  player start hand
  242.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  243.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  244.     }
  245.  
  246.     void InsertTestValuesForPlayerDoubleDown20AndDealer21()
  247.     {
  248.         //  dealer
  249.         cards.insertFirst(CCard(CARD_TYPE_THREE, CARD_SUIT_SPADES));
  250.         //  player
  251.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  252.         //  dealer start hand
  253.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  254.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_SPADES));
  255.         //  player start hand
  256.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  257.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  258.     }
  259.  
  260.     void InsertTestValuesForPlayerDoubleDownAfterThirdCard20AndDealer18()
  261.     {
  262.         //  player
  263.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_SPADES));
  264.         cards.insertFirst(CCard(CARD_TYPE_TWO, CARD_SUIT_SPADES));
  265.         //  dealer first hand
  266.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_SPADES));
  267.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  268.         //  player first hand
  269.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  270.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  271.     }
  272.  
  273.     void InsertTestValuesForPlayerDoubleDownAndBustAndDealer18()
  274.     {
  275.         //  player
  276.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_SPADES));
  277.         //  dealer first hand
  278.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_SPADES));
  279.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  280.         //  player first hand
  281.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  282.         cards.insertFirst(CCard(CARD_TYPE_FIVE, CARD_SUIT_SPADES));
  283.     }
  284.  
  285.     /**< Test data for Lucky Ladies side bet. */
  286.     void InsertTestValuesForLuckyLadiesQohpAndDealerBlackjack()
  287.     {
  288.         //  dealer first hand
  289.         cards.insertFirst(CCard(CARD_TYPE_ACE, CARD_SUIT_SPADES));
  290.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  291.         //  player first hand
  292.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_HEARTS));
  293.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_HEARTS));
  294.     }
  295.     void InsertTestValuesForLuckyLadiesQohpAndDealer20()
  296.     {
  297.         //  dealer first hand
  298.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  299.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  300.         //  player first hand
  301.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_HEARTS));
  302.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_HEARTS));
  303.     }
  304.     void InsertTestValuesForLuckyLadiesMatch()
  305.     {
  306.         //  dealer first hand
  307.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  308.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  309.         //  player first hand
  310.         cards.insertFirst(CCard(CARD_TYPE_KING, CARD_SUIT_HEARTS));
  311.         cards.insertFirst(CCard(CARD_TYPE_KING, CARD_SUIT_HEARTS));
  312.     }
  313.     void InsertTestValuesForLuckyLadiesSuited()
  314.     {
  315.         //  dealer first hand
  316.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  317.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  318.         //  player first hand
  319.         cards.insertFirst(CCard(CARD_TYPE_KING, CARD_SUIT_HEARTS));
  320.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_HEARTS));
  321.     }
  322.     void InsertTestValuesForLuckyLadiesUnSuited()
  323.     {
  324.         //  dealer first hand
  325.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  326.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  327.         //  player first hand
  328.         cards.insertFirst(CCard(CARD_TYPE_KING, CARD_SUIT_CLUBS));
  329.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_HEARTS));
  330.     }
  331.  
  332.     /**< Test data for Perfect Pair side bet. */
  333.     void InsertTestValuesForPerfectPairPlayerPerfectPair()
  334.     {
  335.         //  dealer first hand
  336.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  337.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  338.         //  player first hand
  339.         cards.insertFirst(CCard(CARD_TYPE_SEVEN, CARD_SUIT_HEARTS));
  340.         cards.insertFirst(CCard(CARD_TYPE_SEVEN, CARD_SUIT_HEARTS));
  341.     }
  342.     void InsertTestValuesForPerfectPairPlayerColoredPair()
  343.     {
  344.         //  dealer first hand
  345.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  346.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  347.         //  player first hand
  348.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_HEARTS));
  349.         cards.insertFirst(CCard(CARD_TYPE_EIGHT, CARD_SUIT_DIAMONDS));
  350.     }
  351.     void InsertTestValuesForPerfectPairPlayerMixedPair()
  352.     {
  353.         //  dealer first hand
  354.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  355.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  356.         //  player first hand
  357.         cards.insertFirst(CCard(CARD_TYPE_NINE, CARD_SUIT_HEARTS));
  358.         cards.insertFirst(CCard(CARD_TYPE_NINE, CARD_SUIT_SPADES));
  359.     }
  360.  
  361.     /**< Test data for Royal Match side bet. */
  362.     void InsertTestValuesForRoyalMatchPlayerRoyalMatch()
  363.     {
  364.         //  dealer first hand
  365.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  366.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  367.         //  player first hand
  368.         cards.insertFirst(CCard(CARD_TYPE_KING, CARD_SUIT_HEARTS));
  369.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_HEARTS));
  370.     }
  371.     void InsertTestValuesForRoyalMatchPlayerSuited()
  372.     {
  373.         //  dealer first hand
  374.         cards.insertFirst(CCard(CARD_TYPE_TEN, CARD_SUIT_SPADES));
  375.         cards.insertFirst(CCard(CARD_TYPE_JACK, CARD_SUIT_SPADES));
  376.         //  player first hand
  377.         cards.insertFirst(CCard(CARD_TYPE_QUEEN, CARD_SUIT_SPADES));
  378.         cards.insertFirst(CCard(CARD_TYPE_KING, CARD_SUIT_HEARTS));
  379.     }
  380. };
Advertisement
RAW Paste Data Copied
Advertisement