Advertisement
dimipan80

Exams - Gambling (on JavaScript)

Dec 31st, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You’ll be provided with some amount of cash C you may use to bet on a single hand of a card game called Vokan.
  2.  You will be shown the house’s hand consisting of four cards (drawn from a standard deck of 52 cards).
  3.  The hand’s strength is determined solely by the face values of the cards (suits are ignored).
  4.  Cards with faces 2 to 10 increase the hand’s strength with their numeric value. J, Q, K and A add 11, 12, 13 and
  5.  14 points respectively. Then, the four cards are shuffled back into the deck. At this point you’ll have to
  6.  decide whether you want to play or not. Continuing the game means you’ll draw four cards from the deck,
  7.  one at a time, shuffling back each card before drawing the next; you will then calculate your cards’
  8.  combined strength and compare it to the house’s result.
  9.  You’ll need to figure out the ratio between the number of winning hands (hands that are stronger than the house’s)
  10.  and the total number of all possible hands in order to find the probability of you beating the house.
  11.  If the probability is < 50% you should quit, otherwise the game is worth the risk.
  12.  Assume you bet your entire cash deposit C and the bank matches your bet 1:1; thus the pot’s value will be 2*C.
  13.  Your expected winnings are calculated as (% probability of winning) * (pot amount).
  14.  The input consist of two lines. At the first line you’ll be given some amount of cash C.
  15.  At the second line you’ll be given the house’s hand as a sequence of four cards (faces only),
  16.  each separated by a single whitespace.
  17.  The output should consist of two lines. On the first line, print your decision - “FOLD” if the game isn’t worth
  18.  the risk, or “DRAW” otherwise. On the second line, print your expected winnings rounded to two digits
  19.  after the decimal point (you should use “.” as the decimal separator). */
  20.  
  21. "use strict";
  22.  
  23. function solve(args) {
  24.     var cash = Number(args[0]);
  25.     var houseHand = args[1].split(/\s/);
  26.     var houseStrength = 0;
  27.     for (var i = 0; i < houseHand.length; i += 1) {
  28.         var card = houseHand[i];
  29.         switch (card) {
  30.             case 'J':
  31.                 houseStrength += 11;
  32.                 break;
  33.             case 'Q':
  34.                 houseStrength += 12;
  35.                 break;
  36.             case 'K':
  37.                 houseStrength += 13;
  38.                 break;
  39.             case 'A':
  40.                 houseStrength += 14;
  41.                 break;
  42.             default:
  43.                 houseStrength += parseInt(card);
  44.                 break;
  45.         }
  46.     }
  47.    
  48.     var totalHands = 0;
  49.     var winningHands = 0;
  50.     var c1, c2, c3, c4;
  51.     for (c1 = 2; c1 < 15; c1 += 1) {
  52.         for (c2 = 2; c2 < 15; c2 += 1) {
  53.             for (c3 = 2; c3 < 15; c3 += 1) {
  54.                 for (c4 = 2; c4 < 15; c4 += 1) {
  55.                     totalHands += 1;
  56.                     if ((c1 + c2 + c3 + c4) > houseStrength) {
  57.                         winningHands += 1;
  58.                     }
  59.                 }
  60.             }
  61.         }
  62.     }
  63.    
  64.     var probability = winningHands / totalHands;
  65.     var expectedWinnings = (cash * 2 * probability).toFixed(2);
  66.     console.log((probability < 0.5) ? 'FOLD' : 'DRAW');
  67.     console.log(expectedWinnings);
  68. }
  69.  
  70. solve(['100',
  71.     '2 7 9 A'
  72. ]);
  73.  
  74. solve(['203.03',
  75.     'A A K 3'
  76. ]);
  77.  
  78. solve(['5',
  79.     '8 4 Q 10'
  80. ]);
  81.  
  82. solve(['150000.37',
  83.     '10 4 4 J'
  84. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement