Guest User

Untitled

a guest
Dec 21st, 2010
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. class Card:
  2.     suits = ['Clubs', 'Diamonds', 'Hearts', 'Spades']
  3.     ranks = [None, 'Ace', '2', '3', '4', '5',
  4.             '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King']
  5.    
  6.     def __init__(self, suit, rank):
  7.         self.suit=suit
  8.         self.rank=rank
  9.        
  10.     def __str__(self):
  11.         return '%s %s' % (self.suits[self.suit], self.ranks[self.rank])
  12.        
  13. class Deck:
  14.     def __init__(self):
  15.         self.cards=[]
  16.         for suit in range (4):
  17.             for rank in range(1, 14):
  18.                 card=Card(suit, rank)
  19.                 self.cards.append(card)
  20.                    
  21.     def __str__(self):
  22.         res = [str(card) for card in self.cards]
  23.         return '\n'.join(res)
  24.        
  25.     def shuffle(self):
  26.         import random
  27.         random.shuffle(self.cards)
  28.    
  29.     def pop_card(self):
  30.         return self.cards.pop()
  31.        
  32.     def add_card(self, card):
  33.         self.cards.append(card)
  34.        
  35.     def move_cards(self, hand, num):
  36.         for i in range(num):
  37.             hand.add_card(self.pop_card())
  38.        
  39. class Hand(Deck):
  40.     def __init__(self, label=''):
  41.         self.cards=[]
  42.         self.label=label
  43.        
  44. hand1=Hand('player1')
  45. deck = Deck()
  46. deck.shuffle()
  47. deck.move_cards(hand1, 5)
  48. for item in hand1.cards:
  49.     print item
  50.    
  51.  
  52. ranks = [i.split()[1] for i in hand1.cards]
Advertisement
Add Comment
Please, Sign In to add comment