def display_hands(self): for player in range(self.num_players + 1): display_name = 'Player %d' % player if player == self.num_players: display_name = 'Dealer' hand = self.hands[player] print '%s: [%s]' % (display_name, cards_to_string(hand))
def play(self): while True: logger.info('New Game') self.deal() self.display_hands() # dealer only checks for blackjack if their face up card is an Ace dealers_hand = self.hands[self.num_players] if dealers_hand[1].getValue() == 0 and self.get_value_of_hand(dealers_hand) == BLACK_JACK: logger.info('Dealer has blackjack') return for player in range(self.num_players): while True: action = raw_input('Player %d: [%s] - [H]it, [S]tay? ' % (player, cards_to_string(self.hands[player]))) if action.lower() == 's': logger.info('Player %d stays' % player) break if action.lower() == 'h': self.hands[player].append(self.cards.deal_one()) if self.get_value_of_hand(self.hands[player]) > BLACK_JACK: break while self.get_value_of_hand(self.hands[self.num_players]) < self.dealer_limit: self.hands[self.num_players].append(self.cards.deal_one()) logger.info('Dealer hits: [%s]', cards_to_string(self.hands[self.num_players])) dealer_hand_value = self.get_value_of_hand(self.hands[self.num_players]) if dealer_hand_value == BLACK_JACK: logger.info('Dealer has blackjack') elif dealer_hand_value > BLACK_JACK: logger.info('Dealer bust') for player in range(self.num_players): logger.info('Player %d wins' % player) else: for player in range(self.num_players): player_hand_value = self.get_value_of_hand(self.hands[player]) if player_hand_value > BLACK_JACK: logger.info('Player %d busts' % player) elif player_hand_value > dealer_hand_value: logger.info('Player %d wins' % player) elif player_hand_value == dealer_hand_value: logger.info('Player %d pushes' % player) else: logger.info('Player %d loses' % player) self.reset() logger.info('')
if value not in values: raise ValueError('Invalid value: %d' % value) self.value = value def __str__(self): return u'%s%s' % (suits[self.suit], values[self.value]) def getSuit(self): return self.suit def getValue(self): return self.value def setSuit(self, suit): if suit in suits: self.suit = suit def setValue(self, value): if value in values: self.value = value def toTuple(self): return (self.suit, self.value) if __name__ == '__main__': allow_unicode_console() cards = [Card('s', value) for value in range(14)] print cards_to_string(cards)