Some Background
I wanted to practice Python Classes in a fun way and what’s better than creating a game out of it. I chose “Black Jack” specifically as I could imagine that it will require multiple classes and functions, and at the same time it won’t be overly complicated for a beginner/intermediate person like me.
black jack overview
The game is also known as Twenty-One. It is one of the famous games played in casinos and is the perfect game for someone who excels at mathematics. But even for a casual participant, it can be pretty thrilling and challenging.
Game steps:
- The goal of blackjack is to beat the dealer’s hand without going over 21.
- Face cards are worth 10. Aces are worth 1 or 11, whichever makes a better hand.
- You start with two cards, one of the dealer’s cards is hidden until the end.
- To ‘Hit’ is to ask for another card. To ‘Stand’ is to hold your total and end your turn.
- If you go over 21 you bust, and the dealer wins regardless of the dealer’s hand.
- Once you have decided to ‘Stand’, dealer will hit until his/her cards total 17 or higher.
- The person with the higher value wins and chips are adjusted accordingly
NOTE: The actual rules may differ, but to keep the project from being over complicated I am going to define the project based on the above rules.
Project overview
The overall structure for the project:
- Create a deck of 52 cards.
- Shuffle the deck.
- Deal 2 cards each to the player and the dealer.
- Ask the player for his/her bet amount.
- Display some cards.
- Ask for the player if he/she wants to hit or stand.
- Keep on asking unless the player decides either to stand or exceeds 21.
- Decide the result and deduct/add the appropriate bet amount from the total.
Flow chart for the entire process
Defining Classes
- Card Class
- It will take two parameters: rank and suit.
- We will also define a function to print the card here.
- Deck Class
- It will be a self initialized class which will rely on our Card Class to create the deck of cards.
- We will add two methods here:
- shuffle: for shuffling the deck
- deal: for dealing a card from the deck
- Hand Class
- It will be used to create two objects for the player’s hand and dealer’s hand.
- We will add two methods here:
- add_card: to add a random card to the hand and update the overall value of that hand
- adjust_ace_value: to adjust the value of ace in case the total exceeds 21
- Chips Class
- It will be used to handle the chips for the player
- It will be initialized with two values:
- total: to store the total number of chips
- bet: to store the bet amount
- We will add two methods here:
- bet_won: adds the bet amount to the total on winning the game
- bet_lost: deducts the bet amount from the total on losing the game
Defining Functions
Aside from all the classes, we will be defining some functions that will help us in the logic and general structure of the game.
- bet_this: used to set the bet amount
- show_some_cards: used to show 1 of the dealer card and all of the player cards
- show_all_cards: used to show all of the dealer and player cards
- hit_or_stand: asks the player if he/she wishes to hit or stand every time a card to dealt
- hit_card: deals a new card
- player_won: handles the condition where the player wins
- player_lost: handles the condition where the player loses
- dealer_won: handles the condition where the dealer wins
- dealer_lost: handles the condition where the dealer loses
- tie: handles the condition where the dealer and player ends up in tie
Code
You can find the complete code on my GitHub repository here.
Hey very nice blog!! Man .. Beautiful .. Amazing .. I’ll bookmark your I’m happy to find so many useful info here in the post, we need work out more strategies in this regard, thanks for sharing. . . . . .