C++

Build a Blackjack Game in C++

Blackjack is the world’s quite famous banking game played worldwide daily. It is in between a player and a dealer (not another player). A player used to compare their cards with the dealers. The player has to defeat the dealer with some rules already been set. Three things will define the player’s win. The player must defeat the dealer by getting the 21 points on the first 2 cards, i.e., natural or blackjack. Another is to get a higher score in the end. The third is to let the opposite draw additional cards. This guide will show you to create a blackjack game in C++ within Ubuntu 20.04 system. We have started our program with the necessary header files.

We have declared some function prototypes that will be used in the code in a sequence.

Defined some global variables and the main() method. The “Rules()”, “Difficulty()” and “PlayOneHand()” functions will be called one after another. Let’s see how these functions work.

The method “Rules()” has nothing to return because it demonstrates the game rules, the winning point rules, and levels of difficulty for the player’s ease using the “cout” clauses.

The “Difficulty()” method executes get the difficulty level from a user when he/she enters 1 or 2. The “if” statements are used to check if a user has entered 1 or 2 or not. If not, the buffer will be cleaned up, and this function “Difficulty” will be called again to enter the difficulty level.

After the user enters its level of difficulty, the “PlayOneHand” function got executed. It declared a local variable “Play” equals to “N” and then a “Draw” variable in the “do” statement. The array “deck” of size 52 is declared. The function “initializeDeck” is called to initialize the “Deck,” and the function “shuffle” is used to shuffle the deck. After that, we have initialized two lists, “Ph” and “dH”. The “pH” stands for player hands while the “dH” stands for dealer’s hand.

We have called the “addToHand” function while passing it the “getTopCard” function and “pH” or “dH” arrays in random order. In the “getTopCard()” function, we have also passed the “deck” array as a parameter argument. The cout statements show that the shuffling has been done. The “naturalBlackJack” function and “blackjack” function are called with 3 parameters, i.e., pH, dH, y, or Y. The “showCards()” function is called 2 times to show the cards in player and dealer hand. The “checkSoftorHard” function is called to change the value of an ace card by a user. While the “userWantsToDraw” function is called, we will be performing some actions.

While loop is calling addtoHand(), showCards(), checkSoftOrHard(), checkBust(), and blackjack() function. The hitUntilStands(), checkBust(), blackjack(), whoWins() function is outside to check the win, loss and display it. While the “playAnotherHand” is called to play once more if wanted.

The function “initializeDeck” is used to build the values of cards backgrounds to set the rank in the 10s position and the suit in the 100s position. You can see, we have used the for loop for hearts, diamonds, clubs, and spades separately.

Here comes the “shuffle” method, which is being used to rearrange the cards randomly. This has been done with a huge looping, and the card clones have been made. It will continue to work until the loop ends and all the cards get shuffled.

We have called this function in the “addToHand” function parameter, so discuss it first. So, it has been taking the “deck” array in its parameter and looping all its cards up to the last one. It will find out the specific card from the deck which is not empty and make a copy of it first. Then, it will make it empty by replacing and returning the copied value, i.e., the “topCard” variable, to the “addToHand” function.

Now, the “addToHand” function takes the “topCard” returned from the “getTopCard” function, and the “hand[]” array that has been passed to it either it is dealer hand “dH” or player’s hand “pH”. If the hand has an empty spot for the card, it will replace that spot with the value “cardToAdd” returned from the “getTopCard” value.

This function will get the scores of players and dealers using pH and dH. It is checking whether the player has a blackjack or not. If so, it will call the “scoreboard” function, calculate the winning, call the “backdoorKenny” function, and then the “playAnotherHand” function.

The “blackjack” function will check whether the player has a blackjack or both the player and a dealer has a blackjack. If a player has blackjack, it will call the “scoreboard”, “backdoorKenny,” and “playAnotherHand” function while calculating the winnings. If both have blackjack, it will call the “scoreboard” and “playAnotherHand” function while calculating the winnings.

The “showCards” is all about showing the card values at the terminal while the deck is not empty for players and dealers.

The “checkSoftOrHard” function will be executed to ask a user if he/she wants ace to be counted as 1 or 11. Through this, we will calculate further scores or cards.

The “userWantsToDraw” function call ask the user to draw another card or skip the loop. If the user enters “h” or “H,” it means playing one more time, or it will be skipped.

The “checkBust” function is called when a player or a dealer encounters a score of 21.

This function “hitUntilStand” will be utilized to let a dealer hit while it gets 17. It would be decoded at beginner or expert levels,

The “softOrHardAI” function has been called here to play wisely.

Here comes the “getHandValue()” function to sum the values of the card by getting the values from the function “CardValue” and returning the updated one.

Here comes the “CardValue” function, which checks the background of a card to get its 10s value. This can be called for any card, and it will be called many times in this code.

The “scoreboard” function takes pH and dH to display the current cards of the player and dealer using the “ShowCards()” and display the card value using the “getHandValue” function.

When a user has 1 card of 10 points while the second card is “Ace,” using the“CardValue” function, it will calculate its winning as a 1:4 payout.

This function is called when we want to know about the current card’s rank and its suit.

The ranks are checked with the “switch” statement, and suits are checked via if-else statements.

The “dumpDeck” method will be called to display the deck values.

The “getRandomNumber” has been utilized in the program to get any number from the provided high and low values.

This function is all about logic to check if the dealer has an “ace” or not. If so, the dealer will try to create 21 and win.

The function “whoWins()” will be called to decide the winner by using their scores.

The playAnotherHand() function is called to prompt the user if he/she wants to play once again after one whole game.

The compilation is performed using the “g++” compiler.

The execution shows the blackjack panel. So, here you go.

Conclusion:

This article is a complete bundle of a c++ code to create a Blackjack game in Ubuntu 20.04. We have utilized more than 20 user-defined functions to achieve its functionality. All the functions are interdependent, and we have tried to minimize the complexity by explaining them in a sequence. We hope this whole article will be easy to understand.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.