Project Problem #1: 50 points Using MATLAB, build a program that allows the user
ID: 648253 • Letter: P
Question
Project Problem #1: 50 points
Using MATLAB, build a program that allows the user to play a game of blackjack against the computer. Your program should have the following constraints/requirements
Explanation / Answer
function blackjack(N) clf shg set(gcf,'name','Blackjack','menu','none','numbertitle','off', ... 'double','on','userdata',[]) rand('state',sum(100*clock)) if nargin == 0 N = 10000; kase = 1; else if ischar(N) N = str2double(N); end bj(N) kase = 2; end while kase > 0 kase = bjbuttonclick(kase); switch kase case 0, break % Close case 1, bj(1) % Play one hand case 2, bj(N) % Simulate end end close(gcf) % ------------------------ function bj(N) % Blackjack, main program. % Play N hands. % If N == 1, show detail and allow interaction. 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 title(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.4f',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 c = deal % Deal one card persistent deck ncards if isempty(deck) | ncards < 6 % Four decks deck = [1:52 1:52 1:52 1:52]; % Shuffle ncards = length(deck); deck = deck(randperm(ncards)); end c = deck(ncards); ncards = ncards - 1; % ------------------------ function v = valuehard(X) % Evaluate hand X = mod(X-1,13)+1; X = min(X,10); v = sum(X); % ------------------------ function v = value(X) % Evaluate hand X = mod(X-1,13)+1; X = min(X,10); v = sum(X); % Promote soft ace if any(X==1) & vRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.