Write a MATLAB script (blackjack. m) that plays the dice game Blackjack. The pla
ID: 3688771 • 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. Advance modification (optional): Card Counting. Rather than play 1 hand, play until 150 cards have been dealt. Keep a running "count" of what has been played. Cards 2-6 are worth 1 point, while 10, J, Q, K, A are worth -1 point. At the end of each hand, ask the player what the deck count is and report how accurate they are, then start the next hand. In general, the higher the total, the more favorable the deck is to the player.Explanation / Answer
function strat = hard(p,d)
% Hands without aces.
% hard(player,dealer)
% 0 = stand
% 1 = hit
% 2 = double down
% Dealer shows:
% 2 3 4 5 6 7 8 9 T A
HARD = [ ...
1 x x x x x x x x x x
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];
strat = HARD(p,d);
end
function strat = soft(p,d)
% Hands with aces.
% soft(player-11,dealer)
% 0 = stand
% 1 = hit
% 2 = double down
% Dealer shows:
% 2 3 4 5 6 7 8 9 T A
SOFT = [ ...
1 1 1 1 1 1 1 1 1 1 1
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];
strat = SOFT(p,d);
end
function strat = pair(p,d)
% Strategy for splitting pairs.
% pair(paired,dealer)
% 0 = keep pair
% 1 = split pair
% Dealer shows:
% 2 3 4 5 6 7 8 9 T A
PAIR = [ ...
1 x x x x x x x x x x
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];
strat = PAIR(p,d);
end
x = NaN; % Not possible
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.