Intro to Python: Day 8 - Playing Poker with Python (OOP)
Tags: python, beginners, oop, programming
Published: 2023-08-08 15:30:08

*Hi there 👋 I'm a New York City based web developer documenting my journey with React, React Native, and Python for all to see. Please follow my dev.to profile or [my twitter](https://twitter.com/stonestwebdev) for updates and feel free to reach out if you have questions. Thanks for your support!*

This is a fun one. As some of you know I have been taking the [boot.dev](https://boot.dev) backend programming online course and am just finishing up another module related to OOP.

In this lesson we created a `DeckOfCards` class, which had the poker suits and hands defined on it as class variables. Then we define the constructor so as to build a deck of 52 cards of unique suit + rank combinations (e.g. "Ace of Spades") represented by tuples (e.g. `card = ("Spade", "Ace")`).

The following code shows the beginnings of the class with those class variables defined on them.

```python
import random

class DeckOfCards:
    SUITS = ["Hearts", "Diamonds", "Clubs", "Spades"]
    RANKS = [
        "Ace",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "Jack",
        "Queen",
        "King",
    ]
```

The `random` package is also imported for us to use later.

Now we define a constructor function and a `create_deck` function so that we actually have a deck to work with. The `create_deck` function uses a nested loop to create a unique tuple for each suit + rank combination:

```python
    def __init__(self):
        self.__cards = []
        self.create_deck()

    def create_deck(self):
        for suit in self.SUITS:
            for rank in self.RANKS:
                new_card = (rank, suit)
                self.__cards.append(new_card)
```

Now that we have our deck and it is stored in the private `__cards` instance variable, we can create a function to shuffle the deck just like in real poker. This makes use of the `random.shuffle()` method to shuffle the list of cards in place.

```python
    def shuffle_deck(self):
        random.shuffle(self.__cards)
```

Last, all we need to do is deal a card to begin playing. For this we check if there are any cards left in the deck, and if there are we pick the top card. The top card will be random because it was shuffled, just like in real poker:

```python
    def deal_card(self):
        if (len(self.__cards) == 0):
            return None
        else:
            top_card = self.__cards.pop(0)
            return top_card
```

This was a fun demonstration of the power of object-oriented programming because it makes use of the class to perform all of the behavior and store all of the data necessary for a simple poker deck. 

Unlike functional programming which attempts to make programs which are more transparent and predictable with simple in-out relationships to data, class-based OOP relies on the ability to store both data and methods on a class to accomplish a wide array of tasks.

**If you like projects like this and want to stay up to date with more, check out my Twitter [@stonestwebdev](https://twitter.com/stonestwebdev), I follow back! See you tomorrow for another project.**