{"id":191860,"date":"2025-02-16T09:16:57","date_gmt":"2025-02-16T09:16:57","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=191860"},"modified":"2025-02-16T09:17:00","modified_gmt":"2025-02-16T09:17:00","slug":"refactor-the-assignment-5-code-into-new-functions","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/02\/16\/refactor-the-assignment-5-code-into-new-functions\/","title":{"rendered":"Refactor the Assignment 5 Code Into New Functions"},"content":{"rendered":"\n<p>Part 1: Refactor the Assignment 5 Code Into New Functions<br>In Week 5, we wrote the code we need to simulate a single round of BlackJack, but there was a lot of redundant code in it because we didn&#8217;t have functions yet. We will start by refactoring this code to make it more modular. We will start with a new epp template, but almost all the code will be copied and pasted from Assignment 5.<br>1) Declare global string variables for the shuffled Deck, playerHand, and dealer Hand. These strings will be used by many functions so we need them to be global<br>2) Move the code to create a shuffled deck into its own function called newShuffledDeck() a. The function takes no input and retums a 52-character string that represents a<br>shuffled deck. Re-use your code from Step 1-2 of Assignment 5. b. Test your function by calling it from main() and printing the results. Like last<br>time, you should get something that looks like this:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>clang++-7-pthread -std-c++17 -o main main.cqp &gt;\/main<br>JBK92J2KK30970000686778980735444230AA246065AA6533593&gt;<br>Leave the testing code in main() for now.<\/p>\n<\/blockquote>\n\n\n\n<p><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-6-color\"><strong>The Correct Answer and Explanation is :<\/strong><\/mark><\/p>\n\n\n\n<p>Here&#8217;s the code that refactors the Blackjack program according to the instructions:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\n#include &lt;cstdlib&gt;\n#include &lt;ctime&gt;\n#include &lt;string&gt;\n#include &lt;algorithm&gt;\n\nusing namespace std;\n\n\/\/ Declare global variables\nstring shuffledDeck;\nstring playerHand;\nstring dealerHand;\n\n\/\/ Function to create a shuffled deck\nstring newShuffledDeck() {\n    string deck = \"23456789TJQKA23456789TJQKA23456789TJQKA23456789TJQKA\";\n    random_shuffle(deck.begin(), deck.end());  \/\/ Shuffle the deck\n    return deck;\n}\n\nint main() {\n    \/\/ Test the newShuffledDeck() function\n    shuffledDeck = newShuffledDeck();\n    cout &lt;&lt; \"Shuffled Deck: \" &lt;&lt; shuffledDeck &lt;&lt; endl;\n\n    \/\/ Test the results by printing\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Global Variables<\/strong>:<br>The global variables <code>shuffledDeck<\/code>, <code>playerHand<\/code>, and <code>dealerHand<\/code> are declared outside the functions so they can be accessed by various functions within the program. This makes it easier to manage the deck and hands throughout the game.<\/li>\n\n\n\n<li><strong>newShuffledDeck Function<\/strong>:<br>The function <code>newShuffledDeck()<\/code> is defined to create a shuffled deck. It first creates a string representing a deck of cards (52 characters: each card has two characters, the first is the rank, and the second is the suit). The <code>random_shuffle<\/code> function from the C++ standard library is used to shuffle the string (deck) randomly. Finally, the shuffled deck is returned.<\/li>\n\n\n\n<li><strong>Testing the Function<\/strong>:<br>Inside the <code>main()<\/code> function, the <code>newShuffledDeck()<\/code> function is called, and the shuffled deck is stored in the global variable <code>shuffledDeck<\/code>. It is then printed to verify that the deck has been shuffled correctly. The output should resemble a shuffled string of cards, like:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   Shuffled Deck: JBK92J2KK30970000686778980735444230AA246065AA6533593<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Next Steps:<\/h3>\n\n\n\n<p>This refactor lays the groundwork for more modular code. In future steps, you will move the logic for dealing cards, calculating the hand values, and handling the game flow into their own respective functions. This structure makes the code easier to maintain and debug, as each piece of functionality is contained within a well-defined function.<\/p>\n\n\n\n<p>Let me know if you need further clarification or additional parts of the code!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Part 1: Refactor the Assignment 5 Code Into New FunctionsIn Week 5, we wrote the code we need to simulate a single round of BlackJack, but there was a lot of redundant code in it because we didn&#8217;t have functions yet. We will start by refactoring this code to make it more modular. We will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-191860","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/191860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=191860"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/191860\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=191860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=191860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=191860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}