Skip to content
Related Articles

Related Articles

Improve Article
Shuffle a deck of card with OOPS in Python
  • Last Updated : 29 Dec, 2020

Prerequisites: Python Classes and Objects

Given a deck of cards, the task is to distribute them between two players.

Approach:

  • To shuffle the deck of cards we need to use the shuffle module.
  • Import the required module
  • Declare a class named Cards which will have variables suites and values, now instead of using self.suites and self.values, we are going to declare them as global variables.
  • Declare a class Deck which will have an empty list named as mycardset, and the suites and values will be appended to mycardset  list.
  • Declare a class ShuffleCards along with a method named shuffle() that would check the number of cards and then shuffles them.
  • To remove some cards we will create a popCard() method in ShuffleCards class.

Below is the complete program based on the above approach:

Python3




# Import required modules
from random import shuffle
  
  
# Define a class to create
# all type of cards
class Cards:
    global suites, values
    suites = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
    values = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
  
    def __init__(self):
        pass
  
  
# Define a class to categorize each card
class Deck(Cards):
    def __init__(self):
        Cards.__init__(self)
        self.mycardset = []
        for n in suites:
            for c in values:
                self.mycardset.append((c)+" "+"of"+" "+n)
  
    # Method to remove a card from the deck
    def popCard(self):
        if len(self.mycardset) == 0:
            return "NO CARDS CAN BE POPPED FURTHER"
        else:
            cardpopped = self.mycardset.pop()
            print("Card removed is", cardpopped)
  
  
# Define a class gto shuffle the deck of cards
class ShuffleCards(Deck):
  
    # Constructor
    def __init__(self):
        Deck.__init__(self)
  
    # Method to shuffle cards
    def shuffle(self):
        if len(self.mycardset) < 52:
            print("cannot shuffle the cards")
        else:
            shuffle(self.mycardset)
            return self.mycardset
  
    # Method to remove a card from the deck
    def popCard(self):
        if len(self.mycardset) == 0:
            return "NO CARDS CAN BE POPPED FURTHER"
        else:
            cardpopped = self.mycardset.pop()
            return (cardpopped)
  
  
# Driver Code
# Creating objects
objCards = Cards()
objDeck = Deck()
  
# Player 1
player1Cards = objDeck.mycardset
print('\n Player 1 Cards: \n', player1Cards)
  
# Creating object
objShuffleCards = ShuffleCards()
  
# Player 2
player2Cards = objShuffleCards.shuffle()
print('\n Player 2 Cards: \n', player2Cards)
  
# Remove some cards
print('\n Removing a card from the deck:', objShuffleCards.popCard())
print('\n Removing another card from the deck:', objShuffleCards.popCard())

Output:



 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :