BlackJack Simple Matlab code is needed Input : (maximum value that dealer has to
ID: 638186 • Letter: B
Question
BlackJack Simple Matlab code is needed
Input : (maximum value that dealer has to HIT the next card i.e- 16)
output : final value of the dealer's hand, i.e- 17 or 24
System - 4 deck of cards. Consider that the chance of getting card that is already appeared is decreased.
Just simple code that outputs the final hand of dealer's one play is enough.
No count of any other rules except Ace can be considered 1 or 11
I need 'ready to run' code
Example
function finalhand = BJ(hit)
when you run BJ(16)
output should be : finalhand = 18 %meaning dealer's final hand is 18
or
finalhand = 24 %meaning dealer busted
.
.
and so on
Explanation / Answer
function bj(N) % Blackjack, main program. % Play N hands. % If N == 1, show detail and allow interaction. global PAIR S = get(gcf,'userdata'); n = length(S); bet = 10; detail = N==1; % Set up graphics if detail delete(get(gca,'children')) delete(findobj(gcf,'type','axes')) axes('pos',[0 0 1 1]) axis([-5 5 -5 5]) axis off bjbuttons('detail'); stake = sum(S); if stake >= 0, sig = '+'; else, sig = '-'; end str = sprintf('%6.0f hands, $ %c%d',n,sig,abs(stake)); titl = text(-2.5,4.5,str,'fontsize',20); n0 = n+1; n1 = n0; else bjbuttons('off'); payoffs = [-4:1 1.5 2:4]*bet; % Possible payoffs counts = hist(S,payoffs); n0 = n+1; n1 = ceil((n0)/N)*N; subplot(2,1,2) h = plot(0,0); end S = [S zeros(1,n1-n0+1)]; for n = n0:n1 bet1 = bet; P = deal; % Player's hand D = deal; % Dealer's hand P = [P deal]; D = [D -deal]; % Hide dealer's hole card % Split pairs split = mod(P(1),13)==mod(P(2),13); if split if detail show('Player',P) show('Dealer',D) split = PAIR(value(P(1)),value(D(1))); % 0 = Keep pair % 1 = Split pair split = bjbuttonclick('split',split+1); else split = PAIR(value(P(1)),value(D(1))); end end if split P2 = P(2); if detail, show('Split',P2); end P = [P(1) deal]; bet2 = bet1; end % Play player's hand(s) if detail [P,bet1] = playhand('Player',P,D,bet1); show('Player',P) if split P2 = [P2 deal]; show('Split',P2) [P2,bet2] = playhand('Split',P2,D,bet2); end else [P,bet1] = playhand('',P,D,bet1); if split P2 = [P2 deal]; [P2,bet2] = playhand('',P2,D,bet2); end end % Play dealer's hand D(2) = -D(2); % Reveal dealer's hole card while value(D) = 0, sig = '+'; else, sig = '-'; end str = sprintf('%6.0f hands, $ %c%d',n,sig,abs(stake)); set(titl,'string',str) end chunk = min(2000,N); if ~detail & mod(n,chunk) == 0 Schunk = S(n-chunk+1:n); subplot(2,1,2) ydata = get(h,'ydata'); ydata = ydata(end) + cumsum(Schunk); ylim = get(gca,'ylim'); if max(ydata) > ylim(1) | min(ydata) = 0, sig = '+'; else, sig = '-'; end str = sprintf('%c%d',sig,abs(stake)); if abs(stake) < 1000, str = [' ' str]; end if abs(stake) < 100, str = [' ' str]; end if abs(stake) < 10, str = [' ' str]; end text(-20,.55,sprintf('%6.0f hands, $ %s',n,str)) set(gca,'xtick',payoffs); for k = 1:length(payoffs) if payoffs(k)==15, y = -.12; else, y = -.08; end text(payoffs(k)-6.5,y,sprintf('%9.3f',100*p(k))); end % Mean and confidence interval, relative to unit bet r = payoffs/bet; mu = p*r'; crit = 1.96; % norminv(.975) rho = crit*sqrt((p*(r.^2)'-mu^2)/n); pm = char(177); text(20,.3,sprintf('%6.4f %c %6.4f',mu,pm,rho)); drawnow end end set(gcf,'userdata',S); % ------------------------ function shuffle % Shuffle the deck global DECK DECK = mod(randperm(4*52)-1,52)+1; % ------------------------ function d = deal % Deal one card global DECK if length(DECK)Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.