Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Engr 3: Introduction to Programming

Fall 2022 Final Project

Due Wednesday, December 7, 11:55PM (California time)


Blackjack is a popular card game in which a player attempts to beat the dealer by getting cards whose values add up as close to 21 as possible, without going over 21.

In blackjack, every card in a 52-card deck has a value, as given in the table below. Here, A stands for Ace, J stands for Jack, Q stands for Queen, and K stands for King. There are 4 diferent suits - clubs (兩), spades (♠), hearts (♥), and diamonds (♦) - but in blackjack, the suits don’t matter. In the table, the columns labeled # give a numbering scheme that we’ll use in order to identify the cards in our Matlab programs.  Note that the value of an Ace can be 1 or 11; the player gets to choose.

 

The set of cards that the player is holding is called a“hand”, and the sum of the values of the cards in the hand is called the player’s“score”.  At the beginning of the game, both the player and the dealer are dealt 2 cards. If desired, the player can choose to receive a new card (to do this, they ask the dealer to“hit”), and they add its value to the other cards already in their hand. The player can continue to“hit”to receive new cards one at a time until either their score exceeds 21 (called“bust”) and they lose the game, or they are happy with their hand and choose to stop receiving new cards (to do this, they tell the dealer“stand”).

The dealer then plays. If the dealer’s score is 17 or more, they must stand, and they take no more cards. If their score is 16 or under, they must take a new card. The dealer must continue to take new cards one at a time until their score is 17 or more, at which point the dealer must stand. However, if their score exceeds 21, the dealers“busts”, and loses. If the dealer has an Ace, and counting it as 11 would bring the sum to 17 or more (but not over 21), the dealer must count the Ace as 11 and stand.

If, after the dealer has played, the player has a higher score than the dealer without busting, the player wins. If the dealer has a higher score than the player’s score without busting, the dealer wins.  If the scores are identical (a tie), no one wins; this is called a “push”.

In this inal project, you’ll write Matlab code inspired by blackjack. In particular, you will investigate the probability that the player will win (or at least ties the dealer) if they just keep their irst two cards (that is, they do not ask for additional cards).  I’m not claiming that this is a good strategy, although perhaps it’s not so bad if their irst two cards give a good score?

Note that each game will use a new shued deck of cards.  The following command will“shue”the deck:

shuffled = randperm(52);

This will create a vector called shuffled which randomly orders the numbers 1 through 52. The irst element of shuffled will be the irst card in the player’s hand, the second element of shuffled will be the second card in the player’s hand, the third element of shuffled will be the irst card in the dealer’s hand, the fourth element of shuffled will be the second card in the dealer’s hand, and if the dealer needs to draw another card it’ll be the ifth element of shuffled, etc. In your programs, it is OK to use built-in Matlab functions such as mod, max, min, length, ...

1.  (10 points) Write a Matlab function called card_value .m which takes as input the number of the card (in the sense of the columns labeled # in the table), and returns the value of the card according to the table. If the number of the card corresponds to an Ace, the function should return the number 1.

So, for example,

o the command card_value(33) should return the value 3,

o the command card_value(42) should return the value 10, and

o the command card_value(11) should return the value 1.

2.  (10 points) Write a Matlab function called player .m which takes as input the vector shuffled, and returns as output the player’s score as calculated from the irst two cards in the deck. Aces should be handled as follows:

o If the player draws just one Ace, it should be counted as 11.  Note that since the player is only drawing two cards, their score in this case will always be less than or equal to 21; that is, they will not bust.

o If the player draws two Aces, one should be counted as 11, and the other should be counted as 1. Note that if they were both counted as 11, the player would bust because 11 + 11 = 22 >  21.

Your function player can make calls to your function card_value.

3.  (10 points) Determining the dealer’s score is more dicult, because they will keep drawing cards until their score is at least a 17, or they bust.  With this in mind, and noting that Aces can count as either 1 or 11, it will be useful to consider a vector whose elements are the possible scores that the dealer’s hand can take.  For example, suppose that a dealer’s hand consists of card #’s 6, 20, and 21, with corresponding values 6, 10, and (1 or 11). The possible scores are 6+10+1 = 17, or 6+10+11 = 27, and the vector of possible scores will be [17, 27].

Write a function called  check_possibilities .m which takes as input the vector possibilities, and returns as output either

o 999 if all of the elements of possibilities are greater than 21, (this corresponds to the dealer busting) or

o the largest element of possibilities which is greater than or equal to 17, and less than or equal to 21, if such an element exists, (this will be the dealers inal score), or

o -999 otherwise (this corresponds to situations in which the dealer will take another card)

For example,

o the command check_possibilities([3 13]) should return the value -999,

o the command check_possibilities([15 25]) should return the value -999,

o the command check{possibilities([23 33]) should return the value 999, and

o the command check_possibilities([9 12 19 22]) should return the value 19.

Please note that the inal example doesn’t correspond to actual possibilities that could arise for a hand, but it is useful for testing your code.

4.   (25 points) Write a function called dealer .m which determines the score for the dealer, following the rules described above.  The input to this function should be the vector shuffled, and the output should be the inal score for the dealer’s hand, or 999 if the dealer busts.

For example, suppose that the dealer’s irst card is #3, which has value 3, and their second card is #25, which has the value 5. At this point, their score is 8 = 5 + 3. Because this is less than 17, the dealer must draw another card.  Suppose they draw #31, which either has the value 1 or 11. The possible scores at this point are either 9 = 8 + 1 or 19 = 8 + 11. Because 17  19  21, this will be the dealer’s inal score. The function will return the value 19.

As another example, suppose that the dealer’s irst card is #27, which has value 7, and their second card is #15, which has the value 5.  At this point, their score is 12 = 7 + 5. Because this is less than 17, the dealer must draw another card. Suppose this is #32, which has value 2. Their score is now 14 = 12 + 2. Because this is less than 17, the dealer must draw another card. Suppose this is #48, which has value 10. Their score is now 14+10 = 24, which is greater than 21.  The dealer has busted, so the function will return the value 999.

Note that for your function to work properly, you will need to keep track of all possible scores that the dealer’s hand can have. If the dealer draws an Ace, it can have the value 1 or 11, but it must be counted as an 11 if this brings their score to 17, 18, 19, 20, or 21. Recall that the dealer’s irst card is the third element of shuffled, their second card is the fourth element of shuffled, etc.  Your function dealer can make calls to your functions card_value and check_possibilities.

5. (15 points) Write a program called main .m which determines and plots the probability that the player wins (or at least ties the dealer), as a function of their score from their two cards. Your program should ind these probabilities using a Monte Carlo simulation with 100,000 trials, where each trial is one game. (Note that this number of trials is sucient to have a large number of games for which the player’s score is a 4, a large number of games for which the player’s score is a 5, etc. Here the minimum score for the player is 4, which would correspond to drawing two 2’s, and the maximum score for the player is 21.) The probabilities of winning should be shown in blue, and the probabilities of winning or pushing should be shown on the same plot in red. The axes, labels, and legend should be as shown below, where I’ve just shown data points for a player score of 18 and 19; your plot should include these and ill in the rest.  Here, for example, the blue point for the player score of 18 shows the probability that the player will win with that score, and the red point for the player score of 18 shows the probability that the player will win or push with that score.

1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0

 

     Probability player wins

     Probability player wins or pushes

 

4             6            8            10           12           14           16           18          20

Player Score

Please turn in the following Matlab programs:

#1: card_value .m

#2: player .m

#3: check_possibilities .m

#4: dealer .m

#5: main .m

Academic Honesty Agreement

This project is open book, open notes.  However, all work submitted must be your own.  By submitting these programs, you are asserting that all work on this project is yours alone, and that you did not and will not provide any information to or discuss this project with anyone else doing the project, nor did you receive help from anyone in the class or anyone not in the class, including people on the internet.  You are not allowed to post any information about this project on the internet at any time, either before or after the due date. Discussing any aspect of this project with anyone else before the due date constitutes a violation of the academic honesty agreement. A violation can lead to getting an F in the course.  Please note that you may ask questions about this project to the Professor and the TAs, but we may answer in a limited way because we want to assess your coding ability, not our own.