Advertisement
rae

Python cards

rae
Oct 31st, 2014
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. SUITS=('S','D','C','H')
  2. RANKS=('A',2,3,4,5,6,7,8,9,10,'J','Q','K')
  3.  
  4. # define card class
  5. class Card:
  6.     def __init__(self, suit, rank):
  7.         if (suit in SUITS) and (rank in RANKS):
  8.             self.suit = suit
  9.             self.rank = rank
  10.         else:
  11.             self.suit = None
  12.             self.rank = None
  13.             print "Invalid card: ", suit, rank
  14.  
  15.     def __str__(self):
  16.         return "%s%s" % (self.suit, self.rank)
  17.  
  18.     def get_suit(self):
  19.         return self.suit
  20.  
  21.     def get_rank(self):
  22.         return self.rank
  23.  
  24.     # def draw(self, canvas, pos):
  25.     #    card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank),
  26.     #                CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
  27.     #    canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE)
  28.        
  29. # define hand class
  30. class Hand:
  31.     def __init__(self):
  32.         self.hand =[]
  33.         # create Hand object
  34.  
  35.     def __str__(self):
  36.         s = None
  37.         for card in self.hand:
  38.             print "# adding %s" % str(card)
  39.             if s:
  40.                 s = s + "," + str(card)
  41.             else:
  42.                 s = str(card)
  43.         return s
  44.  
  45.     def add_card(self, card):
  46.         print "Adding card %s" % card
  47.         self.hand.append(card)
  48.         # add a card object to a hand
  49.  
  50.     def get_value(self):
  51.         pass
  52.         #aceval = 1
  53.         #self.val1 = 0
  54.         # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
  55.         # compute the value of the hand, see Blackjack video
  56.    
  57.     # def draw(self, canvas, pos):
  58.     #    pass   # draw a hand on the canvas, use the draw method for cards
  59.  
  60. card1 = Card("S", 2)
  61. print "Card is %s" % str(card1)
  62. card2 = Card("H", "J")
  63. myHand = Hand()
  64. myHand.add_card(card1)
  65. myHand.add_card(card2)
  66. print "Hand is %s" % str(myHand)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement