Code covered by the MathWorks Limited License

# Blackjack computational kernel

### Edric Ellis (view profile)

Simulation of a single hand of blackjack

blackjack_kernel
```function s = blackjack_kernel
%BLACKJACK_KERNEL  Simulate a single hand of blackjack
%   S = BLACKJACK_KERNEL returns the result of playing a single hand of
%   blackjack.

%   Copyright 2007-2012 The MathWorks, Inc.

bet  = 10;
bet1 = bet;
P    = iDeal();         % Player's hand.
D    = iDeal();         % Dealer's hand.
P    = [P iDeal()];
D    = [D -iDeal()];    % Hide dealer's hole card.
P2   = []; bet2 = 0;    % No split by default

% Split pairs.
split = mod(P(1), 13) == mod(P(2), 13);
if split
split = iPair(iValue(P(1)), iValue(D(1)));
end

if split
P2 = P(2);
P = [P(1) iDeal()];
bet2 = bet1;
end

% Play player's hand(s).
[P, bet1] = iPlayHand(P, D, bet1);
if split
P2 = [P2 iDeal()];
[P2, bet2] = iPlayHand(P2, D, bet2);
end

% Play dealer's hand.
D(2) = -D(2);     % Reveal dealer's hole card.
while iValue(D) <= 16
D = [D iDeal()]; %#ok<AGROW>
end

% Calculate the payoff.
s = iPayoff(P, D, split, bet1);
if split
s = s + iPayoff(P2, D, split, bet2);
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iDeal - Deal one card.
function c = iDeal()

persistent deck ncards
if isempty(deck) || ncards <  6
% Shuffle four decks.
deck = [1:52 1:52 1:52 1:52];
ncards = length(deck);
deck = deck(randperm(ncards));
end
c = deck(ncards);
ncards = ncards - 1;
end % End of iDeal.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iValueHard - Evaluate hand.
function v = iValueHard(X)
X = mod(X - 1, 13) + 1;
X = min(X, 10);
v = sum(X);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iValue - Evaluate hand, promoting soft aces.
function v = iValue(X)
% Evaluate hand.
X = mod(X - 1, 13) + 1;
X = min(X, 10);
v = sum(X);
% Promote soft ace
if any(X == 1) && v <= 11
v = v + 10;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iPlayHand - play the player's hand.
function [P, bet] = iPlayHand(P, D, bet)

while iValue(P) < 21
% 0 = stand
% 1 = hit
% 2 = double down
if any(mod(P, 13) == 1) && iValueHard(P) <= 10
strat = iSoft(iValue(P)-11, iValue(D(1)));
else
strat = iHard(iValue(P), iValue(D(1)));
end
if length(P) > 2 && strat == 2
strat = 1;
end
switch strat
case 0
break
case 1
P = [P iDeal]; %#ok<AGROW>
case 2
% Double down.
% Double bet and get one more card.
bet = 2*bet;
P = [P iDeal];  %#ok<AGROW>
break
otherwise
break
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iPayoff - return the payoff.
function s = iPayoff(P, D, split, bet)
valP = iValue(P);
valD = iValue(D);
if valP == 21 && length(P) == 2 && ...
~(valD == 21 && length(D) == 2) && ~split
s = 1.5*bet;
elseif valP > 21
s = -bet;
elseif valD > 21
s = bet;
elseif valD > valP
s = -bet;
elseif valD < valP
s = bet;
else
s = 0;
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iHard - Strategy for hands without aces.
% strategy = iHard(player's_total, dealer's_upcard)
function strat = iHard(p, d)

% 0 = stand
% 1 = hit
% 2 = double down

persistent HARD
if isempty(HARD)
n = NaN; % Not possible.
% Dealer shows:
%        2 3 4 5 6 7 8 9 T A
HARD = [ ...
1    n n n n n n n n n n
2    1 1 1 1 1 1 1 1 1 1
3    1 1 1 1 1 1 1 1 1 1
4    1 1 1 1 1 1 1 1 1 1
5    1 1 1 1 1 1 1 1 1 1
6    1 1 1 1 1 1 1 1 1 1
7    1 1 1 1 1 1 1 1 1 1
8    1 1 1 1 1 1 1 1 1 1
9    2 2 2 2 2 1 1 1 1 1
10   2 2 2 2 2 2 2 2 1 1
11   2 2 2 2 2 2 2 2 2 2
12   1 1 0 0 0 1 1 1 1 1
13   0 0 0 0 0 1 1 1 1 1
14   0 0 0 0 0 1 1 1 1 1
15   0 0 0 0 0 1 1 1 1 1
16   0 0 0 0 0 1 1 1 1 1
17   0 0 0 0 0 0 0 0 0 0
18   0 0 0 0 0 0 0 0 0 0
19   0 0 0 0 0 0 0 0 0 0
20   0 0 0 0 0 0 0 0 0 0];
end
strat = HARD(p, d);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iSoft - Strategy array for hands with aces.
% strategy = iSoft(player's_total, dealer's_upcard)
function strat = iSoft(p, d)

% 0 = stand
% 1 = hit
% 2 = double down

persistent SOFT
if isempty(SOFT)
n = NaN; % Not possible.
% Dealer shows:
%       2 3 4 5 6 7 8 9 T A
SOFT = [ ...
1   n n n n n n n n n n
2   1 1 2 2 2 1 1 1 1 1
3   1 1 2 2 2 1 1 1 1 1
4   1 1 2 2 2 1 1 1 1 1
5   1 1 2 2 2 1 1 1 1 1
6   2 2 2 2 2 1 1 1 1 1
7   0 2 2 2 2 0 0 1 1 0
8   0 0 0 0 0 0 0 0 0 0
9   0 0 0 0 0 0 0 0 0 0];
end
strat = SOFT(p, d);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% iPair - Strategy for splitting pairs.
% strategy = iPair(paired_card, dealer's_upcard)
function strat = iPair(p, d)

% 0 = keep pair
% 1 = split pair

persistent PAIR
if isempty(PAIR)
n = NaN; % Not possible.
% Dealer shows:
%        2 3 4 5 6 7 8 9 T A
PAIR = [ ...
1    n n n n n n n n n n
2    1 1 1 1 1 1 0 0 0 0
3    1 1 1 1 1 1 0 0 0 0
4    0 0 0 1 0 0 0 0 0 0
5    0 0 0 0 0 0 0 0 0 0
6    1 1 1 1 1 1 0 0 0 0
7    1 1 1 1 1 1 1 0 0 0
8    1 1 1 1 1 1 1 1 1 1
9    1 1 1 1 1 0 1 1 0 0
10   0 0 0 0 0 0 0 0 0 0
11   1 1 1 1 1 1 1 1 1 1];
end
strat = PAIR(p, d);
end
```