C++: Blackjack


In this C++ tutorial, we program the game Blackjack using material from previous lessons. This video runs through the code for Blackjack, where the player tries to get as close to 21 without going over. There are many variations on Blackjack and this video shows one of the simplest ones. In fact, this version could probably be referred to, more properly, as Twenty-one, since a blackjack holds no special status in this game. Of course, we could easily add new rules into our game to accommodate any variation.

For our version of Blackjack, we have one player playing against the house. There are no specialized rules like insurance, double down, etc.; the player can only hit or stay. Unlike casinos, we use a single deck and it is reshuffled after every hand (no card counting!). Also, a tie, which is also known as a push, happens whenever the house and the player have the same score, regardless of whether or not there is a blackjack (21 in two cards); that is somewhat different than most casinos, since the blackjack rule was the reason that the name of the game was changed from Twenty-one. No bets may be placed in this game either. Finally, we mention that the hitting rules for the house are pretty standard: the house hits below seventeen and stays otherwise.

Just as we had in the Tic Tac Toe game tutorial, Blackjack has a main game loop. The main game loop runs once for each hand. Internally, there is a loop to ask the player whether he would like a hit. That is pretty much the overall flow of the game. When programming games, it is important to understand the flow of the game. Often, some of what we consider the simplest games are still extremely difficult to program.

Every card in the game is given with two characters: one for the rank and one for the suit. The rank is 2-9, T for ten, A for ace, J for jack, Q for queen, or K for King. The suits are given as C for clubs, D for diamonds, H for hearts, and S for Spades. The screenshot above shows the house with a six of diamonds up and the player with a queen and ten of hearts, for example.