Understanding OOP Python with card deck ♠︎️

Richard Taujenis
Geek Culture
Published in
4 min readJul 30, 2021

--

To start it off OOP Object-Oriented Programming why use it you ask?

Because it makes code more reusable and makes it easier to work with larger programs. OOP programs prevent you from repeating code because a class can be defined once and reused many times.

Defining a class

A class is like a blueprint for creating an object concept most known when talking about OOP.

An object is simply a collection of data (variables) and methods (functions) that act on those data.

As mentioned we will use OOP to help define a real-world concept in a way that the computer will acknowledge. Thats why my example is from card game BlackJack and will start of creating a deck. I will use the example from my own project.

Full project encapsulates Pygame GitHub link is here additionally will make a related blog for it.

Deck

A deck object really only needs two attributes: suit and rank.

SUITS = ['C', 'S', 'H', 'D']RANKS = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']

First of all we import random and I created constants file for SUITS, RANKS etc due to scaling my project in Pygame but you can just write it in same file.

So we create a Deck class we use self as the first parameter in each case. And we will have in the __init__ a “cards” array attribute and “build()” call method. The build as shown will iterate through RANKS and SUIT and append it to self.cards array.

And of course we shuffle and make a method that will deal the card from the deck using the pop method.

Hand

For the Hand class we start off by passing Deck class instance within as a parent class so we can get the shuffled cards additionally call the deal method. In the Hands __init__ method users cards are being set same as and self.value

IMPORTANT: If your just following the OOP path and not interested how the game can be implemented in Pygame take in account

REQUIRED IN PYGAME and END where it ends.Thank you!

Add card method to add card to user. The calc_hand method is for BlackJack game as an example.

At the begging of this method we create few List Comprehenisions that will handle to check if the card is an Ace or not. There after create a for loop for non aces if its ‘JQK’ assign value of 10 otherwise the number of the card like 6,7 etc.

For aces if the value is equal or bigger then 10 then assign value of 11 otherwise if self.value is bigger then 11 assign Ace value of 1.

Display cards will get the card from own cards and join the card first and second index of the card like ‘♠︎️’ and ‘J’. If its not already listed in users card_img then append it

Create the deck and player instances

We wont do the whole blackjack project here I will cover it in the Pygame version of the blog.We just

deck = Deck()
deck.shuffle()
player = Hand()
dealer = Hand()
for i in range(2):
player.add_card(deck.deal())
dealer.add_card(deck.deal())
print(player.cards)
print(dealer.cards)
player.calc_hand()
dealer.calc_hand()
print(dealer.value)
print(player.value)
The print result of player, dealers hand and value of hand

Ones the classes are created initializing the Deck comes at first by assigning it to deck variable. And use the shuffle() method we created within it.

Then player and dealer get their own Hand() instance and because the Hand class had passed in Deck in the parameter makes Hand the child and Deck the parent instance.Thus both player and dealer get cards from the same deck.

As we recall the Hand() class has add_card() method and calc_hand() with those we are able to add the hand of the player as well as calculate the hand!

Conclusion

I hope you enjoyed the article. OOP was the thing that took me time to grasp and something I wanted to avoid for quite a while.But then I figured out I can not scale with projects properly without OOP approach of creating classes and making multiple inheritances based upon project requirements.

As mentioned before I will follow up with BlackJack pygame blog in the near future that will double down on the importance of OOP approach.GitHub link bellow if interested.

Related Stories

--

--

Richard Taujenis
Geek Culture

Quality engineer here to bring you relevant content related to Python, JavaScript(VueJS) and so much more. Find out more 🌲 https://linktr.ee/richard_taujenis