Advertisement
Guest User

PythonCards

a guest
Jun 2nd, 2011
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.16 KB | None | 0 0
  1.  
  2. class Card:
  3.     suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
  4.     ranks = ["narf", "Ace", "2", "3", "4", "5", "6", "7",
  5.              "8", "9", "10", "Jack", "Queen", "King"]
  6.  
  7.     def __init__(self, suit=0, rank=2):
  8.         """ Initializes a card """
  9.         self.suit = suit
  10.         self.rank = rank
  11.  
  12.     def __str__(self):
  13.         """ String Representation """
  14.         return (self.ranks[self.rank] + " of " + self.suits[self.suit])
  15.  
  16.     def __cmp__(self, other):
  17.         """ Compares cards, returns 1 if greater, -1 if lesser, 0 if equal """
  18.         # check the suits
  19.         if self.suit > other.suit: return 1
  20.         if self.suit < other.suit: return -1
  21.         # suits are the same... check ranks
  22.         # check for aces first.
  23.         if self.rank == 1 and other.rank == 1: return 0
  24.         if self.rank == 1 and other.rank != 1: return 1
  25.         if self.rank != 1 and other.rank == 1: return -1
  26.         # check for non-aces.
  27.         if self.rank > other.rank: return 1
  28.         if self.rank < other.rank: return -1
  29.         # ranks are the same... it's a tie
  30.         return 0
  31.  
  32.  
  33. class Deck:
  34.     def __init__(self):
  35.         """ Initializes the deck """
  36.         self.cards = []
  37.         for suit in range(4):
  38.             for rank in range(1, 14):
  39.                 self.cards.append(Card(suit, rank))
  40.  
  41.     def __str__(self):
  42.         """ String Representation """
  43.         s = ""
  44.         for i in range(len(self.cards)):
  45.             s = s + " " * i + str(self.cards[i]) + "\n"
  46.         return s
  47.  
  48.     def print_deck(self):
  49.         """ Prints the deck """
  50.         for card in self.cards:
  51.             print (card)
  52.  
  53.     def shuffle(self):
  54.         """ Shuffles the deck """
  55.         # Modifier!
  56.         import random
  57.         rng = random.Random()        # create a  random generator
  58.         rng.shuffle(self.cards)      # use its shuffle method
  59.  
  60. ##    def removeOriginal(self, card):
  61. ##        """ Removes the card from the deck, returns true if successful """
  62. ##        if card in self.cards:
  63. ##            self.cards.remove(card)
  64. ##            return True
  65. ##        else:
  66. ##            return False
  67. ##
  68. ##    def remove2(self, card):
  69. ##        """ Removes the card from the deck, returns true if successful """
  70. ##        for lol in self.cards:
  71. ##            if lol == card:
  72. ##                self.cards.remove(lol)
  73. ##                return True
  74. ##        return False
  75.  
  76.     def remove(self, card):
  77.         """ Removes the card from the deck, returns true if successful """
  78.         for lol in self.cards:
  79.             if lol.__cmp__(card) == 0:
  80.                 self.cards.remove(lol)
  81.                 return True
  82.         return False
  83.  
  84.     def pop(self):
  85.         """ Removes and returns the card at the bottom """
  86.         # Bottom deal ftw!
  87.         return self.cards.pop()
  88.  
  89.     def is_empty(self):
  90.         """ Checks if the deck is empty """
  91.         return (len(self.cards) == 0)
  92.  
  93.     def deal(self, hands, num_cards=999):
  94.         """ Deals the deck to hands """
  95.         num_hands = len(hands)
  96.         for i in range(num_cards):
  97.             if self.is_empty(): break   # break if out of cards
  98.             card = self.pop()           # take the top card
  99.             hand = hands[i % num_hands] # whose turn is next?
  100.             hand.add(card)              # add the card to the hand
  101.  
  102.  
  103. class Hand(Deck):
  104.     def __init__(self, name=""):
  105.         """ Initializes the hand """
  106.         self.cards = []
  107.         self.name = name
  108.  
  109.     def add(self,card):
  110.         """ Adds a card to the hand """
  111.         self.cards.append(card)
  112.  
  113.     def __str__(self):
  114.         """ String Representation """
  115.         s = "Hand " + self.name
  116.         if self.is_empty():
  117.             s = s + " is empty\n"
  118.         else:
  119.             s = s + " contains\n"
  120.         return s + Deck.__str__(self)
  121.  
  122.  
  123. class CardGame:
  124.     def __init__(self):
  125.         """ Initializes the game. Creates a deck and shuffles it """
  126.         self.deck = Deck()
  127.         self.deck.shuffle()
  128.  
  129.  
  130. class OldMaidHand(Hand):
  131.     def remove_matches(self):
  132.         count = 0
  133.         original_cards = self.cards[:]
  134.         for card in original_cards:
  135.             match = Card(3 - card.suit, card.rank)
  136.             for lol in self.cards:
  137.                 if lol.__cmp__(match) == 0:
  138.                     self.cards.remove(card)
  139.                     self.cards.remove(match)
  140.                     print("Hand {0}: {1} matches {2}".format(self.name, card, match))
  141.                     count = count + 1
  142.         return count
  143.  
  144. ##    def remove_matchesOriginal(self):
  145. ##        count = 0
  146. ##        original_cards = self.cards[:]
  147. ##        for card in original_cards:
  148. ##            match = Card(3 - card.suit, card.rank)
  149. ##            if match in self.cards:
  150. ##                self.cards.remove(card)
  151. ##                self.cards.remove(match)
  152. ##                print("Hand {0}: {1} matches {2}".format(self.name, card, match))
  153. ##                count = count + 1
  154. ##        return count
  155.  
  156. class OldMaidGame(CardGame):
  157.     def play(self, names):
  158.         # remove Queen of Clubs
  159.         self.deck.print_deck()
  160.         self.deck.remove(Card(0, 12))
  161.         print("---------- {0} has been removed from the deck".format(str(Card(0, 12))))
  162.         # make a hand for each player
  163.         self.hands = []
  164.         for name in names:
  165.             self.hands.append(OldMaidHand(name))
  166.         # deal the cards
  167.         self.deck.deal(self.hands)
  168.         print("---------- Cards have been dealt")
  169.         self.printHands()
  170.         # remove initial matches
  171.         matches = self.remove_all_matches()
  172.         print("---------- Matches discarded, play begins")
  173.         self.printHands()
  174.         # play until all 50 cards are matched
  175.         turn = 0
  176.         numHands = len(self.hands)
  177.         while matches < 25:
  178.             matches = matches + self.play_one_turn(turn)
  179.             turn = (turn + 1) % numHands
  180.         print("---------- Game is Over")
  181.         self.printHands()
  182.  
  183.     def remove_all_matches(self):
  184.         count = 0
  185.         for hand in self.hands:
  186.             count = count + hand.remove_matches()
  187.         return count
  188.  
  189.     def play_one_turn(self, i):
  190.         if self.hands[i].is_empty():
  191.             return 0
  192.         neighbor = self.find_neighbor(i)
  193.         pickedCard = self.hands[neighbor].pop()
  194.         self.hands[i].add(pickedCard)
  195.         print("Hand", self.hands[i].name, "picked", pickedCard)
  196.         count = self.hands[i].remove_matches()
  197.         self.hands[i].shuffle()
  198.         return count
  199.  
  200.     def find_neighbor(self, i):
  201.         numHands = len(self.hands)
  202.         for next in range(1,numHands):
  203.             neighbor = (i + next) % numHands
  204.             if not self.hands[neighbor].is_empty():
  205.                 return neighbor
  206.  
  207.     def printHands(self):
  208.         for hand in self.hands:
  209.             print(hand)
  210.  
  211. ##game = CardGame()
  212. ##hand = OldMaidHand("frank")
  213. ##game.deck.deal([hand], 13)
  214. ##print(hand)
  215. ##hand.remove_matches()
  216. ##print(hand)
  217.  
  218. ##game = OldMaidGame()
  219. ##game.play(["Allen","Jeff","Chris"])
  220.  
  221. # THIS IS NOT WORKING. Remove functions do not work because it cant match any
  222. # searched card to any other card in the deck.  Something to do with Card being
  223. # a totally seperate object.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement