Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

matlab code Script 4 XXXXXXXXXXxxxxxxxxxxxxxxxxxxxxxx 5 %to make an anonymous function for score_hand) 6 complete this line: 8 score_hand - Scode to calculate the score

matlab code

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Script 4 XXXXXXXXXXxxxxxxxxxxxxxxxxxxxxxx 5 %to make an anonymous function for score_hand) 6 complete this line: 8 score_hand - Scode to calculate the score 11 xxxxxxx x xcocoooooooooooooooooox 13 %Testing the functions 14 testScore - score_hand(deal_card(deal_hand()); 15 testLength1-length(deal_hand(); 16 testLength2=length(deal_card(deal_hand()); 17 disp('Blackjack:') 18 display_hand([1,13]); 26 % 22 %The main things that your program has to do are: 23 % 24 % Deal the cards 25 % Display the hand Calculate the score and store in the variable player_score or 27 % dealer_score 28 % display the score 29 % 30 %The other logic (when to quit the game, etc.) is already built into the 31 %template 33 ******* 34 % ENTER YOUR CODE BETWEEN THE 80000CXOOXXOOOOX LINES. 35 % 36 RRR 37 dealer_score = 0; >initialize dealer score so grader doesn't fail if player busts XXXXXXXXX 40 disp('Your hand:') 41 X0000OOOXXOOXXOO000X00XXXXXX000XXXX 0000000000XXOO000000000xx 42 % Deal the player's initial cards, score the hand, display the hand, display the score 40 disp("Your hand:) 42 % Deal the player's initial cards, score the hand, display the hand, display the score player_score = 0; Replace the with a function call fprintf('Score = $i ', player_score); %20XXXXOO000xxxxxxxxxxX0000000000000CXXX000000xxxxocooox hit = 1; flag that says deal another card 56 %X00000000000000xxxxxx00000000000000000000000000000000000000 %CHANGE THE FOLLOWING LINE AFTER YOU HAVE COMPLETED THE REST OF THE SCRIPT 58 %CHANGE THE 1 TO A 60 busted = 1; %flag that says player is not busted 61 %XXXXXXXXX00000XXXXXXXXXXXXXXXX0000000CXXXX000000000xxx while busted == 0 && hit == 1 && player_score 18, do not hit if his score is between 15 and 18, generate a random number to decide if he hits. hit - player_scoreplayer_score; if hit == 1 %0000000XXXXXXXXXXcxcxxxxxxX0000X % Deal one more card and calculate the score and display the hand % and the score display the score fprintf('Score = sin, player_score); 00000000000000000000000000000000 if player_score > 21 99000000000000000000000000000000000000000OOOOOOOOOOOOOOOOOOOX % The player is busted and stops playing. Display a losing % message busted - 1; 0000000000000000000 00000000000000000000000000000000000000X end end 100 end 104 if busted == XXXXXXXXXXXX00000000000000000000000000000000000000000 % Deal the dealer a separate hand, score it, then show it disp('Dealer's hand:) 105 %XXXXXXXXX XXXX00000000000000000000000000000000000000 while (dealer_score 21 if dealer_score > 21 xxxxxxxxxxxxxxxxx000000000000X % Show that the player wins 90XXXXXXX XXOOOOOOOOOOX else %30000XX000000000000000000000000000X Show that the dealer wins %XC000XXXXXXXXXXXXX0000000000000 end 145 end 146 151 150 Complete these local functions Add input & output arguments as needed 153 154 function () = deal_hand() 155 156 157 end 158 159 function () - deal_card() 161 163 end 166 164 165 function () - display_hand() cards = 'A23456789TJQK'; 167 168 169 end 171 *XXXXXXXXXXXXXXXXXXXXXXXX Since the problem is fairly complex, it may be better to complete & debug the program offline, then submit in Grader. In this exercise you will complete a simplified program to play a hand of Blackjack. The goal is to complete a modular program using local and anonymous functions. Although there are several functions that must be written, they are all quite short and simple, just 1 to 4 lines. The most important learning objective is to understand how function definitions and function calls fit together PART 1: At the bottom of the template, complete the following local functions: deal_hand() function This function will generate a hand of two cards by generating two random numbers between 1 and 13. Input argument: None Output argument: A row vector of 2 integers between 1 and 13. The body of the function can be implemented in a single line using the Matlab built-in function randi(). (NOTE: we are making a simplifying assumption that we have an infinite Oeck carcis en we ann't have to This function will generate a hand of two cards by generating two random numbers between 1 and 13. Input argument: None Output argument: A row vector of 2 integers between 1 and 13. The body of the function can be implemented in a single line using the Matlab built-in function randi(). (NOTE: We are making a simplifying assumption that we have an infinite deck of cards, so we don't have to keep track of which cards have already been dealt.) To deal two cards, each having a value between 1 and 13, the form of the function is: hand = randi (13, 1, 2); display_hand_function This function will display a blackjack hand to the screen input argument: a row vector containing the hand as described in 1. output argumen: None The contents of the display_hand() function will be as follows: The contents of the display_hand() function will be as follows: Define a row vector with characters representing the card values, like so: cards = I'A','2','3', '4', '5', '6', '7', '8', '9','T','J',' (this can also be written more compactly as cards='A23456789TJQK') NOTE: Each card is represented by a single character; thus 'T' is used for 10. The character corresponding to a card value is accessed by indexing. For example if the card is a ten, the corresponding character using can be displayed by: fprintf("%c', cards (10) The entire hand can be displayed with a single fprintf() call as follows: fprintf('%c',cards (hand)) Once the hand has been displayed, print a new line using fprintf('%c',cards (hand)) Once the hand has been displayed, print a new line using fprintf(' ') score_hand function This function calculates the score of a blackjack hand. Input argument: a hand (row vector of numbers between 1 and 13) Output argument: the score corresponding to that hand of cards To keep it simple, we will only allow the Ace to have a value of 1. (Allowing it to have either a value of 1 or 11 depending on context is a challenge problem that you can try after you have the rest of the game working.) With this restriction, the value of the card is equal to the number representing the card, with the exception of face cards (11, 12, and 13), which are worth 10. You can use the min() function and the sum() function to sum the values of the cards while limiting each card to an upper value of 10. Since the simplified score_hand() function only requires one line of code, it can be implemented via an in-line (anonymous) function. Try implementing it that way. It can be defined anywhere in the script before it is called for the first time. deal card function This function adds a card to an existing hand. Generate a single card using randi(), and use concatenation to add it to the existing hand. For example, new_hand = [existing_hand new_card); Input argument: a hand (row vector of numbers between 1 and 13) Output argument: a hand consisting of the input hand with one additional card added to it. PART 2: Add calls to your user-defined functions in the appropriate places of the template script. So that you don't have to think much about the logic of the game for this exercise, we have provided the looping and branching code. Script 4 XXXXXXXXXXxxxxxxxxxxxxxxxxxxxxxx 5 %to make an anonymous function for score_hand) 6 complete this line: 8 score_hand - Scode to calculate the score 11 xxxxxxx x xcocoooooooooooooooooox 13 %Testing the functions 14 testScore - score_hand(deal_card(deal_hand()); 15 testLength1-length(deal_hand(); 16 testLength2=length(deal_card(deal_hand()); 17 disp('Blackjack:') 18 display_hand([1,13]); 26 % 22 %The main things that your program has to do are: 23 % 24 % Deal the cards 25 % Display the hand Calculate the score and store in the variable player_score or 27 % dealer_score 28 % display the score 29 % 30 %The other logic (when to quit the game, etc.) is already built into the 31 %template 33 ******* 34 % ENTER YOUR CODE BETWEEN THE 80000CXOOXXOOOOX LINES. 35 % 36 RRR 37 dealer_score = 0; >initialize dealer score so grader doesn't fail if player busts XXXXXXXXX 40 disp('Your hand:') 41 X0000OOOXXOOXXOO000X00XXXXXX000XXXX 0000000000XXOO000000000xx 42 % Deal the player's initial cards, score the hand, display the hand, display the score 40 disp("Your hand:) 42 % Deal the player's initial cards, score the hand, display the hand, display the score player_score = 0; Replace the with a function call fprintf('Score = $i ', player_score); %20XXXXOO000xxxxxxxxxxX0000000000000CXXX000000xxxxocooox hit = 1; flag that says deal another card 56 %X00000000000000xxxxxx00000000000000000000000000000000000000 %CHANGE THE FOLLOWING LINE AFTER YOU HAVE COMPLETED THE REST OF THE SCRIPT 58 %CHANGE THE 1 TO A 60 busted = 1; %flag that says player is not busted 61 %XXXXXXXXX00000XXXXXXXXXXXXXXXX0000000CXXXX000000000xxx while busted == 0 && hit == 1 && player_score 18, do not hit if his score is between 15 and 18, generate a random number to decide if he hits. hit - player_scoreplayer_score; if hit == 1 %0000000XXXXXXXXXXcxcxxxxxxX0000X % Deal one more card and calculate the score and display the hand % and the score display the score fprintf('Score = sin, player_score); 00000000000000000000000000000000 if player_score > 21 99000000000000000000000000000000000000000OOOOOOOOOOOOOOOOOOOX % The player is busted and stops playing. Display a losing % message busted - 1; 0000000000000000000 00000000000000000000000000000000000000X end end 100 end 104 if busted == XXXXXXXXXXXX00000000000000000000000000000000000000000 % Deal the dealer a separate hand, score it, then show it disp('Dealer's hand:) 105 %XXXXXXXXX XXXX00000000000000000000000000000000000000 while (dealer_score 21 if dealer_score > 21 xxxxxxxxxxxxxxxxx000000000000X % Show that the player wins 90XXXXXXX XXOOOOOOOOOOX else %30000XX000000000000000000000000000X Show that the dealer wins %XC000XXXXXXXXXXXXX0000000000000 end 145 end 146 151 150 Complete these local functions Add input & output arguments as needed 153 154 function () = deal_hand() 155 156 157 end 158 159 function () - deal_card() 161 163 end 166 164 165 function () - display_hand() cards = 'A23456789TJQK'; 167 168 169 end 171 *XXXXXXXXXXXXXXXXXXXXXXXX Since the problem is fairly complex, it may be better to complete & debug the program offline, then submit in Grader. In this exercise you will complete a simplified program to play a hand of Blackjack. The goal is to complete a modular program using local and anonymous functions. Although there are several functions that must be written, they are all quite short and simple, just 1 to 4 lines. The most important learning objective is to understand how function definitions and function calls fit together PART 1: At the bottom of the template, complete the following local functions: deal_hand() function This function will generate a hand of two cards by generating two random numbers between 1 and 13. Input argument: None Output argument: A row vector of 2 integers between 1 and 13. The body of the function can be implemented in a single line using the Matlab built-in function randi(). (NOTE: we are making a simplifying assumption that we have an infinite Oeck carcis en we ann't have to This function will generate a hand of two cards by generating two random numbers between 1 and 13. Input argument: None Output argument: A row vector of 2 integers between 1 and 13. The body of the function can be implemented in a single line using the Matlab built-in function randi(). (NOTE: We are making a simplifying assumption that we have an infinite deck of cards, so we don't have to keep track of which cards have already been dealt.) To deal two cards, each having a value between 1 and 13, the form of the function is: hand = randi (13, 1, 2); display_hand_function This function will display a blackjack hand to the screen input argument: a row vector containing the hand as described in 1. output argumen: None The contents of the display_hand() function will be as follows: The contents of the display_hand() function will be as follows: Define a row vector with characters representing the card values, like so: cards = I'A','2','3', '4', '5', '6', '7', '8', '9','T','J',' (this can also be written more compactly as cards='A23456789TJQK') NOTE: Each card is represented by a single character; thus 'T' is used for 10. The character corresponding to a card value is accessed by indexing. For example if the card is a ten, the corresponding character using can be displayed by: fprintf("%c', cards (10) The entire hand can be displayed with a single fprintf() call as follows: fprintf('%c',cards (hand)) Once the hand has been displayed, print a new line using fprintf('%c',cards (hand)) Once the hand has been displayed, print a new line using fprintf(' ') score_hand function This function calculates the score of a blackjack hand. Input argument: a hand (row vector of numbers between 1 and 13) Output argument: the score corresponding to that hand of cards To keep it simple, we will only allow the Ace to have a value of 1. (Allowing it to have either a value of 1 or 11 depending on context is a challenge problem that you can try after you have the rest of the game working.) With this restriction, the value of the card is equal to the number representing the card, with the exception of face cards (11, 12, and 13), which are worth 10. You can use the min() function and the sum() function to sum the values of the cards while limiting each card to an upper value of 10. Since the simplified score_hand() function only requires one line of code, it can be implemented via an in-line (anonymous) function. Try implementing it that way. It can be defined anywhere in the script before it is called for the first time. deal card function This function adds a card to an existing hand. Generate a single card using randi(), and use concatenation to add it to the existing hand. For example, new_hand = [existing_hand new_card); Input argument: a hand (row vector of numbers between 1 and 13) Output argument: a hand consisting of the input hand with one additional card added to it. PART 2: Add calls to your user-defined functions in the appropriate places of the template script. So that you don't have to think much about the logic of the game for this exercise, we have provided the looping and branching code

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions