Advertisement
Guest User

ArmA BlackJack

a guest
Sep 5th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQF 6.25 KB | None | 0 0
  1. hint "Gellow, Start BlackJack by executing your action";
  2.  
  3. cardSuits = [
  4.     "Spades", "Hearts", "Diamonds", "Clubs"
  5. ];
  6.  
  7. cardFaces = [
  8.     "2", "3", "4", "5", "6", "7", "8", "9",
  9.             "10", "J", "Q", "K", "A"
  10. ];
  11.  
  12. cardDeck = [];
  13. dealerCards = [];
  14. playerCards = [];
  15.  
  16. getCard = {
  17.     _cardNumber = _this select 0;
  18.  
  19.     _cardSuit = floor (_cardNumber / 13);
  20.     _cardFace = _cardNumber % 13;
  21.  
  22.     [_cardSuit, _cardFace];
  23. };
  24.  
  25. getCardNameFromNumber = {
  26.     format ["%1 %2", (cardSuits select (_this select 0)), (cardFaces select (_this select 1))];
  27. };
  28.  
  29. getCardFromDeck = {
  30.     _card = selectRandom cardDeck;
  31.  
  32.     // Remove card from deck
  33.     cardDeck deleteAt (cardDeck find _card);
  34.  
  35.     _card;
  36. };
  37.  
  38. getValueOfCards = {
  39.     _cards = _this select 0;
  40.  
  41.     _totalValue = 0;
  42.     _aces = 0;
  43.  
  44.     {
  45.         _card = [_x] call getCard;
  46.         _cardFace = cardFaces select (_card select 1);
  47.  
  48.         switch (_cardFace) do {
  49.             //Put the aces appart for now
  50.             case "A": { _aces = _aces + 1; };
  51.             case "K": { _totalvalue = _totalValue + 10; };
  52.             case "Q": { _totalvalue = _totalValue + 10; };
  53.             case "J": { _totalvalue = _totalValue + 10; };
  54.             default { _totalValue = _totalValue + (parseNumber _cardFace); };
  55.         };
  56.     } forEach _cards;
  57.  
  58.     if (_aces > 0) then {
  59.         if (_totalValue + (_aces * 11) > 21) then {
  60.             if ((_totalValue + 11) <= 21 - (_aces - 1)) then {
  61.                 _totalValue = _totalValue + 11 + (_aces - 1);
  62.             } else {
  63.                 _totalValue = _totalValue + _aces;
  64.             };
  65.         } else {
  66.             _totalValue = _totalValue + (_aces * 11);
  67.         };
  68.     };
  69.  
  70.     _totalValue;
  71. };
  72.  
  73. handToString = {
  74.     _hand = _this select 0;
  75.     _str = "";
  76.  
  77.     {
  78.         _str = format ["%1%2, ", _str, ([_x] call getCard) call getCardNameFromNumber];
  79.     } forEach _hand;
  80.  
  81.     _str;
  82. };
  83.  
  84. dealerGrabCards = {
  85.     while { ([dealerCards] call getValueOfCards) < 17; } do {
  86.         dealerCards append [call getCardFromDeck];
  87.     };
  88.  
  89.     _dealerValue = [dealerCards] call getValueOfCards;
  90.     _playerValue = [playerCards] call getValueOfCards;
  91.  
  92.     if (_dealerValue > 21) then {
  93.         hint format [
  94.             "Dealer is > 21 (%4) (%1)!\n\nYou have %2\n\nYour hand value: %3\n\nYou win!",
  95.             [dealerCards] call handToString,
  96.             [playerCards] call handToString,
  97.             _playerValue,
  98.             _dealerValue
  99.         ];
  100.     } else {
  101.         if (_dealerValue isEqualTo _playerValue) then {
  102.             hint format [
  103.                 "Dealer has %4 (%1)!\n\nYou have %2\n\nYour hand value: %3\n\nIt Equals, You get your money back.",
  104.                 [dealerCards] call handToString,
  105.                 [playerCards] call handToString,
  106.                 _playerValue,
  107.                 _dealerValue
  108.             ];
  109.         } else {
  110.             if (_dealerValue > _playerValue) then {
  111.                 hint format [
  112.                     "Dealer has %4 (%1)!\n\nYou have %2\n\nYour hand value: %3\n\nThe dealer is closer to 21, You lose!",
  113.                     [dealerCards] call handToString,
  114.                     [playerCards] call handToString,
  115.                     _playerValue,
  116.                     _dealerValue
  117.                 ];
  118.             } else {
  119.                 hint format [
  120.                     "Dealer has %4 (%1)!\n\nYou have %2\n\nYour hand value: %3\n\nYou are closer to 21, You win!",
  121.                     [dealerCards] call handToString,
  122.                     [playerCards] call handToString,
  123.                     _playerValue,
  124.                     _dealerValue
  125.                 ];
  126.             }
  127.         }
  128.     }
  129.  
  130.     call startBlackjack;
  131. };
  132.  
  133. getPlayerTurnStatus = {
  134.     _playerValue = [playerCards] call getValueOfCards;
  135.     _dealerValue = [dealerCards] call getValueOfCards;
  136.  
  137.     if (_dealerValue isEqualTo 21) then {
  138.         if (_playerValue isEqualTo 21) then {
  139.             hint format [
  140.                 "Dealer has BlackJack (%1 and %2)!\n\nYou have %3\n\nYour hand value: %4\n\nYou have BlackJack too! You get your money back.",
  141.                 ([(dealerCards select 0)] call getCard) call getCardNameFromNumber,
  142.                 ([(dealerCards select 1)] call getCard) call getCardNameFromNumber,
  143.                 [playerCards] call handToString,
  144.                 _playerValue
  145.             ];
  146.  
  147.             call startBlackjack;
  148.         } else {
  149.             hint format [
  150.                 "Dealer has BlackJack (%1 and %2)!\n\nYou have %3\n\nYour hand value: %4\n\nYou Lose!",
  151.                 ([(dealerCards select 0)] call getCard) call getCardNameFromNumber,
  152.                 ([(dealerCards select 1)] call getCard) call getCardNameFromNumber,
  153.                 [playerCards] call handToString,
  154.                 _playerValue
  155.             ];
  156.  
  157.             call startBlackjack;
  158.         }
  159.     } else {
  160.         if (_playerValue isEqualTo 21) then {
  161.             if ((count playerCards) > 2) then {
  162.                 hint format [
  163.                     "Dealer has %1\n\You have %2\n\nYour hand value: %3",
  164.                     ([(dealerCards select 0)] call getCard) call getCardNameFromNumber,
  165.                     [playerCards] call handToString,
  166.                     _playerValue
  167.                 ];
  168.             } else {
  169.                 hint format [
  170.                     "Dealer has %1\n\You have %2\n\nYour hand value: %3\n\nYou have BlackJack!",
  171.                     ([(dealerCards select 0)] call getCard) call getCardNameFromNumber,
  172.                     [playerCards] call handToString,
  173.                     _playerValue
  174.                 ];
  175.             };
  176.  
  177.             call dealerGrabCards;
  178.         } else {
  179.             if (_playerValue > 21) then {
  180.                 hint format [
  181.                     "Dealer has %1\n\nYou have %2\n\nYour hand value: %3\n\nYour value is over 21, You Lose!",
  182.                     ([(dealerCards select 0)] call getCard) call getCardNameFromNumber,
  183.                     [playerCards] call handToString,
  184.                     _playerValue
  185.                 ];
  186.  
  187.                 call startBlackjack;
  188.             } else {
  189.                 hint format [
  190.                     "Dealer has %1\n\nYou have %2\n\nYour hand value: %3",
  191.                     ([(dealerCards select 0)] call getCard) call getCardNameFromNumber,
  192.                     [playerCards] call handToString,
  193.                     _playerValue
  194.                 ];
  195.  
  196.                 call giveChoices;
  197.             };
  198.         };
  199.     };
  200. };
  201.  
  202. fold = {
  203.     call dealerGrabCards;
  204. };
  205.  
  206. hit = {
  207.     playerCards append [call getCardFromDeck];
  208.  
  209.     call getPlayerTurnStatus;
  210. };
  211.  
  212. giveChoices = {
  213.     player addAction ['Fold', {removeAllActions (_this select 0); call fold;}, [player]];
  214.     player addAction ['Hit', {removeAllActions (_this select 0); call hit;}, [player]];
  215. };
  216.  
  217. initBlackjack  = {
  218.     _caller = _this select 0;
  219.  
  220.     if (isNull _caller) exitWith {};
  221.  
  222.     //reset vars
  223.     cardDeck = [];
  224.     playerCards = [];
  225.     dealerCards = [];
  226.     playerHits = 0;
  227.  
  228.     // Make the carddeck
  229.     for "_i" from 1 to 52 do {
  230.         cardDeck set [_i - 1, _i];
  231.     };
  232.  
  233.     // Shuffle the carddeck
  234.     cardDeck call BIS_fnc_arrayShuffle;
  235.  
  236.     // Get Dealer Cards
  237.     for "_i" from 0 to 1 do {
  238.         dealerCards set [_i, call getCardFromDeck];
  239.     };
  240.  
  241.     // Give the player cards
  242.     for "_i" from 0 to 1 do {
  243.         playerCards set [_i, call getCardFromDeck];
  244.     };
  245.  
  246.     call getPlayerTurnStatus;
  247. };
  248.  
  249. startBlackjack = {
  250.     player addAction ['Start Blackjack', {(_this select 0) removeaction (_this select 2); call initBlackjack;}, [player]];
  251. };
  252.  
  253. call startBlackjack;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement