Entry 6024

card_classes.py

   

Submitted by anonymous on Aug. 13, 2010 at 10:37 p.m.
Language: Python. Code size: 9.5 KB.

'''

Card and Deck Classes:
------------------------------------------------------------------------

  ********************************************************************
  
  Code by Julien McArdle for <removed> job application. Python code 
  tested with Python 2.6.5 under Ubuntu Linux. Julien can be 
  contacted at the following address:
  
     julien@jmcardle.com
     
  ********************************************************************

Object "card":

        OVERVIEW
        ============
        This is an object representing a single card.

        INITIALIZING
        ============
        There are two optional input arguments when initializing the card
        object. They are the card's suit and rank. If no input arguments
        are used, the suit and rank are defined as "None."
          card(suit, rank)
          ie. card(1, "A")
        
        Where 1 in the above example means "spades", and "A" means an Ace 
        card. The suits are 1 (spades), 2 (hearts), 3 (diamonds) and 
        4 (clubs). The ranks are the integers 1,2,3,4,5,6,7,8,9,10,11,12,13. 
        1 can be substituted for the string "A", 11 for "J", 12 for "Q" 
        and 13 for "K".

        METHODS
        ============
        card.getSuit():
                Returns the suit of the card as an integer, where 1 = spades, 
                2 = hearts, 3 = diamonds, and 4 = clubs.
        card.getRank():
                Returns the rank of the card as an integer. Ace is given as 1,
                Jack as 11, Queen as 12, and King as 13.
        card.setSuit(Suit):
                Set the suit of the card. Input argument is a numerical value,
                where 1 = spades, 2 = hearts, 3 = diamonds, and 4 = clubs.
        card.setRank(Rank):
                Set the rank of the card. Input argument can be a numerical value,
                from 1 to 13. Alternatively, the characters "A", "J", "Q", and 
                "K" can also be used to denote a rank.
        card.printSuit():
                Returns the suit of the card as a string.
        card.printRank():
                Returns the rank of the card as a string.
        card.printCard():
                Returns the suit and rank of the card as a string.
                

Object "stackOfCards":

        OVERVIEW
        ============
        This is an object representing a series of card pobjects.
        
        INITIALIZING
        ============
        This object takes in a list of card objects as an input argument. 
        A full deck of cards can be created by using the following line 
        of code:
            deckOfCards = stackOfCards(stackOfCards().fullOrderedDeck())

        METHODS
        ============
        numberOfCards():
                Returns an integer indicating how many cards are in the stack
                of cards.
                
        showCards():
                Returns a list containing all the cards in the stack of cards.
        showCard(i):
                Returns the card at the index i. 0 represents the bottom of the
                deck of card.
        showTopCard():
                Returns the card at the top of the stack.
                
        takeCard(i):
                Returns the card at the index i, and removes it from the stack 
                of cards.
        takeTopCard():
                Returns the card at the top of the stack, and removes it from
                the pack of cards.
                
        cutCards():
                Returns two lists of cards, each containing a half of the current
                stack of cards.
        dealCards(numberOfPlayers, cardsPerPlayer):
                Input arguments are the number of players to deal cards for 
                (default: 1), and how many cards each player is assigned (default:
                1). The output is a list of stackOfCards objects, one for
                each of the players that was dealt cards.
        reverseCardOrder():
                Reverses the order of the cards in the stack, such that the top
                card becomes the bottom card and vice versa.
        shuffleCards():
                Shuffles the stack of cards.
        

Assumptions when Coding:
------------------------------------------------------------------------
* That the input argument to the stackOfCards object when initializing 
  it will be valid.
* That the user is running a version of Python compatible with the code
  used in this source file.

'''

import copy, random

########################################################################
####  CARD OBJECT

class card:
        
        # DICTIONARIES FOR THE CARD OBJECT
        
        suitTypes = {1:"spades",2:"hearts",3:"diamonds",4:"clubs"}
        rankTypes = {1:"A",11:"J",12:"Q",13:"K"}
        
        # INPUT VALIDATION
        
        def __valueToRank__(self, input):
                if input == "A":
                        return 1
                elif input == "J":
                        return 11
                elif input == "Q":
                        return 12
                elif input == "K":
                        return 13
                elif input >= 1 and input <= 13:
                        return input 
                else:
                        return None
                        
        def __valueToSuit__(self, input):
                if input >= 1 and input <= 4:
                        return input
                else:
                        return None
                        
        # PRINT THE STRING REPRESENTATION OF THE CARD VALUE
                        
        def printCard(self):
                if self.__rank__ != None and self.__suit__ != None:
                        return str(self.printRank()) + " of " + str(self.printSuit())
                else:
                        return None
                        
        def printRank(self):
                if self.__rank__ == None:
                        return None
                if self.__rank__ >= 2 and self.__rank__ <= 10:
                        return str(self.__rank__)
                else:
                        return self.rankTypes[self.__rank__]
                
        def printSuit(self):
                if self.__suit__ == None:
                        return None
                return self.suitTypes[self.__suit__]
        
        # SET/GET SUIT AND RANK
                
        def setSuit(self, assignedSuit=None):
                self.__suit__ = self.__valueToSuit__(assignedSuit)
                        
        def setRank(self, assignedRank=None):
                self.__rank__ = self.__valueToRank__(assignedRank)
                
        def getSuit(self):
                return self.__suit__
                
        def getRank(self):
                return self.__rank__
                
        # INITIALIZE THE CARD OBJECT
                
        def __init__(self, assignedSuit=None, assignedRank=None):
                self.setSuit(assignedSuit)
                self.setRank(assignedRank)
                
########################################################################
####  STACK OF CARDS OBJECT

class stackOfCards:
        
        # CREATE A FRESH DECK OF CARDS
        
        def fullOrderedDeck(self):
                listOfCards = list()
                for currentSuit in range(1,5):
                        for currentRank in range(1,14):
                                listOfCards.append(card(currentSuit, currentRank))
                return listOfCards
                
        # OPERATIONS ON THE WHOLE STACK OF CARDS
                                
        def shuffleCards(self):
                random.shuffle(self.__cards__)
                
        def cutCards(self):
                copyOfStack = copy.deepcopy(self.__cards__)
                halfTheCards = int(len(self.__cards__) / 2)
                return copyOfStack[:halfTheCards], copyOfStack[halfTheCards:]
                
        def reverseCardOrder(self):
                self.__cards__.reverse()
                
        def dealCards(self, numberOfPlayers=1, cardsPerPlayer=1):
                stacksForAllPlayers = list()
                for i in range(numberOfPlayers):
                        cardsForPlayer = list()
                        for j in range(cardsPerPlayer):
                                cardsForPlayer.append(self.__cards__.pop())
                        stackForPlayer = stackOfCards(cardsForPlayer)
                        stacksForAllPlayers.append(stackForPlayer)
                return stacksForAllPlayers
                                
        def numberOfCards(self):
                return len(self.__cards__)
                
        def showCards(self):
                copyOfStack = copy.deepcopy(self.__cards__)
                return copyOfStack
                
        # OPERATIONS ON SPECIFIC CARDS
                
        def showTopCard(self):
                return self.__cards__[-1]
                
        def takeTopCard(self):
                return self.__cards__.pop()
                
        def showCard(self, index=0):
                return self.card[index]
                
        def takeCard(self, index=0):
                return self.card.pop(index)
                                
        # INITIALIZE THE STACK OF CARDS OBJECT
        
        def __init__(self, listOfCards=None):
                self.__cards__ = listOfCards

This snippet took 0.02 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).