⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 blackjack.m

📁 This m file have many simulation about environment
💻 M
📖 第 1 页 / 共 2 页
字号:
function [P,bet] = playhand(hand,P,D,bet)
% Play player's hand

while value(P) < 21
   % 0 = stand
   % 1 = hit
   % 2 = double down
   if any(mod(P,13)==1) & valuehard(P)<=10
      strat = soft(value(P)-11,value(D(1)));
   else
      strat = hard(value(P),value(D(1)));
   end
   if length(P) > 2 & strat == 2
      strat = 1;
   end
   if ~isempty(hand)
      show(hand,P)
      show('Dealer',D)
      strat = bjbuttonclick('hit',strat+1,length(P)>2);
   end
   switch strat
       case 0
          break
       case 1
          P = [P deal];
       case 2
          % Double down.
          % Double bet and get one more card
          bet = 2*bet;
          P = [P deal];
          break
       otherwise
          break
   end
end


% ------------------------

function s = payoff(who,P,D,split,bet)
% Payoff
detail = ~isempty(who);
fs = 20;
valP = value(P);
valD = value(D);
if valP == 21 & length(P) == 2 & ...
   ~(valD == 21 & length(D) == 2) & ~split
   s = 1.5*bet;
   if detail, str = ['BLACKJACK: +' int2str(s)]; end
elseif valP > 21
   s = -bet;
   if detail, str = ['BUST: ' int2str(s)]; end
elseif valD > 21
   s = bet;
   str = ['WIN: +' int2str(s)];
   if detail
      text(min(1.5*length(D)-4.5,2.75),-2.5,'BUST','fontsize',fs)
   end
elseif valD > valP
   s = -bet;
   if detail, str = ['LOSE: ' int2str(s)]; end
elseif valD < valP
   s = bet;
   if detail, str = ['WIN: +' int2str(s)]; end
else
   s = 0;
   if detail, str = 'PUSH'; end
end
if detail
   x = min(1.5*length(P)-4.5,2.75);
   if isequal(who,'Player')
      y = 2.5;
   else
      y = 0;
   end
   text(x,y,str,'fontsize',fs)
end


% ------------------------

function show(who,H)
% Displays one hand
switch who
   case 'Player', y = 2.5;
   case 'Split', y = 0;
   case 'Dealer', y = -2.5;
end
x = -4;
for j = 1:length(H)
   card(x,y,H(j),length(H))
   x = x + 1.5;
end


% ------------------------

function card(x,y,v,gray)
% card(x,y,v) plots v-th card at position (x,y).
z = exp((0:16)/16*pi/2*i)/16;
edge = [z+1/2+7*i/8 i*z-1/2+7*i/8 -z-1/2-7*i/8 -i*z+1/2-7*i/8 9/16+7*i/8];
pips = {'A','2','3','4','5','6','7','8','9','10','J','Q','K'};
if v <= 0
   % Hole card
   patch(real(edge)+x,imag(edge)+y,[0 0 2/3])
else
   fs = 20;
   s = ceil(v/13);
   v = mod(v-1,13)+1;
   x1 = x;
   if v==10, x1 = x1-.2; end
   offwhite = [1 1 1];
   if y == 0 & gray == 1, offwhite = [.75 .75 .75]; end
   patch(real(edge)+x,imag(edge)+y,offwhite)
   switch s
      case {1,4}, redblack = [0 0 0];
      case {2,3}, redblack = [2/3 0 0];
   end
   if ispc
      % PC has symbol font with card suits.
      text(x1-.2,y,pips{v},'fontname','courier','fontsize',fs, ...
         'fontweight','bold','color',redblack)
      text(x,y+.025,char(166+s),'fontname','symbol','fontsize',fs, ...
         'color',redblack)
   else
      text(x1-.1,y,pips{v},'fontname','courier','fontsize',fs, ...
         'fontweight','bold','color',redblack)
   end
end


% ------------------------

function val = bjbuttonclick(kase,basic,disable)
bjb = bjbuttons(kase);
if nargin == 3 & disable
   set(bjb(3),'enable','off')
end
if nargin >= 2
   set(bjb(basic),'fore','red')
end
while all(cell2mat(get(bjb,'val')) == 0)
   drawnow
end
val = find(cell2mat(get(bjb,'val')))-1;


% ------------------------

function bjb = bjbuttons(kase)

bjb = findobj(gcf,'style','toggle');
if isempty(bjb)
   for b = 3:-1:1
      bjb(b,1) = uicontrol('units','normal','style','toggle', ...
         'pos',[.95-.18*b .02 .16 .08],'fontweight','bold');
   end
end
set(bjb,'fore','black')
switch kase
   case {1,2}
      switch kase
         case 1
            fs = 12; y = .02; dy = .08;
         case 2
            fs = 10; y = .01; dy = .06;
      end
      for b = 1:3
         set(bjb(b),'pos',[.95-.18*b y .16 dy])
      end
      set(bjb,'val',0,'vis','on','enable','on','fontsize',fs)
      set(bjb(1),'string','Close')
      set(bjb(2),'string','Play')
      set(bjb(3),'string','Simulate')
      set(bjb(kase+1),'fore','red')
   case 'detail'
      set(bjb(1:2),'vis','on')
      set(bjb(3),'vis','off')
      for b = 1:3
         set(bjb(b),'pos',[.95-.18*b .02 .16 .08])
      end
      set(bjb,'val',0,'fontsize',12)
   case 'off'
      set(bjb,'vis','off')
   case 'split'
      set(bjb,'val',0,'fontsize',12)
      set(bjb(1),'string','Keep')
      set(bjb(2),'string','Split')
   case 'hit'
      set(bjb,'val',0,'vis','on','fontsize',12)
      set(bjb(1),'string','Stand')
      set(bjb(2),'string','Hit')
      set(bjb(3),'string','Double')
end


% ------------------------

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

% 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);


% ------------------------

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

% 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);


% ------------------------

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

% 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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -