Ask MATLAB Expert

Blackjack

 

 

Setup

 

In this homework, we will be playing the classic casino game of Blackjack. We will accomplish this by creating a single function file with several local functions. The function file should contain the following functions:

 

1. Blackjack function (main function)

 

2. Initdeck function

 

3. Shuffle function

 

4. Dealcard function

 

5. Calcscore function

 

6. Printcard function

 

 

Each function should have a function definition and a help line. The main function should also have the header information immediately after the help line.

 

To play the game, we will make use of a card structure. The card structure is as follows:

 

 

 

  • Card Structure

     

    • Attribute: Suit

     

     

 

 

- Type: String value

 

- Description: Holds the card's suit

 

o  Attribute: Value

 

- Type: String value

 

- Description: Holds the card's value (Ace, 2, 3...)

 

o Attribute: Number

 

- Type: Numeric value

 

- Description: Holds the card's point value (2, 3, 4...) Blackjack is played like this:

 

1. The player and dealer each get two cards.

 

a. If the player and dealer's cards add up to 21, then no one wins. This is called a "Push"

 

b. If the player's cards add up to 21, and the dealer's do not, then the player wins. This is "Blackjack"

 

c. If the players' cards add up to less than 21 and the dealer's add up to 21, the player loses. This is also "Blackjack"

 

d. If neither the player's cards, nor the dealer's cards add up to 21, then the game moves on to step 2

2. The player has the option to "Hit" or "Stand". "Hit" means that they will be dealt another card. "Stand" means that they will keep the cards they have and it will be the dealer's turn. If they choose to stand, move on to step 4

 

3. If the user chooses to "Hit" they will be dealt another card. Their score will be calculated.

 

a. If their score is 21 or under, they will have the option to "Hit" or "Stand"

 

b. If their score is above 21, and they have an Ace in their hand, their score is decreased by

 

10. This is the duality of the Ace card. They will again have the option to "Hit" or "Stand"

 

c. If their score is above 21, and they don't have any Aces in their hand, or their score was already decreased and they are still above 21, then they "Bust" and lose the game.

 

d. If their score is under 21 and they have 5 cards in their hand, they win. This is called a "Charlie"

 

4. If the user chooses to "Stand", then the dealer begins their turn.

 

5. The dealer's score is calculated

 

a. If the dealer's score is less than 18, the dealer MUST "Hit"

 

b. If the dealer's score is 18 or above, the dealer will "Stand"

 

6. The dealer's score is recalculated

 

a. If the dealer's score is 21 or under, move on to step 7

 

b. If the dealer's score is over 21, the dealer "Busts" and the player wins

 

c. If the dealer's score is equal to the player's score, then no one wins. This is also a "Push"

 

7. If the dealer's score and the player's score are 21 or under, but are not the same, the person with the higher score wins the game.

 

Part 1: initdeck() Function

 

The initdeck() function has only one input and only one output. The input of this function is a single positive integer value. This value represents the number of decks that the function will be creating. The output of this function is a structure array of cards. The number of cards in the structure array is dependent upon the number of decks to create. The function should operate like this:

 

1. Check the input:

 

a. It should be a single, positive, integer value. If it's not, return an error to the user.

 

2. Create a single card structure with the correct fields as described in the setup

 

3. Replicate the card structure 52 times to form a single deck

 

4. Fill each card with the correct values for suit

 

5. Fill each card with the correct values for value

 

6. Fill each card with the correct values for number:

 

a. Cards 2 - 10 have values 2 - 10 accordingly

 

b. Cards Jack - King have a values of 10

 

c. Aces have a value of 11

 

7. Replicate the card structure array (the deck) the correct number of times to create multiple decks

 

8. Return the card structure array as output

Part 2: shuffle() Function

 

The shuffle() function has only one input and only one output. The input of this function is a card structure array. The output of this function is a card structure array. The input is the deck(s) of cards you want to shuffle. The output is the shuffled deck(s). Your code should perform the following:

 

1. Check the input:

 

a. It should be a structure array with at least 52 elements (cards). If it's not, return an error to the user.

 

2. Create two random integers with a maximum value equal to the number of elements of the card structure array

 

3. Swap the cards of the structure array at these two random integer indexes (swap #45 and #27 for example)

 

4. Repeat steps 2 and 3 at least 3000 times

 

5. Return the shuffled structure array as output

 

Part 3: dealcard() Function

 

The dealcard() function has only one input and two outputs. The input of this function is the card structure array (deck) from which you are dealing the card. The outputs from this function are:

 

1. The card that was dealt

 

2. The card structure array with the card that was dealt removed

 

The function should operate like this:

 

1. Check the input:

 

a. It should be a structure array

 

2. Output the first card as the first output

 

3. Delete the first card from the deck

 

4. Output the new deck as the second output

 

Part 4: calcscore() Function

 

The calcscore() function has only one input and only one output. The input of this function is a card structure array (the cards that either the dealer or the player has). The output of this function is a single integer value that represents the total value of the structure array (either the player's score, or the dealer's score). The function should operate like this:

 

1. Check the input:

 

a. It should be a structure array

 

2. Calculate the raw score (this is the sum of the number field of each card)

 

3. Calculate the number of Aces in the hand

 

4. If the score is above 21 and the number of Aces is at least 1, decrease the raw score by 10 and the number of Aces by 1 until either the raw score is 21 or below or the number of Aces is less than 1.

 

5. Output the score as the output

 

Part 5: printcard() Function

 

The printcard() function has only one input and no outputs. The input of this function is a single card structure. The function should operate like this:

 

1. Check the input:

 

a. It should be a single card structure

 

2. Using an fprintf() statement, print the card's value and suit in the following way:

 

10 of Diamonds Jack of Clubs

 

Part 6: blackjack() Function

 

The blackjack() function is the main function. It has only one input and no output. The input of the function is a single integer value which represents the number of decks that you will be playing with. The function should operate like this:

 

1. Check the input;

 

a. It should be a single, positive, integer value. If it is not, produce an error

 

2. Create the deck(s)

 

3. Shuffle the deck(s)

 

4. Deal two cards to the player's hand

 

a. Keep track of the number of cards for the player

 

5. Deal two cards to the dealer's hand

 

a. Keep track of the number of cards for the dealer

 

6. Print the cards for each person (player and dealer)

 

7. Print the scores for each person (player and dealer)

 

8. Determine if anyone has reached "Blackjack"

 

a. If so, announce the winner and end the function

 

9. Determine if there was a "Push"

 

a. If so, announce the push and end the function

 

10. Ask the player to hit or stand

 

11. If the player hits, deal the player a card

 

a. Update the number of cards the player has

 

b. Calculate the player's new score

 

c. If the player's score is above 21, they lose. Announce the loser and end the function

 

d. If the player's score is 21 or below, and they have 5 cards, they win. Announce the winner and end the function

 

e. If the player's score is 21 or below and they have less than 5 cards, go back to Step 10

 

12. If the player stands, move on to the dealer

 

13. If the dealer's score is below 18, hit

 

a. Update the number of cards the dealer has

 

b. Calculate the dealer's new score

 

c. If the dealer's score is above 21, they lose. Announce the loser and end the function

 

d. If the dealer's score is 21 or below, and they have 5 cards, they win. Announce the winner and end the function

 

e. If the dealer's score is 21 or below and they have less than 5 cards, go back to Step 13.

 

14. If the dealer's score is 18 or above, stand

 

15. Check to see who won

 

16. Announce the winner, and then end the function.

 

MATLAB, Engineering

  • Category:- MATLAB
  • Reference No.:- M91732038

Have any Question?


Related Questions in MATLAB

Assignment - matlab programmingusing appropriate matlab

Assignment - MatLab Programming Using appropriate MatLab syntax, write the code required to analyse and display the data as per the problem description. The order of the MatLab Program should be as follows: Variables and ...

Assignment details -need to write a code for connecting

Assignment Details - Need to write a code for connecting segments (Lines) a special case of TSP. The problem is to connect lines in 2d/ 3d space with path obstructions. Can you help me write the code for this? Hope you m ...

Assignment -we have daily gridded rainfall data of 40 years

Assignment - We have daily gridded rainfall data of 40 years and structure of the dataset is like below; Lat = [6.5:0.25:38.5]; Lon = [66.5:0.25:100]; Rainfall (135x129x365x40) (Lon, Lat, days, years). Now, we looking fo ...

Question a safe prime is a prime number that can be written

Question : A safe prime is a prime number that can be written in the form 2p + 1 where p is also a prime number. Write a MATLAB script file that finds and displays all safe primes between 1 and 1000.

Question - verify the attached paper with matlab and get

Question - Verify the attached paper with matlab and get all the results in the paper and explain step by step the matlab code. Paper - Improving Massive MIMO Belief Propagation Detector with Deep Neural Network. Attachm ...

Assignment -data is given on which want to do computational

Assignment - Data is given on which want to do computational production planning using Metaheuristic MATLAB Programming: 1) Ant Colony Algorithm on both Partial and Total Flexible Problem. 2) Bee Algorithm on both Partia ...

What comparison of means test was used to answer the

What comparison of means test was used to answer the question

Question 1 manipulate spectral imagehyperspectral images

Question 1. Manipulate spectral image Hyperspectral images can be seen as a generalisation of normal colour images such as RGB images. In a normal RGB colour image, there are 3 channels, i.e. channels for red colour, gre ...

Assignment -matlab codes and simulated model in

Assignment - Matlab codes and simulated model in simulink/matlab and truetime. 1. Matlab codes and simulink model for pid controller optimization using particle swarm optimization (PSO) my plant is integer order 1000/(s^ ...

Assignment matlab programmingusing appropriate matlab

Assignment: MatLab Programming Using appropriate MatLab syntax, write the code required to analyse and display the data as per the problem description. The order of the MatLab Program should be as follows: Variables and ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As