Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GAMES
- #A simple game module
- class Player(object):
- """A player for a game."""
- def __init__(self, name, score = 0):
- self.name = name
- self.score = score
- def __str__(self):
- rep = self.name + "\t" + str(self.score)
- return rep
- def ask_yes_no(question):
- """Asks a question"""
- response = None
- while response not in ("y", "n"):
- response = input(question).lower()
- return response
- def ask_number(question, low, high = 5):
- """Asks a number within a range"""
- response = None
- while response not in range(low, high):
- try:
- response = int(input(question))
- except:
- print(response, "isn't a valid choice")
- return response
- if __name__ == "__main__":
- print("You ran this module directly (and didn't import it)")
- input("Press blah-blah to exit...")
- -----------------------------------------------------------------------------------------------
- GCARD
- class Card(object):
- """docstring for Card"""
- SUITS = ["H", "S", "A", "D"]
- VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
- def __init__(self, suit, value, face_up = True):
- self.suit = suit
- self.value = value
- self.face_up = face_up
- def __str__(self):
- if self.face_up:
- rep = self.suit + self.value
- else:
- rep = "XX"
- return rep
- def flip(self):
- self.face_up = not self.face_up
- @property
- def points(self):
- self.points = Card.VALUES.index(self.value) + 1
- class Hand(object):
- """docstring for Hand"""
- def __init__(self):
- self.cards = []
- def __str__(self):
- if self.cards:
- rep = ""
- for card in self.cards:
- rep += str(card) + "\t"
- else:
- rep = "<empty>"
- return rep
- def add(self, card):
- self.cards.append(card)
- def give(self, card, other_hand):
- self.cards.remove(card)
- other_hand.add(card)
- def clear(self):
- self.cards = []
- class Deck(Hand):
- def fill(self):
- if not self.cards:
- for suit in Card.SUITS:
- for value in Card.VALUES:
- self.add(Card(suit, value))
- else:
- print("Can't fill. The deck isn't empty")
- def shuffle(self):
- import random
- random.shuffle(self.cards)
- def deal(self, players, rounds = 1):
- for rnd in range(rounds):
- for player in players:
- if self.cards:
- top_card = self.cards[0]
- self.give(top_card, player)
- else:
- print("Can't deal: out of cards")
- if __name__ == "__main__":
- print("This is a module for a game of cards")
- input("Press the enter key to exit...")
Advertisement
Add Comment
Please, Sign In to add comment