Build a Deck of Cards with OO Python

Build a Deck of Cards with OO Python

We will build a deck of cards with Object-Orientated Programming.

Step 1: Prepare our classes:

We will have three classes. A class Card, class Deck and class Player . Each of these will inherit from the object. We create an init method for each class.

Image for post

Step 2: Create our class Card:

The Card is going to take a suit and value self. We?re going to create the attributes suit, value and set them equal to whatever we pass in when we create a card.

Image for post

We create another method to show the card. Everything takes self so we have access it? s attribute self. In the show method we want to print out the card, so we make a string that will print out the value and the suit.

Image for post

We are now done with the Card class! Lets test this by creating an instance of the card:

Image for post

Step 3: Create our class Deck:

When we create a deck what do we want to do? Well we might first want to build our deck with 52 cards of 4 suits from Ace to King. We start with the init method and we initialize an attribute called cards with an empty list which we will append to and a build method to create our deck.

Image for post

We create a build method which takes in self, and we want to create the 52 cards of 4 suits. We create a for loop that will loop ?suit? through [“Spades”, “Clubs”, “Diamonds”, “Hearts”]. We create another for loop within the first for loop that will loop through values in range(1,14).

We then append Card(suit, value) to the self.cards list under the second for loop, which will give us ?1 of Spades?, ?2 of Spades? and so forth. This will add each card to the cards attribute list. We will now have 1?13 with suit displayed and we now have a total 52 cards (4 * 13 = 52)!

Image for post

We then make a show method that takes in self and for every card in self.cards (our list) it will show the card that it has. We then test the class!

Image for post

Step 4: Create a Shuffle method:

Now what do we want to do next? Well we would probably like to shuffle our deck. To do this we ?import random? to use the Fisher Yates shuffle which is a very neat shuffling algorithm that makes sure that each card has an equal likelihood of ending up in every other position.

Image for post

We then create a shuffle method that takes in self. We want to go from the end of our list back to the beginning. We create a for loop in our method, for ?i in (range(len(self.cards)) minus 1 (which is the last element and in this case we want to go to zero), 0, -1 decrement)?. If we run the loop we get values 51?1 which is what we want because when we are accessing elements in an array, we want to start with the length minus 1 because the index starts at 0. In this case we want to start at 1 because by the time we shuffle every other card, that 0 index is also going to be shuffled and we don?t want to shuffle that one itself.

Image for post

Next, we want to create a random number we call ?r = random.randit(0, i)? we?re passing our range from 0 to i. We only want to pick a number that is to the left our our current position. Because we are starting to the right of our list, iterating backwards and we want to pick a random index that is to the left of that. Then we want to swap two cards, the card at ?i? and the card at ?r?.

Image for post

Lets test our method by calling shuffle and show to display our deck!

Image for post

Step 5: Create a Draw Card method:

We would like to draw a card from our shuffled deck. To do this we simply create a drawCard method that takes in self. The method will return self.cards.pop() which will remove the last card from the top of the deck and return that card. Lets test this out.

Image for post

Step 6: Create Player:

Lastly, we create a class Player with a name attribute set to name and a hand attribute set to an empty list.

Next we create a draw method that takes in self and a deck in which we will pass in a deck. We append Deck.drawCard() to the self.hand and return self for the card drawn.

Image for post

We then create a showHand method to show the hand of the player. We create a for loop that will loop card in self.hand. It will then show our hand with card.show(). Now lets test our code!

Image for post

And now we can play around with our deck of cards. Hoora!

Source: youtube.com, Python OOP Deck of Cards, Eli Byers

19