Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function, playDealer.m, that takes as an input argument a vector contain

ID: 3833510 • Letter: W

Question

Write a function, playDealer.m, that takes as an input argument a vector containing a valid Blackjack hand for the dealer, and returns a vector of the completed hand for the dealer. Use the addCard.m function to add a card to the hand, and your dealer.m function to determine what should be done with the dealer’s hand.

In addition to returning the hand vector, your function should display either, “Dealer has blackjack”, “Dealer busts”, or “Dealer is standing with x” once the dealer’s hand is completed.

Have your function take into account the dual nature of the ace, and before a “bust” is declared check to see if the hand contains any aces and reduce them from 11 to 1.

Develop a script playBlackjack.m that deals two cards to the player and to the dealer, and then gets user input whether or not to “hit” or “stand”. On hit, make a call to addCard.m to add an additional card, and continue to ask until the user replies “stand”. After the player stands, call your playDealer.m function, and then determine if the player won or lost and display the appropriate message.

addCard.m

function [hand] = addCard (hand)

%This function takes as input a vector of values representing a Blackjack hand
%and appends one card value (between 1 and 10) to the hand.
%The functions returns the vector containing the new hand.

newCard = randi([1 13]); %pick a random integer between 1 and 13
if newCard > 10 %a face card: value is 10.
newCard = 10;
elseif newCard == 1 %an ace: give it a value of 11.
newCard = 11;
end
n=max(size(hand)); %determine the length of the vector of the current hand
hand(n+1) = newCard;
end

dealer.m

function [ d ] = dealer(hand)
%This function takes a vector of card values in the dealer's hand and
%returns a numeric value depencding on what the dealer has to do
%hand = [3 7]
s = sum(hand);
if s>=17 && s<21
d = s;%Stand
disp('Dealer standing')
elseif s<17 && s>0
d = 1;%Hit
elseif s==21
d = 2;%Blackjack
disp('Dealer has Blackjack')
elseif s>21
d = 3;%Bust
disp('Dealer busts')
end

Explanation / Answer

addCard.m

function [hand] = addCard (hand)

%This function takes as input a vector of values representing a Blackjack hand
%and appends one card value (between 1 and 10) to the hand.
%The functions returns the vector containing the new hand.

newCard = randi([1 13]); %pick a random integer between 1 and 13
if newCard > 10 %a face card: value is 10.
newCard = 10;
elseif newCard == 1 %an ace: give it a value of 11.
newCard = 11;
end
n=max(size(hand)); %determine the length of the vector of the current hand
hand(n+1) = newCard;
end

dealer.m

function [ d ] = dealer(hand)
%This function takes a vector of card values in the dealer's hand and
%returns a numeric value depencding on what the dealer has to do
%hand = [3 7]
s = sum(hand);
if s>=17 && s<21
d = s;%Stand
disp('Dealer standing')
elseif s<17 && s>0
d = 1;%Hit
elseif s==21
d = 2;%Blackjack
disp('Dealer has Blackjack')
elseif s>21
d = 3;%Bust
disp('Dealer busts')
end

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote