class Dealer(Player):
    def __init__(
        self
    ):  # Constructor to initalize the dealer info and ensure that Player class' (the base class) constructor is invoked as necessary
        super().__init__("Dealer",
                         self)  # Invokes the Player class' constructor
        self.deck = CardDeck()  # Creates a new CardDeck object
        self.natural_blackjack_dflag = 0

    def shuffle_deck(
            self):  # The dealer uses the Cardeck Object to shuffle the deck
        self.deck.shuffle()  # Shuffles the deck

    def signal_hit(
        self, player
    ):  # Used to request a card after which, the dealer picks up a card from the top of the pile and gives it to the person who requested the card
        player.deal_to(self.deck.draw()
                       )  # The dealer draws the topmost card from the deck

    def play_round(self):  # Simulates the dealer playing a round of blackjack
        if self.card_sum < 17:  # The dealer hits as long his card sum is less than 17
            self.deal_to(self.deck.draw(
            ))  # The dealer hits (draws a car from the top of the deck)
            self.play_round(
            )  # Recursively calls play round till the dealer's card sum is more than 16 after which, the dealer stands
Exemple #2
0
class Dealer(Player):
    def __init__(self):
        self.deck = CardDeck()
        super().__init__('Dealer', self)

        return

    def shuffle_deck(self):

        self.deck.shuffle()
        return

    def signal_hit(self, player):

        player.deal_to(self.deck.draw())
        return

    def play_round(self):

        while self.card_sum < 17:
            self.deal_to(self.deck.draw())
        return
Exemple #3
0
class Dealer(Player):

    def __init__(self):
        super().__init__("Dealer", None)
        self.deck = CardDeck()

    def shuffle_deck(self):
        """
        >>> import random; random.seed(1)
        >>> dealer = Dealer()
        >>> dealer.shuffle_deck()
        >>> str(dealer.deck)[0:20]
        '10 8 10 6 8 9 3 10 2'
        """
        self.deck.shuffle()

    def signal_hit(self, player):
        """
        A method called by players when they want to hit
        Player objects should pass their `self` references to this method
        Should deal one card to the player that signalled a hit

        These doctests will not run properly if the `deal_to` method 
        in the `Player` class is not properly implemented

        >>> import random; random.seed(1)
        >>> dealer = Dealer()
        >>> dealer.shuffle_deck()
        >>> player = Player(None, None)
        >>> dealer.signal_hit(player)
        >>> player.hand
        [10]
        """
        card = self.deck.draw()
        if card:
            player.deal_to(card)

    def play_round(self):
        """
        A dealer should hit if his hand totals to 16 or less

        >>> import random; random.seed(1)
        >>> dealer = Dealer()
        >>> dealer.shuffle_deck()
        >>> dealer.play_round()
        >>> dealer.hand
        [10, 8]
        """
        while not self.busted and self.card_sum <= 16:
            self.signal_hit(self)
class Dealer(Player):

    def __init__(self):
        super().__init__('Dealer', self)
        self.deck = CardDeck()
        self.busted = False
        self.black_jack = False
        self.is_processing_done = False
        self.is_standing = False


    def shuffle_deck(self):
        """
        >>> import random; random.seed(1)
        >>> dealer = Dealer()
        >>> dealer.shuffle_deck()
        >>> str(dealer.deck)[0:20]
        '10 8 10 6 8 9 3 10 2'
        """
        self.deck.shuffle()

    def signal_hit(self, player):
        """
        A method called by players when they want to hit
        Player objects should pass their `self` references to this method
        Should deal one card to the player that signalled a hit

        These doctests will not run properly if the `deal_to` method 
        in the `Player` class is not properly implemented

        >>> import random; random.seed(1)
        >>> dealer = Dealer()
        >>> dealer.shuffle_deck()
        >>> player = Player(None, None)
        >>> dealer.signal_hit(player)
        >>> player.hand
        [10]
        """

        if player != None:
            player.deal_to(self.deck.draw())
        else:
            self.deal_to(self.deck.draw())

        


    def play_round(self):
        """
        A dealer should hit if his hand totals to 16 or less

        >>> import random; random.seed(1)
        >>> dealer = Dealer()
        >>> dealer.shuffle_deck()
        >>> dealer.play_round()
        >>> dealer.play_round()
        >>> dealer.hand
        [10, 8]
        """
        if len(self.hand) < 2:
            if len(self.hand) == 0:
                self.signal_hit(None)
                return
            if len(self.hand) == 1:
                self.signal_hit(None)
                if self.card_sum > 16 and self.card_sum < 21:
                    self.is_standing = True
                    return
                if self.card_sum == 21:
                    self.busted = True
                    self.black_jack = True
                    return
                if self.card_sum > 21:
                    self.busted = True
                    return
        if self.card_sum <= 16:
            while self.card_sum <= 16:
                self.signal_hit(None)
                if self.card_sum > 16 and self.card_sum < 21:
                    self.is_standing = True
                    break
                if self.card_sum == 21:
                    self.busted = True
                    self.black_jack = True
                    break
                if self.card_sum > 21:
                    self.busted = True
                    break
                    
    def get_name(self):
        return self.__class__.__name__
    
    def __str__(self):
        """Return string representation for str()."""
        return f'Dealer: {self.hand} 0/0/0'