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

Write a MATLAB script (blackjack.m) that plays the dice game Blackjack. The play

ID: 3689291 • Letter: W

Question

Write a MATLAB script (blackjack.m) that plays the dice game Blackjack. The player begins by drawing 2 cards and the computer draws one card, the suit does not matter. The cards are drawn from an infinite deck and are numbered 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, and A. They occur with equal frequency. The cards J, Q, and K all have a value of 10. The card A has a value of 1 or 11 depending on the situation.

Your script should display the one card dealt to the computer and the two cards dealt to the player, along with the point total of the two player cards. It then should ask the player if they want an additional card. If yes, then show the three cards and the total. If your total is greater than 21, you lose regardless of what happens. If your total is still less 21, ask if the player wants another card. Remember to check the value of an A and make it 1 if it prevents going over 21.

Once the player no longer wants another card, deal a second card to the computer and check its total. If the total is 16 or less, the computer MUST draw another card, if it is 17 or greater it cannot draw another card. If it is 22 or greater the computer loses (unless there is an A in which case the value becomes 1 and the game continues).

Once the computer stops drawing cards, your script should print out a message declaring who wins: the computer, the player, or a tie.

Example:

>> blackjack

Computer: J

Player: 5 7 Total=12

Do you want another card (Y/N)? Y

Player: 5 7 8 Total=20

Do you want another card (Y/N)? N

Computer: J 4 Total=14

Computer: J 4 A Total=15

Computer: J 4 A 7 Total=22

The player wins!

Explanation / Answer

function total = getLogicalTotal(cards)
while hasOver(cards, 21) && hasAce(cards)
for i = 1:length(cards)
if cards(i) == 14
cards(i) = 1;
break;
end
end
end
total = getTotal(cards);
end


function cVal = getCardVal(val)
if val <= 10
cVal = val;
elseif val == 11 || val == 12 || val == 13
cVal = 10;
elseif val == 14
cVal = 11;
end
end

function val = dealACard()
val = randi(13, 1);
if val == 1
val = 14;
end
end

function s = getCardChar(val)
if val > 1 && val <= 10
s = int2str(val);
elseif val == 11
s = 'J';
elseif val == 12
s = 'Q';
elseif val == 13
s = 'K';
elseif val == 14 || val == 1
s = 'A';
end
end

function status = hasAce(cards)
status = false;
for i = 1:length(cards)
if cards(i) == 14
status = true;
end
end
end

function status = hasOver(cards, val)
total = getTotal(cards);
status = total > val;
end

function total = getTotal(cards)
total = 0;
for i = 1:length(cards)
total += getCardVal(cards(i));
end
end


function printCards(cards)
for i = 1:length(cards)
printf('%s ', getCardChar(cards(i)))
end
printf('Total = %d ', getLogicalTotal(cards))
end

gameOver = false;
player = [];
computer = [];
player(1) = dealACard();
player(2) = dealACard();
computer(1) = dealACard();
computer(2) = dealACard();
printf('Computer: %s ', getCardChar(computer(1)))
while true
printf('Player: ')
printCards(player);
if getLogicalTotal(player) > 21
printf('Player lost! ')
gameOver = true;
break;
end
choice = input('Do you want another card (Y/N)? ', 's');
if choice == 'N' || choice == 'n'
break;
else
player(length(player) + 1) = dealACard();
end
end

if gameOver == false
while true
printf('Computer: ')
printCards(computer);
if getLogicalTotal(computer) > 21
printf('Player won!! ');
break;
elseif getLogicalTotal(computer) > getLogicalTotal(player)
printf('Player lost!! ');
break;
elseif getLogicalTotal(computer) < 17
computer(length(computer) + 1) = dealACard();
else
if getLogicalTotal(computer) == getLogicalTotal(player)
printf('Match Tied!! ');
else
printf('Player won!! ');
end
break;
end
end
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