Advertisement
Guest User

Untitled

a guest
Sep 7th, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.48 KB | None | 0 0
  1. GAMES
  2. #A simple game module
  3.  
  4. class Player(object):
  5.     """A player for a game."""
  6.     def __init__(self, name, score = 0):
  7.         self.name = name
  8.         self.score = score
  9.  
  10.     def __str__(self):
  11.         rep = self.name + "\t" + str(self.score)
  12.         return rep
  13.  
  14. def ask_yes_no(question):
  15.     """Asks a question"""
  16.     response = None
  17.     while response not in ("y", "n"):
  18.         response = input(question).lower()
  19.     return response
  20.  
  21. def ask_number(question, low, high = 5):
  22.     """Asks a number within a range"""
  23.     response = None
  24.     while response not in range(low, high):
  25.         try:
  26.             response = int(input(question))
  27.         except:
  28.             print(response, "isn't a valid choice")
  29.     return response
  30.  
  31. if __name__ == "__main__":
  32.     print("You ran this module directly (and didn't import it)")
  33.     input("Press blah-blah to exit...")
  34.  
  35.  
  36.  
  37. -----------------------------------------------------------------------------------------------
  38. GCARD
  39.  
  40.  
  41.  
  42. class Card(object):
  43.     """docstring for Card"""
  44.  
  45.     SUITS = ["H", "S", "A", "D"]
  46.     VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
  47.  
  48.     def __init__(self, suit, value, face_up = True):
  49.         self.suit = suit
  50.         self.value = value
  51.         self.face_up = face_up
  52.  
  53.     def __str__(self):
  54.         if self.face_up:
  55.             rep = self.suit + self.value
  56.         else:
  57.             rep = "XX"
  58.         return rep
  59.  
  60.     def flip(self):
  61.         self.face_up = not self.face_up
  62.  
  63.     @property
  64.     def points(self):
  65.         self.points = Card.VALUES.index(self.value) + 1
  66.    
  67. class Hand(object):
  68.     """docstring for Hand"""
  69.     def __init__(self):
  70.         self.cards = []
  71.  
  72.     def __str__(self):
  73.         if self.cards:
  74.             rep = ""
  75.             for card in self.cards:
  76.                 rep += str(card) + "\t"
  77.         else:
  78.             rep = "<empty>"
  79.         return rep
  80.  
  81.     def add(self, card):
  82.         self.cards.append(card)
  83.  
  84.     def give(self, card, other_hand):
  85.         self.cards.remove(card)
  86.         other_hand.add(card)
  87.  
  88.     def clear(self):
  89.         self.cards = []
  90.  
  91. class Deck(Hand):
  92.  
  93.     def fill(self):
  94.         if not self.cards:
  95.             for suit in Card.SUITS:
  96.                 for value in Card.VALUES:
  97.                     self.add(Card(suit, value))
  98.         else:
  99.             print("Can't fill. The deck isn't empty")
  100.  
  101.     def shuffle(self):
  102.         import random
  103.         random.shuffle(self.cards)
  104.  
  105.     def deal(self, players, rounds = 1):
  106.         for rnd in range(rounds):
  107.             for player in players:
  108.                 if self.cards:
  109.                     top_card = self.cards[0]
  110.                     self.give(top_card, player)
  111.                 else:
  112.                     print("Can't deal: out of cards")
  113.  
  114.     if __name__ == "__main__":
  115.         print("This is a module for a game of cards")
  116.         input("Press the enter key to exit...")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement