Exemplo n.º 1
0
 def __init__(self, shoe):
     self.shoe = shoe
     self.dealer = Dealer(shoe)
     self.player = User(shoe)
     self.initial_hand_size = 2
     self.max_target_score = 21
     self.dealer_trigger = 17
Exemplo n.º 2
0
class Game:
    """ Initialize game of blackjack

    Parameters
    ---------
    shoe : Instance of Shoe Class
        The current shoe initialized in Blackjack.sitting()


    Attributes
    ----------
    dealer : instance of Player class
    player : instance of Player class
    initial_hand_size : int
        Number of cards initially dealt
    max_target_score : int
        A constant set to 21 per the rules of blackjack
    dealer_trigger : int
        A constant set to 17, the score at which the dealer will stop accepting
        additional cards, per the rules of blackjack
    """
    def __init__(self, shoe):
        self.shoe = shoe
        self.dealer = Dealer(shoe)
        self.player = User(shoe)
        self.initial_hand_size = 2
        self.max_target_score = 21
        self.dealer_trigger = 17

    def deal(self):
        """ Deals the set # of starting cards (defined in the init)"""
        player_list = [self.dealer, self.player]
        for elem in player_list:
            for x in range(self.initial_hand_size):
                if self.shoe.cards:
                    elem.hand_of_cards.append(self.shoe.hand_out_card())
                else:
                    return False
        return True

    def report(self, finished=False):
        """ Reports the status of the game to the stdout on command line """
        
        if finished is False:
            verb = "has"
        else:
            verb = "had"
        print(chr(27) + "[2J")
        print("Dealer {}: {}For a total of {}"
              .format(verb, self.dealer.pretty_output(), self.dealer.score()))
        print("\n")
        print("Player {}: {}For a total of {}"
              .format(verb, self.player.pretty_output(), self.player.score()))
        print("\n")

    def eval_state(self):
        """ Evaluates the status of the game to the stdout on command line

        Returns
        -------
        string
            "d", "p", or None (if out of cards or unresolved)
        """
        winner = None
        if self.dealer.status["bj"] or self.player.status["bust"]:
            winner = "d"
        elif self.player.status["bj"] or self.dealer.status["bust"]:
            winner = "p"
        return winner

    def get_winner(self):
        """ Determines the winner if both players pass

        Returns
        -------
        string 
            "d" (dealer) or "p" (player) for victor

        """
        if self.player.score() <= self.dealer.score():
            return "d"
        else:
            return "p"


    def report_results(self):
        """ Report final outcome of the hand

        Returns
        -------
        True
            As notification of execution

        """
        print("Dealer finished with: ", self.dealer.score())
        print("You had: ", self.player.score())
        print("\n")
        return True

    def play(self):
        """ Plays a single round of blackjack

        Returns
        -------
        instance of Player object
        
        """

        if not self.deal():
            return None  # .hand_out_card reports back no more cards
        if not self.dealer.update_status() or\
               not self.player.update_status():
                print("Something went wrong.")
        winner = self.eval_state()        
        while not winner:
            self.report()
            if self.dealer.status["interested"] and self.dealer.hits() and\
               not self.dealer.get_card():
                return None  # .get_card reports back no more cards
            elif self.player.status["interested"] and self.player.hits() and\
                 not self.player.get_card():
                return None  # .get_card reports back no more cards
            
            if not self.dealer.update_status() or\
               not self.player.update_status():
                print("Something went wrong.")  # If update fails 
            winner = self.eval_state()
            if winner:
                break
            elif not self.dealer.status["interested"] and\
                 not self.player.status["interested"]:
                winner = self.get_winner()
                break
        self.report(finished=True)
        if self.report_results():
            
            return winner 
        else:
            print("Something went wrong.")
            return None