Create our own Blackjack Game using Python
Blackjack is a card-based game played at gambling clubs. The members in this game don’t contend with one another yet the vendor doled out by the club. In this article, we will make the Blackjack game between a player and a seller without any preparation, that can be played on the terminal.
Rules of Blackjack
We will give a concise arrangement of rules for perusers who have never played Blackjack. The enchantment number for Blackjack is 21. The qualities for all the cards managed to a player are added and if the aggregate surpasses 21, the player busts and loses quickly.
On the off chance that a player gets a definite 21, the player wins against the seller. Something else, to win, the amount of the player’s cards should be more than the amount of the seller’s cards.
Each face card has a definite value of 10, whereas the ace can be counted as 1 or 11 suitable to the player’s chances of winning. The value of the rest of the cards is defined by their number.
The dealing of the cards in a game of Blackjack is as follows:
- A card is dealt to the player facing upwards (visible to everyone).
- The dealer deals a card to himself visible to everyone.
- Another card is given to the player facing upwards.
- The dealer deals a card facing downwards for himself.
- The player has to decide whether to stand with the current set of cards or get another card.
- If the player decides to hit, another card is dealt.
- If the player decides to stand, then the dealer reveals his hidden card.
- The dealer does not have the authority to decide whether to hit or stand. The general rule is that the dealer needs to keep hitting more cards if the sum of dealer’s cards is less than 17.
- As soon as the sum of dealer’s cards is either 17 or more, the dealer is obliged to stand.
- According to the final sum of the cards, the winner is decided.
The programming of the Blackjack game becomes simple as soon as the rules are understood. Creating a terminal-based game from scratch requires three main components: The Game Design, The Game Logic, and Management of Player Interaction.
Blackjack Game Demo
Designing Blackjack in Python
Firstly, we will work on our game’s design. Our job is to effectively display a series of cards on the terminal something like the following figure.
We need a function that prints a sequence of cards and is independent of the number of cards. Moreover, it must provide a functionality to print a hidden card if needed.
The following code solves our problem.
The details of each Card are stored as a Card Object. The second parameter of the
print_cards() function is a boolean value that indicates whether a hidden card is to be displayed or not.
Creating a Card
With the help of classes and objects, we can create an ensemble of suits and values to represent a “playing card”. In Blackjack, a card has three properties, its suit, its representing value and its value as score.
All the above properties are maintained within the following Card Class.
Using the above class we can create a proper deck of cards containing 52 Card objects.
Some fundamental values
Each game of cards requires fundamental values like the types of suits, the types of cards, and the values for each card.
One thing to note here is that, the Ace is initially marked as a 11-point card. The idea behind this strategy is that whenever the player/dealer’s score seems to cross 21, we can reduce the score of Ace (if dealt) to 1.
We will see the implementation of the reduction later, in this article.
Generate a deck of playing cards
A normal deck of playing cards consists of 52 cards, each bearing a different combination of suit and value. Using the above fundamental values and the Card Class, we generate a deck of cards.
In reality, a game of Blackjack involves multiple decks, therefore the above set of loops can be re-used for populating multiple decks.
The newly created deck is passed to the function that executes the game.
Let us learn the game logic behind a single iteration of a Blackjack Game between a player and a computer-based dealer.
Declaring Important Game Variables
At any instant of time, we require the following game variables:
- A list of cards dealt to the player and the dealer.
- The sum of the card values for each side.
These game variables come into play when we design the game logic.
Python Blackjack Game Logic
The entire game logic revolves around the dealing of cards and player’s choices for either hitting or standing. As soon as we handle the above two things, we are done for the day.
First Phase of Dealing: Mandatory Cards
The initial dealing involves giving two cards to the player and the dealer. However, the second card for the dealer must remain unknown.
It might be a lot to imbibe for a seemingly simple dealing. Let us understand the process involved in the above code:
- The main loop runs until the player and the dealer gets two cards each.
- A card is randomly chosen from the deck and in the next step that card is removed from the deck.
- The card’s value is added to the player’s score.
- Similarly, a card is randomly chosen for the dealer and its value is added to the dealer’s score.
- The player’s cards are displayed on the screen normally.
- The dealer’s cards are displayed carefully such that the second card and its value are not revealed.
- In case, either of the participants gets double Aces, their scores are adjusted such that neither of them busts.
- After all the above things happen smoothly, we move on to the second stage of dealing.
Note: There is an inconspicuous contrast between the change of scores for the player and the vendor. In the previous case, the estimation of the principal card is changed, while, in the last mentioned, the estimation of the subsequent card is changed.
The explanation behind changing the estimation of the subsequent card is that, had we changed the first, we would have uncovered the character of the concealed card as an Ace.
Something last that should be done here is checking whether the player as of now has a Blackjack. On the off chance that he does, the player dominates and the match closes.
input()function pauses the program until the player presses “ENTER”. This prevents a quick fall-through of all the game events.
clear()function is responsible for clearing the terminal giving a clean aesthetic for the game.
Second phase of dealing: Player’s Choices
The second stage of dealing depends on the player’s decision of either wanting another card for boosting score or standing with the current set of cards.
The player decides whether to hit or stand until the score exceeds 21 or the player decides to stand. There is no limit to the number of cards dealt to the player, just on the score.
Every time the player decides to hit, a new card is dealt from the deck and the score is updated. As mentioned before, an Ace can be counted as 1 or 11. A special piece of code converts Ace’s value from 11 to 1, in case the score exceeds 21.
The player stands when he is satisfied with the current score. When he does, we move on to the final stage of dealing after making some mandatory checks like a Blackjack or a busting scenario.
Final phase of dealing: Dealer’s Cards
In the final stage of dealing, the hidden card of the dealer is revealed and so is the dealer’s score. According to the standard Blackjack rules, the dealer has to deal more cards to himself until its score is more than or equal to 17.
The dealer keeps hitting until the score crosses 17 mark. We have a similar implementation of converting card values of Aces from 11 to 1, if needed.
The End Game
When the dealer’s score is either 17 or more, we move onto the End Game, which involves comparing of values and nominating the winner of the game. There can be a few scenarios possible:
- The Dealer Busts – The dealer’s score exceeds 21.
- The Dealer has Blackjack – The dealer has an exact score of 21.
- A Tie Game – Both the player and the dealer has equal score.
- The Player Wins – The player’s score is more than that of the dealer.
- The Dealer Wins – The dealer’s score is more than that of the player.
We check for each of the above possibilities and declare the winner.
This concludes a single iteration of a game of Blackjack between a player and a dealer.
Complete Python Code for Blackjack Game
The reader is not obliged to follow the entire coding sequence. There can be various amends made to the above code, by adding the facility of multiple players against the dealer.
A game of Blackjack may appear to be basic and arbitrary from the outset, yet just when the players follow certain systems like Card Counting, the game gets intricate.
There are numerous variants of Blackjack skimming across the world like Swedish Pub Blackjack and Home Game Blackjack. Inquisitive perusers can find out about these variations and attempt to execute them using the information acquired in this article.