Refactor the Assignment 5 Code Into New Functions

Part 1: Refactor the Assignment 5 Code Into New Functions
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’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.
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
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
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
time, you should get something that looks like this:

clang++-7-pthread -std-c++17 -o main main.cqp >/main
JBK92J2KK30970000686778980735444230AA246065AA6533593>
Leave the testing code in main() for now.

The Correct Answer and Explanation is :

Here’s the code that refactors the Blackjack program according to the instructions:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include <algorithm>

using namespace std;

// Declare global variables
string shuffledDeck;
string playerHand;
string dealerHand;

// Function to create a shuffled deck
string newShuffledDeck() {
    string deck = "23456789TJQKA23456789TJQKA23456789TJQKA23456789TJQKA";
    random_shuffle(deck.begin(), deck.end());  // Shuffle the deck
    return deck;
}

int main() {
    // Test the newShuffledDeck() function
    shuffledDeck = newShuffledDeck();
    cout << "Shuffled Deck: " << shuffledDeck << endl;

    // Test the results by printing
    return 0;
}

Explanation:

  1. Global Variables:
    The global variables shuffledDeck, playerHand, and dealerHand 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.
  2. newShuffledDeck Function:
    The function newShuffledDeck() 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 random_shuffle function from the C++ standard library is used to shuffle the string (deck) randomly. Finally, the shuffled deck is returned.
  3. Testing the Function:
    Inside the main() function, the newShuffledDeck() function is called, and the shuffled deck is stored in the global variable shuffledDeck. It is then printed to verify that the deck has been shuffled correctly. The output should resemble a shuffled string of cards, like:
   Shuffled Deck: JBK92J2KK30970000686778980735444230AA246065AA6533593

Next Steps:

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.

Let me know if you need further clarification or additional parts of the code!

Scroll to Top