Chapter 13 : Objects of Vectors

example 13.1 page no: 137

In [1]:
'''
example 13.1 page no: 137
'''
class Suit:
    CLUBS = 0
    DIAMOND = 1
    HEARTS = 2
    SPADES = 3

class Rank:    
    ACE=1
    TWO = 2
    THREE = 3
    FOUR =4
    FIVE = 5
    SIX = 6
    SEVEN = 7
    EIGHT = 8
    NINE= 9
    TEN = 10
    JACK = 11 
    QUEEN = 12
    KING =13
        
class Card:
    def __init__(self,s=None,r=None):
        if s==None:
            self.suit = 0
            self.rank = 0
        else:
            self.suit = s; 
            self.rank = r;

    def print_(self):
        suits = ["Clubs","Diamonds","Hearts","Spades"]
        ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
        print  ranks[self.rank] , " of " , suits[self.suit] 

deck = []

for suit in range(Suit.SPADES):
    for rank in range(Rank.KING):
        a = Card(suit,rank)
        deck.append(a)
        

for i in deck:
    i.print_()        
Ace  of  Clubs
2  of  Clubs
3  of  Clubs
4  of  Clubs
5  of  Clubs
6  of  Clubs
7  of  Clubs
8  of  Clubs
9  of  Clubs
10  of  Clubs
Jack  of  Clubs
Queen  of  Clubs
King  of  Clubs
Ace  of  Diamonds
2  of  Diamonds
3  of  Diamonds
4  of  Diamonds
5  of  Diamonds
6  of  Diamonds
7  of  Diamonds
8  of  Diamonds
9  of  Diamonds
10  of  Diamonds
Jack  of  Diamonds
Queen  of  Diamonds
King  of  Diamonds
Ace  of  Hearts
2  of  Hearts
3  of  Hearts
4  of  Hearts
5  of  Hearts
6  of  Hearts
7  of  Hearts
8  of  Hearts
9  of  Hearts
10  of  Hearts
Jack  of  Hearts
Queen  of  Hearts
King  of  Hearts

example 13.2 page no : 140

In [2]:
'''
example 13.2 page no : 140
'''
class Suit:
    CLUBS = 0
    DIAMOND = 1
    HEARTS = 2
    SPADES = 3

class Rank:    
    ACE=1
    TWO = 2
    THREE = 3
    FOUR =4
    FIVE = 5
    SIX = 6
    SEVEN = 7
    EIGHT = 8
    NINE= 9
    TEN = 10
    JACK = 11 
    QUEEN = 12
    KING =13

class Card:
    def __init__(self,s=None,r=None):
        if s==None:
            self.suit = 0
            self.rank = 0
        else:
            self.suit = s; 
            self.rank = r;

    def print_(self):
        suits = ["Clubs","Diamonds","Hearts","Spades"]
        ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
        print  ranks[self.rank] , " of " , suits[self.suit] 

class Deck:
    def __init__(self,n=None):
        self.cards = []
        if n ==None:
            size = 52
        else:
            size = n
                                 
        for suit in range(Suit.SPADES):
            for rank in range(Rank.KING):
                a = Card(suit,rank)
                self.cards.append(a)

    def print_(self):
        for i in self.cards:
            i.print_()

d = Deck(52)
d.print_()
Ace  of  Clubs
2  of  Clubs
3  of  Clubs
4  of  Clubs
5  of  Clubs
6  of  Clubs
7  of  Clubs
8  of  Clubs
9  of  Clubs
10  of  Clubs
Jack  of  Clubs
Queen  of  Clubs
King  of  Clubs
Ace  of  Diamonds
2  of  Diamonds
3  of  Diamonds
4  of  Diamonds
5  of  Diamonds
6  of  Diamonds
7  of  Diamonds
8  of  Diamonds
9  of  Diamonds
10  of  Diamonds
Jack  of  Diamonds
Queen  of  Diamonds
King  of  Diamonds
Ace  of  Hearts
2  of  Hearts
3  of  Hearts
4  of  Hearts
5  of  Hearts
6  of  Hearts
7  of  Hearts
8  of  Hearts
9  of  Hearts
10  of  Hearts
Jack  of  Hearts
Queen  of  Hearts
King  of  Hearts

example 13.3 page no : 142

In [3]:
'''
example 13.3 page no : 142
'''

class Card:
    def __init__(self,s=None,r=None):
        if s==None:
            self.suit = 0
            self.rank = 0
        else:
            self.suit = s; 
            self.rank = r;

    def print_(self):
        suits = ["Clubs","Diamonds","Hearts","Spades"]
        ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
        print  ranks[self.rank] , " of " , suits[self.suit] 

    def equals(self,c2):
        return (self.rank == c2.rank and self.suit == c2.suit);
        
    def find(self,deck):
        for i in range(len(deck.cards)):
            if self.equals(deck.cards[i]):
                return i
        return -1;

class Deck:
    def __init__(self,n=None):
        self.cards = []
        if n ==None:
            size = 52
        else:
            size = n
                                 
        for suit in range(4):
            for rank in range(13):
                a = Card(suit,rank)
                self.cards.append(a)

    def print_(self):
        for i in self.cards:
            i.print_()

d = Deck()
a = Card(1,11)
print a.find(d)
24

example 13.4 page no :144

In [4]:
'''
example 13.4 page no :144
'''

class Card:
    def __init__(self,s=None,r=None):
        if s==None:
            self.suit = 0
            self.rank = 0
        else:
            self.suit = s; 
            self.rank = r;

    def print_(self):
        suits = ["Clubs","Diamonds","Hearts","Spades"]
        ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
        print  ranks[self.rank] , " of " , suits[self.suit] 

    def equals(self,c2):
        return (self.rank == c2.rank and self.suit == c2.suit);
        
    def find(self,deck):
        for i in range(len(deck.cards)):
            if self.equals(deck.cards[i]):
                return i
        return -1;

class Deck:
    def __init__(self,n=None):
        self.cards = []
        if n ==None:
            size = 52
        else:
            size = n
            return
                                 
        for suit in range(4):
            for rank in range(13):
                a = Card(suit,rank)
                self.cards.append(a)

    def print_(self):
        for i in self.cards:
            i.print_()
            
    def subdeck(self,low,high):
        sub = Deck(high-low+1)
        for i in range(len(sub.cards)):
            sub.cards.append(self.cards[low+i])
        return sub;

deck = Deck()
hand1 = deck.subdeck (0, 4);
hand2 = deck.subdeck (5, 9);
pack = deck.subdeck (10, 51);

example 13.5 page no : 146

In [5]:
'''
example 13.5 page no : 146
'''
class Card:
    def __init__(self,s=None,r=None):
        if s==None:
            self.suit = 0
            self.rank = 0
        else:
            self.suit = s; 
            self.rank = r;

    def print_(self):
        suits = ["Clubs","Diamonds","Hearts","Spades"]
        ranks = ["Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"]
        print  ranks[self.rank] , " of " , suits[self.suit] 

    def equals(self,c2):
        return (self.rank == c2.rank and self.suit == c2.suit);
        
    def find(self,deck):
        for i in range(len(deck.cards)):
            if self.equals(deck.cards[i]):
                return i
        return -1;

class Deck:
    def __init__(self,n=None):
        self.cards = []
        if n ==None:
            size = 52
        else:
            size = n
            return
                                 
        for suit in range(4):
            for rank in range(13):
                a = Card(suit,rank)
                self.cards.append(a)

    def print_(self):
        for i in self.cards:
            i.print_()
            
    def merge(self,d2):
        # create a new deck big enough for all the cards
        result = Deck (len(d1.cards)+ len(d2.cards));
        # use the index i to keep track of where we are in
        # the first deck, and the index j for the second deck
        i = 0;
        j = 0;
        # the index k traverses the result deck
        return result;
In [ ]: