Sutto / blackjackdrill forked from jkk/blackjackdrill

Blackjack Basic Strategy Driller

This URL has Read+Write access

blackjackdrill / blackjack.js
100644 203 lines (184 sloc) 6.594 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
var kinds = {
  'S' : 'stood',
  'H' : 'hit',
  'P' : 'split',
  'D' : 'doubled'
}
 
var upcards = '2 3 4 5 6 7 8 9 10 A'.split(/ /);
    myhands = ('5 6 7 8 9 10 11 12 13 14 15 16 17 ' +
               'A-2 A-3 A-4 A-5 A-6 A-7 A-8 A-9 ' +
               '2-2 3-3 4-4 5-5 6-6 7-7 8-8 9-9 T-T A-A').split(/[\s|\n]/),
    strategy = getStrategy(),
    deck1 = getDeck(),
    deck = shuffle(deck1.concat(deck1.concat(deck1.concat(deck1.concat(deck1.concat(deck1)))))),
    pickedCardIndexes = [],
    cards = {},
    answered = false;
 
function getDeck() {
    var ranks = '2 3 4 5 6 7 8 9 10 j q k a'.split(/ /),
        suits = 'c s h d'.split(/ /),
        deck = [];
    for (var i in ranks)
        for (var j in suits)
            deck.push(suits[j] + ranks[i]);
    return deck;
}
 
function shuffle(v) {
    for(var j, x, i = v;
        i;
        j = parseInt(Math.random() * i), x = v[--i], v[i] = v[j], v[j] = x);
    return v;
}
 
function getStrategy(ruleset) {
    // # decks, dealer on soft 17, double after split, surrender, peek
    ruleset = ruleset || 'burswood';
    var strategyStr = {
        '6d,s17,das,ns,p': 'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHDDDDHHHHHDDDDDDDDHH' +
                           'DDDDDDDDDHHHSSSHHHHHSSSSSHHHHHSSSSSHHHHHSSSSSHHHHHSSSSSHHHHH' +
                           'SSSSSSSSSSHHHDDHHHHHHHHDDHHHHHHHDDDHHHHHHHDDDHHHHHHDDDDHHHHH' +
                           'SDDDDSSHHHSSSSSSSSSSSSSSSSSSSSPPPPPPHHHHPPPPPPHHHHHHHPPHHHHH' +
                           'DDDDDDDDHHPPPPPHHHHHPPPPPPHHHHPPPPPPPPPPPPPPPSPPSSSSSSSSSSSS' +
                           'PPPPPPPPPP',
        'burswood' : 'HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHDDDDHHHHHDDDDDDDDHH' +
                           'DDDDDDDDDHHHSSSHHHHHSSSSSHHHHHSSSSSHHHHHSSSSSHHHHHSSSSSHHHHH' +
                           'SSSSSSSSSSHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH' +
                           'SSSSSSSHHHSSSSSSSSSSSSSSSSSSSSPPPPPPHHHHPPPPPPHHHHHHHPPHHHHH' +
                           'DDDDDDDDHHPPPPPHHHHHPPPPPPHHHHPPPPPPPPHHPPPPPSPPSSSSSSSSSSSS' +
                           'PPPPPPPPPH'};
    var strategy = {};
    for (var myhand, i = 0; myhand = myhands[i]; i++) {
        strategy[myhand] = {};
        for (var upcard, j = 0; upcard = upcards[j]; j++)
            strategy[myhand][upcard] = strategyStr[ruleset].charAt(i * upcards.length + j);
    }
    return strategy;
}
 
function getStrategyTableHtml() {
    var action, upcard, myhand, i, j,
        html = '<table id="strategy">' +
               '<tr class="upcards-label"><td>&nbsp;</td><th colspan="' +
               (upcards.length) + '">Dealer Upcard</th></tr>' +
               '<tr class="upcards-row"><td>Your Hand</td>';
    for (upcard, j = 0; upcard = upcards[j]; j++)
        html += '<th>' + upcard + '</th>';
    html += '</th>';
    for (myhand, i = 0; myhand = myhands[i]; i++) {
        html += '<tr class="myhand-row"><th>' + myhand + '</th>';
        for (upcard, j = 0; upcard = upcards[j]; j++) {
            action = strategy[myhand][upcard];
            html += '<td class="' + action + '" id="cell-' + upcard +
                '-' + myhand + '">' + action + '</td>';
        }
        html += '</tr>';
    }
    html += '</table>';
    return html;
}
 
function indexOf(arr, needle) {
    for (var i = 0; i < arr.length; i++)
        if (arr[i] === needle)
            return i;
    return -1;
}
 
function getCardIndex() {
    var index = -1;
    while (index == -1 || indexOf(pickedCardIndexes, index) != -1)
        index = Math.floor(Math.random() * deck.length);
    pickedCardIndexes.push(index);
    return index;
}
 
function deal() {
    pickedCardIndexes = [];
    answered = false;
    $('#correct').hide();
    $('#response').html('');
    $('#strategy td.highlighted').removeClass('highlighted');
    cards.dealer = deck[getCardIndex()];
    var value;
    while (typeof value == 'undefined' || value == 21) {
        cards.my1 = deck[getCardIndex()];
        cards.my2 = deck[getCardIndex()];
        value = getCardValue(cards.my1, true) + getCardValue(cards.my2, true);
    }
    $('#dealer-cards').html("<img src='images/" + cards.dealer + ".png'><img src='images/back.png'>");
    $('#my-cards').html("<img src='images/" + cards.my1 + ".png'><img src='images/" + cards.my2 + ".png'>");
}
 
function getCardValue(card, aceEleven) {
    var rank = card.substring(1),
        value = parseInt(rank, 10);
    if (!isNaN(value))
        return value;
    if (rank == 'a')
        return aceEleven ? 11 : 'A';
    return 10;
}
 
function checkAction(action) {
    if (answered) {
        deal();
        return;
    }
    var upcard, my1, my2, myhand, correctAction;
    upcard = getCardValue(cards.dealer);
    my1 = getCardValue(cards.my1);
    my2 = getCardValue(cards.my2);
    if (my1 == 10 && my2 == 10)
        myhand = 'T-T';
    else if (my1 == my2)
        myhand = my1 + '-' + my2;
    else if (my1 == 'A')
        myhand = my1 + '-' + my2;
    else if (my2 == 'A')
        myhand = my2 + '-' + my1;
    else
        myhand = (my1 + my2) > 17 ? 17 : (my1 + my2);
    correctAction = strategy[myhand][upcard];
    if (action == correctAction)
        $('#response').html("<div class='correct'>Correct</div>");
    else {
        $('#response').html("<div class='wrong'>Wrong</div>");
        var value = $('#cell-' + upcard + '-' + myhand).html();
        $("#correct").show().html("Sorry chap, you should of " + kinds[value] + " just then.");
    }
    $('#cell-' + upcard + '-' + myhand).addClass('highlighted');
    
    answered = true;
}
 
function initBlackjack() {
  
  
  $('#strategy-shell').html(getStrategyTableHtml());
 
  $('#action li').click(function() {
      var action;
      switch (this.id) {
          case 'hit': action = 'H'; break;
          case 'stand': action = 'S'; break;
          case 'double': action = 'D'; break;
          case 'split': action = 'P'; break;
      }
      if (!action)
          return;
      checkAction(action);
  });
 
  $(document).keypress(function(evt) {
      if (answered) {
          $('#correct').hide();
          deal();
          if (evt.which == 32) {
              evt.stopPropagation();
              evt.preventDefault();
          }
          return;
      }
      var action;
      switch (evt.which) {
          case 49: action = 'H'; break;
          case 50: action = 'S'; break;
          case 51: action = 'D'; break;
          case 52: action = 'P'; break;
      }
      if (!action)
          return;
      checkAction(action);
  });
 
  $('#response').click(function() {
      deal();
  });
 
  deal();
}