import string import game class Player: """Player has money and plays cards. A player knows how to play some games, but not necessarily all. We _delegate_ the game playing to another class.""" def __init__(self, name, cash=0): self.name = name self.cards = [] self.cash = cash <= 0 and 300 or cash self.play = {} def draw(self, card): self.cards.append(card) def discard(self, card): self.cards.remove(card) def learn(self, play): if not isinstance(play, game.GamePlay): raise ValueError, 'not learning game play' self.play[play.GameName] = play def gamePlay(self, game): return self.play.get(game.GameName, None) def win(self, amount): self.cash = self.cash + amount def bet(self, amount): if self.cash < amount: raise ValueError, 'not enough money' self.cash = self.cash - amount def show(self, showHoleCard=1): cards = map(str, self.cards) if not showHoleCard: cards[0] = '??' print '%s: %s' % (self.name, string.join(cards)) class Dealer(Player): """Dealer plays for the house.""" def __init__(self): Player.__init__(self, 'Dealer', 1000000) def show(self, showHoleCard=0): Player.show(self, showHoleCard) if __name__ == '__main__': import deck d = deck.Deck() d.shuffle() p = Player('Joe') p.draw(d.deal()) p.draw(d.deal()) p.draw(d.deal()) p.show()