Пример #1
0
    def play(self):
        """The core logic of Blackjack

        Args:
            None

        Returns:
            False: Retured to exit loop (This seems kludgy but I am not
                sure how to do it better
        """
        while True:
            # If more than 2/3 of the deck has been seen a new shoe is created.
            if len(self.deck) < 104:  # one third of a shoe filled with 6 decks
                self.deck = Blackjack()
            self.deal()
            self.evaluate()
            if input("Press 'enter' to continue.  Enter 's' to stop: ")\
                    .lower() == "s":
                return False
Пример #2
0
class BlackjackGame(object):
    """Blackjack game object.

    This object holds the methods, logic and values for the game of
    Blackjack.

    Attributes:
        val_dict: A dictionary that allows us to convert card ranks to
            values for the purpose of playing Blackjack.
        stand_val = the value at and above which the dealer will stand.
            For all values below stand_val the dealer will hit.
        bust_val = The value above which is a bust.  The goal of the game
            is to get as close to bust_val without going over as possible.
    """
    val_dict = {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9,
                10: 10, 11: 10, 12: 10, 13: 10}
    stand_val = 16
    bust_val = 21

    def __init__(self):
        """Creates a game of Blackjack with a Shoe that is shuffled."""
        self.deck = Blackjack()
        self.deck.shuffle()

    def cls(self):
        """"Clears the repl so that all text is printed a the top."""
        os.system('cls' if os.name == 'nt' else 'clear')

    def deal(self):
        """Resets dealer and player hand and deals 2 cards to each"""
        self.dealer = Hand()
        self.player = Hand()
        if len(self.deck) > 4:
            self.deck.move_cards(self.dealer, 2)
            self.deck.move_cards(self.player, 2)
            return True
        else:
            return False

    def dealer_turn(self):
        """Conducts the dealers Turn.

            The value of the dealers hand is checked. If the value of the
            dealers hand is higher than the players the dealer wins.
            Otherwise the deal follows standard rules (always hits if his
            hand is worth less than 17 otherwise stands).

            Args:
                None
            Returns:
                False: False is returned once the dealer cannot win.
                True: True is returned if the dealer has not busted and
                    still has a chance to win.
        """
        while True:
            print(self.print_dealer())
            if self.value(self.dealer) > self.value(self.player):
                return True
            if self.stand_val <= self.value(self.dealer) <= self.bust_val:
                print("Dealer Stands")
                return True
            else:
                print("Dealer Hits")
                self.deck.move_cards(self.dealer, 1)
            if self.value(self.dealer) > self.bust_val:
                print(self.print_dealer())
                return False
            else:
                input("Press enter to continue")
                continue

    def evaluate(self):
        """Evaluations the results in neither player busts.

        This function evuates the game state and declares a winner.
        The boolean values passed by the turn functions guide it. 
        If both players return True than the value of the hands is compared.

        Args:
            None

        Returns:
            None
        """
        p_res = self.player_turn()
        if p_res == "Blackjack":
            print("Congrats! Blackjack! You Win!")
        elif p_res == False:
            print("You Bust. You lose!")
        else:
            if self.dealer_turn() == False:
                print("Dealer busts.  You win!")
            else:
                if self.value(self.player) == self.value(self.dealer):
                    print("Push")
                elif self.value(self.player) > self.value(self.dealer):
                    print("You win!")
                else:
                    print("You lose!")

    def print_player(self):
        """Formats player output to be human readable"""
        self.cls()  # Clears the REPL so the text is easier to read
        return "Player's Turn:\
                \n=======================================\
                \nDealer: Has ???\nHand = {d_hand}, ???\n\
                \nPlayer: Has {p_val}\nHand = {p_hand}\n"\
                .format(d_val=self.value(self.dealer),
                        d_hand=self.dealer.cards[0],
                        p_val=self.value(self.player),
                        p_hand=self.player)

    def print_dealer(self):
        """Formats player output to be human readable"""
        self.cls()  # Clears the REPL so the text is easier to read
        return "Dealer's Turn:\
                \n=======================================\
                \nDealer: Has {d_val}\nHand = {d_hand}\n\
                \nPlayer: Has {p_val}\nHand = {p_hand}\n"\
                .format(d_val=self.value(self.dealer),
                        d_hand=self.dealer,
                        p_val=self.value(self.player),
                        p_hand=self.player)

    def play(self):
        """The core logic of Blackjack

        Args:
            None

        Returns:
            False: Retured to exit loop (This seems kludgy but I am not
                sure how to do it better
        """
        while True:
            # If more than 2/3 of the deck has been seen a new shoe is created.
            if len(self.deck) < 104:  # one third of a shoe filled with 6 decks
                self.deck = Blackjack()
            self.deal()
            self.evaluate()
            if input("Press 'enter' to continue.  Enter 's' to stop: ")\
                    .lower() == "s":
                return False

    def player_turn(self):
        """Conducts the players Turn.

            The value of the players hand is checked. If the value of the
            players hand is 21 with 2 cards then the plays has Blackjack.
            Otherwise the player is offered the chance to hit or stand.

            Args:
                None
            Returns:
                False: False is returned once the player cannot win.
                True: True is returned if the player has not busted and
                    still has a chance to win.
        """
        if self.value(self.player) == self.bust_val:
            print(self.print_player())
            return "Blackjack"
        while True:
            print(self.print_player())
            if input("Press 's' to stand or any other key to hit: ")\
                    .lower() == "s":
                return True
            else:
                self.deck.move_cards(self.player, 1)
            if self.value(self.player) > self.bust_val:
                print(self.print_player())
                return False
            else:
                continue

    def value(self, hand):
        """Calculates the value of a hand.

        The value of a hand is calculated by adding the value of the cards.
        where ace is worth 1 and face cards are worth 10.  An ace can be worth
        11 if it is advantages to the player.  This function aways treats aces 
        as worth 1 but 10 extra points of value are added in teh correct
        situation.

        Args:
            hand: A Hand object populated with cards.
        Returns:
            value: The value of the hand input into the function.
        """
        cards = hand.cards
        card_values = [self.val_dict[card.rank] for card in cards]
        tot_value = sum(card_values)
        if 1 in card_values and tot_value <= 11:
            tot_value += 10
        return tot_value
Пример #3
0
 def __init__(self):
     """Creates a game of Blackjack with a Shoe that is shuffled."""
     self.deck = Blackjack()
     self.deck.shuffle()