Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- import random
- import time
- RANDMAX = 1<<32
- random.seed(time.time())
- # I like C's rand().
- def rand():
- return random.randint(0, RANDMAX)
- def flatten(list):
- return [item for sublist in list for item in sublist]
- # Thanks, StackOverflow.
- class Card:
- (ST_SPADE, ST_HEART, ST_CLUB, ST_DIAMOND, ST_END) = xrange(5)
- SUIT_STR = ("Spade", "Heart", "Club", "Diamond")
- VAL_STR = ("Joker","Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King")
- def __init__(self, suit = None, value = None):
- self.suit = suit
- self.value = value
- if self.suit is None:
- self.suit = rand() % Card.ST_END
- if self.value is None:
- self.value = rand() % len(Card.VAL_STR)
- def info(self):
- if self.value == 0:
- return Card.VAL_STR[self.value]
- return "%s of %ss" % (Card.VAL_STR[self.value], Card.SUIT_STR[self.suit])
- class Deck:
- def __init__(self):
- self.generate()
- self.shuffle()
- self.drawn = []
- def generate(self):
- generate_suit = lambda suit: map(lambda value: Card(suit, value), xrange(1, len(Card.VAL_STR)))
- joker = lambda: Card(value = 0)
- # ^ Probably being overly defensive here, but I've been burned by pass-by-reference before.
- self.cards = [joker()] * 2 + flatten(map(generate_suit, xrange(Card.ST_END)))
- def reassemble(self):
- while len(self.drawn) > 0:
- self.undraw()
- def shuffle(self):
- random.shuffle(self.cards)
- def info(self):
- for card in self.cards:
- print card.info()
- def undraw(self):
- self.cards.append(self.drawn.pop())
- def draw(self):
- if len(self.cards) == 0:
- self.reassemble()
- self.shuffle()
- self.drawn.append(self.cards.pop())
- return self.drawn[-1]
- def deal(self, hands, amt):
- for hand in hands:
- hand.empty_hand()
- hand.draw(self, amt)
- class Hand:
- def __init__(self):
- self.cards = []
- def draw(self, deck, amt):
- for x in xrange(amt):
- self.cards.append(deck.draw())
- def empty_hand(self):
- while len(self.cards) > 0:
- self.cards.pop()
- def remove_card(self, index):
- c = self.cards[index]
- self.cards.remove(c)
- return c
- def info(self):
- print "Your hand:"
- for card in self.cards:
- print " -> %s" % card.info()
- def showcase_hand___or___5_drunk_friends_attempt_to_play_poker():
- deck = Deck()
- players = map(lambda x: Hand(), [None] * 5)
- deck.deal(players, 5)
- print "Our game begins with five players. Player 0, the dealer, deals everyone their cards. Five each, in accordance with the rules of poker"
- print
- print "Everyone looks at their hands..."
- print
- for player in players:
- print "Player %i:" % (players.index(player))
- player.info()
- print
- uno_card = players[0].remove_card(3)
- print "Player 0 lays down a card--a %s--and attempts to call 'Uno!', apparently not realizing this is a game of poker." % (uno_card.info())
- print
- print "Player 0 appears to be drunk."
- print
- print "Surprisingly, the other players roll with it. Player 0 looks at his hand..."
- players[0].info()
- print
- print "Suddenly, player 4 calls out player 0, pointing out that he in fact has four cards, not one. Player 0 is forced to draw three cards."
- players[0].draw(deck, 3)
- print
- print "Player 0 looks at his hand again..."
- players[0].info()
- print
- print "Suddenly, player 3 decides to steal everyone's cards (including the %s that player 0 laid down earlier) and yell them out one by one" % (uno_card.info())
- print
- for card in deck.drawn:
- print " * %s" % card.info()
- print
- print "Unsurprisingly, everyone leaves the room and the game ends."
- def showcase_deck(): # this function doesn't get called
- d = Deck()
- for x in xrange(54):
- print d.draw().info()
- print
- print "-- Deck has been exhausted. Let's try to break it by drawing a few more! --"
- print
- for x in xrange(10):
- print d.draw().info()
- print
- print "As you can see, after the deck has been exhausted, it is reassembled and re-shuffled"
- print "I considered generating a new deck, but I admittedly don't know what happens to 'lost' objects in python."
- print "So to avoid memory leaks, I opted for frugality. Drawn cards are kept track of."
- showcase_hand___or___5_drunk_friends_attempt_to_play_poker()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement