Blackjack is a very popular casino game, and a personal favorite of mine. It has
ID: 3668843 • Letter: B
Question
Blackjack is a very popular casino game, and a personal favorite of mine. It has many rules and is extremely mathematical. But the basic rules are fairly simple. · Card Values – Numbered cards have the same values as numbered. Jacks, Queens and Kings have a face value of 10, while Aces have a value of 1 or 11. However, for the purpose of this assignment code, consider the Ace to only have a value of 11. · The dealer deals to the players. First a card is dealt from the top of the deck to each player, and then to the dealer. A round of dealing is then repeated, so every player and the dealer have two cards. The sum of the values of the cards is the value of the players ‘hand’. The dealer has two cards, but the players can only see the second card that was dealt to the dealer. All players know the value of this second card, but not the first (this helps them make decisions). · The objective of the game is to defeat the dealer. If the value of the hand is over 21, then the player busts, and automatically loses. If the hands are below 21, then the player’s hand needs to be higher than the dealer’s to win. If the hands tie, then there is a push, and no one wins. · In order, each player can choose to hit or stand. If they hit, they are dealt a new card from the top of the pile. A player can continue hitting as long as they like, unless they bust at which point they have automatically lost their bet. If at any point they stand, whether without hitting or after hitting, that is the final value of the hand they will have. · Once each player is finished hitting/standing, the dealer deals themselves cards from the top of the deck as long as the value of their hand is less than 17. If their hand is 17 or higher, they stand. If they bust, then all players in the game who have not busted win automatically. · If the dealer does not bust, then the dealer’s hand is compared to each of the players. Players with a higher valued hand win their bet and players with a lower value lose. Players who tie push and get their bet back. So if you bet $1, you could end up with either $2 (win), $1 (push), or $0 (lose). For this week’s code, you are going to simulate a game of blackjack.
1. Your code should be a function; the input is the number of players n (must be an integer between 1 and 5) and the output is a matrix called Money showing how each player’s money has progressed through to the end of the game. Money should have n columns. The number of rows depends on the rules below. Each of the n players begin the game with a $100.
2. Create a sub-function that will create a deck of cards comprised of a 1000 shuffled decks. You can use the randperm() function for this. You will need to use a loop or some other strategy to shuffle 1000 decks! Also, now Jacks, Queens and Kings are all worth 10, not 11, 12, and 13, respectively. The variable that stores the deck of cards should be called “deck”, which is the output of your sub-function (there are no inputs). Also remember that there will be more 10’s in your deck than other cards, because 10’s, Jacks, Queen, and Kings each count as 10. An Ace, which is probably a 1 in your deck, has a value of 11 in the game.
3. You can now start playing blackjack using the rules above. Each round, the players bet $1. If the player has $100 at the beginning of a hand, they will have $101 at the end of the hand if they win, $100 if they push, and $99 if they lose. Continue to store the players bankroll in the Money matrix, so that after the first round is played, the second row of the matrix has the current bankroll of each player.
4. Each of the 5 players use the same decision making strategy to decide whether to hit or stand. A baseline strategy is this: a. If the dealer is showing (the second card, which all players see) a 7, 8, 9, 10, or 11, then the player hits until their hand is worth 17 or higher (or they bust). b. If the dealer is showing 2-6, then the player hits until their hand is worth 12 or higher (or they bust). So a player would stand when they have a 12 and the dealer is showing a 3, for example (you are hoping the dealer busts).
5. If a player has $0 in their hand, they cannot play anymore and the dealer skips them when dealing the round (luckily the Casino continues to give them free drinks). Your code must reflect this and only deal to players who are still playing. You should create a vector “player_status” that is nx1 and is true when the player is playing and false when they are not. Their column of Money should just have a value of $0 for all rounds afterwards.
6. You must remember to keep discarding the cards that have already been used. You do not need to store the cards in a discard pile variable, but the deck variable needs to be updated each round. Deck can either become a smaller vector after each round, or you can add NaNs to the end of deck so that the number of real valued cards decreases each round.
7. The game stops when either all players are broke, or when there are less than 30 cards in the deck before a round starts. The remaining players will have played 1000 decks of blackjack and probably need to sleep it off.
8. Finally, plot a graph showing each player’s bankroll on the y-axis versus each hand on the x-axis. Use proper formatting and labeling to make the plot look nice.
I can do some of the simpler coding such as creating the plot or creating a deck compiled with 1000 randomly shuffled decks however, I am struggling with writing a code for steps 3 through 7.
Explanation / Answer
matlabpool
players = 5; % players count
n = 15000; % hands per player.
BJ = zeros(n,players);
%simulating blackjack
for k = 1:players
BJ(:,k) = blackjacksim(n);
end
%plotting graph
plot(cumsum(BJ))
%printing results
r = sum(BJ(n,1:players));
res = '%d x %d hands, total = $ %d'
title(sprintf(res,p,n,r))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.