예제 #1
0
    def play(self):
        players = self._players
        shoe = self._shoe
        #movegen = MoveGenerator(shoe)
                    
        while True:
            if players:
                eliminated_players = []
                for player in players:
                    if not player.can_bet(self._MINIMUM_BET):
                        eliminated_players.append(player)
                        players.remove(player)
                if eliminated_players:
                    print '<'*60
                    print 'Following players cannot bet any further and are removed from table:'
                    for player in eliminated_players:
                        print 'Player {0}'.format(player.id)
                    print '>'*60
                    pass
                else:
                    #Collect bets from each player
                    bets = self._collect_bets(players)
                    
                    #Initialize them a hand for the bet they made
                    hands = [ [player,Hand(bet)] for player, bet in zip(players, bets) ]
                    dealer_hand = Hand(0)
                    
                    
                    print '#'*60
                    print 'First Card will be served'
                    print '#'*60
                    #Serve them their first card
                    [hand.insert(shoe.draw()) for player,hand in hands]
                    #Serve the dealer the first card
                    #TODO When we have limits for dealer, break conditions here
                    dealer_hand.insert(shoe.draw())
                    #Print state for each player
                    for player,hand in hands:
                        print 'Player {0} : Your Cards are :'.format(player.id)
                        print hand
                        print '*'*50

                    print 'Dealers Hand :'
                    print dealer_hand
                    print '*'*50

                    
                    print '#'*60
                    print 'Next Card will be served'
                    print '#'*60
                    #Serve them their second card
                    [hand.insert(shoe.draw()) for player,hand in hands]
                    dealer_hand.insert(shoe.draw())
                    
                    for player,hand in hands:
                        print 'Player {0} : Your Cards are :'.format(player.id)
                        print hand

                    print '#'*60
                    
                    final_hands =[]
                    
                    for player,hand in hands:
                        for ret_hand in self._user_action(player,hand):
                            final_hands.append(ret_hand)
                    
                    #Show Dealer's Hand 
                    print '#'*60
                    print 'Dealer\s Hand'
                    print dealer_hand  

                    while True:
                        dealer_value = dealer_hand.evaluate()
                        if dealer_value in range(1,17):
                            self._move_generator._hit(self._dealer,dealer_hand)
                        else:
                            break

                    print '#'*60
                    print 'Dealer\'s Final Hand'
                    print dealer_hand     
                    #Print Dealer's state 
                    
                    for player,hand in final_hands:
                        payoff_code = self._compare(hand,dealer_hand)
                        value = self._payoff(player, hand, payoff_code)
                        
                        if payoff_code >  1:
                            print 'Player {0} had a blackjack. Gets Back {1}'.format(player.id, value)
                        if payoff_code == 1:
                            print 'Player {0} had a win.       Gets Back {1}'.format(player.id, value)
                        if payoff_code == 0:
                            print 'Player {0} had a push.      Gets Back {1}'.format(player.id, value)
                        if payoff_code <  0:
                            print 'Player {0} had a loss.      Loses     {1}'.format(player.id, value)

                        #collect used cards
                        for card in hand._cards:
                            self._shoe.collect(card)    
                    
                    
            else:
                print 'Table is now empty. Now closing the table.'
                break