Exemplo n.º 1
0
def play_blackjack(chips=Account('Chris', 500)):
    """
    Play BlackJack
    :param chips: object
    :return:
    """

    # request a name be entered if one isn't present
    while not len(chips.name):
        try:
            named = input('What\'s your name? ')

            if named.isalpha():
                chips.name = named
                break
            else:
                print(
                    f"{named} is not a valid name, please enter a valid name")
                continue

        except:
            print(
                'Sorry something went wrong, please try and input your name again'
            )
            continue

    # Add fund to account if none is present
    if not chips.has_funds():
        chips.add_funds()
    # Add bet if none has been placed
    while not chips.has_bet():
        chips.make_bet()

    # Build deck and shuffle
    deck = Deck()
    deck.shuffle()

    deal_count = 1
    player = Hand()
    dealer = Hand()
    player.cards = []
    dealer.cards = []
    # Start first phase of the dealing
    while deal_count <= 4:
        deal_count += 1
        if deal_count % 2 == 0:
            if deal_count == 4:
                the_fourth_card = deck.deal()
                the_fourth_card.hide_card()
                dealer.add_card(the_fourth_card)
            else:
                dealer.add_card(deck.deal())

        else:
            player.add_card(deck.deal())

    # Second phase of the dealing
    while True:
        # display cards
        clear()
        print(chips.name + ': ')
        print(player)
        print('Dealers: ')
        print(dealer)

        if player.bust():
            break
        if player.next_move():
            player.add_card(deck.deal())
            continue
        else:
            break

    # change view permissions
    for card in dealer.cards:
        card.show_card()

    # Once player finishes turn continue to deal dealers hand
    while True:
        # display cards
        clear()
        print(chips.name + ': ')
        print(player)
        print('Dealers: ')
        print(dealer)

        if dealer.bust():
            break
        if player.bust():
            break
        if dealer.total() > player.total():
            break
        else:
            dealer.add_card(deck.deal())
            continue

    # display results and finalise transaction
    if player.bust():
        clear()
        chips.subtract()
        print(
            f"{chips.name} you lost this one\n\nYour new Balance {chips.balance()}\n"
            f"{chips.name}: {player}"
            f"Dealers: {dealer}"
            f"\nYour Score {player.total()}\nDealers Score {dealer.total()}")

    elif player.total() < dealer.total() and not dealer.bust():
        clear()
        chips.subtract()
        print(
            f"{chips.name} the dealer beat your score\n\nYour new Balance {chips.balance()}\n"
            f"{chips.name}: {player}"
            f"Dealers: {dealer}"
            f"\nYour Score {player.total()}\nDealers Score {dealer.total()}")

    else:
        clear()
        chips.add()
        print(f"{chips.name} you won!\n\nYour new Balance {chips.balance()}\n"
              f"{chips.name}: {player}"
              f"Dealers: {dealer}"
              f"\nYour Score {player.total()}\nDealers Score {dealer.total()}")

    # Do they want to replay
    while True:
        try:
            replay = input('Do you want to play again? ').upper()

            if replay == 'YES':
                play_blackjack()
                break
            elif replay == 'NO':
                print(
                    f'{chips.name} thank you for playing and we look forward to seeing you again'
                )
                break
            else:
                print(
                    'We didn\'t understand your request, a YES or NO is desired'
                )
                continue
        except ValueError:
            print(
                'Invalid Input: We didn\'t recognise your request please try again YES or No'
            )
            continue
Exemplo n.º 2
0
def main():
    player = Player(1000)
    while player.money > 0:
        print('Your money: ${}'.format(player.money))
        while True:
            bet_string = input('Your bet: ')
            try:
                bet = int(bet_string)
            except ValueError:
                print('bed value')
                continue
            if bet > player.money:
                print('Insufficient money')
                continue
            break

        deck = shuffle_deck()
        player.cards = Hand([])
        dealer_cards = Hand([])
        for i in range(2):
            player.cards.add_card(deck.pop())
        dealer_cards.add_card(deck.pop())
        display_table(dealer_cards, player.cards)
        while True:
            while True:
                action = input('Do you (h)it, (s)tand or (d)ouble down?')
                if action not in ['h', 's', 'd']:
                    continue
                break
            if action == 'h':
                player.cards.add_card(deck.pop())
                display_table(dealer_cards, player.cards)
                if player.cards.bust:
                    break
            elif action == 'd':
                if bet <= player.money // 2:
                    bet *= 2
                    player.cards.add_card(deck.pop())
                    break
                else:
                    print('Insufficient money')
                    continue
            elif action == 's':
                break
        if player.cards.bust:
            print('You bust!')
            player.money -= bet
            continue
        while (
            dealer_cards.value < 16 or
            dealer_cards.value == 17 and dealer_cards.soft
        ):
            dealer_cards.add_card(deck.pop())
        display_table(dealer_cards, player.cards)
        if player.cards.blackjack:
            print('Blackjack')
            player.money += round(bet * 1.5)
        elif dealer_cards.bust:
            print('Dealer busts')
            player.money += bet
        elif dealer_cards.value == player.cards.value:
            print('Push')
        elif dealer_cards.value > player.cards.value:
            print('Dealer wins')
            player.money -= bet
        else:
            print('Player wins')
            player.money += bet
    print("You're bankrupt")
Exemplo n.º 3
0
class State:
    def __init__(self,
                 starting_chips=1000,
                 number_decks=6,
                 percent_to_use=0.8):
        self.player_chips = starting_chips
        self.shoe = Shoe(number_decks=number_decks,
                         percent_to_use=percent_to_use)
        self.player_hand = Hand()
        self.dealer_hand = Hand()

    def deal(self, bet):
        if bet > self.player_chips:
            raise Exception(f'Insufficient chips to place bet of {bet} chips.')
        self.player_hand = Hand(bet=bet)
        self.dealer_hand = Hand()
        self.player_hand.add_card(self.shoe.deal_card())
        self.dealer_hand.add_card(self.shoe.deal_card())
        self.player_hand.add_card(self.shoe.deal_card())
        self.dealer_hand.add_card(self.shoe.deal_card())

    def inspect_dealers_hand(self):
        return self.dealer_hand.inspect_card()

    def hit(self):
        self.player_hand.add_card(self.shoe.deal_card())

    def dealer_play(self):
        print(f'Dealer shows {self.dealer_hand.get_cards()}')
        while self.dealer_hand.get_value() <= 17:
            # Dealer hits soft 17
            if self.dealer_hand.get_value() == 17 \
                    and len(self.dealer_hand.get_cards()) == 2 \
                    and 'A' in self.dealer_hand.get_cards():
                break
            self.dealer_hand.add_card(self.shoe.deal_card())
        print(
            f'Dealer ends up with {self.dealer_hand.get_cards()}. (Total={self.dealer_hand.get_value()})'
        )

    def play(self, bet):
        if not self.shoe.is_shoe_open():
            self.shoe.reset_shoe()
            print('Shuffling shoe.')
        self.deal(bet)
        dealer_card = self.inspect_dealers_hand()
        action = 'bj' if self.player_hand.get_value() == 21 else None
        while action not in ['stand', 'bust', 'bj']:
            print(f'Dealer is showing: {dealer_card}.')
            player_cards = self.player_hand.get_cards()
            print(
                f'You have: {player_cards}. (Total={self.player_hand.get_value()})'
            )
            action = input('Hit or Stand?')
            if action == 'hit':
                self.hit()
            if self.player_hand.get_value() > 21:
                action = 'bust'
        if action != 'bust':
            self.dealer_play()

        # Check result
        if action == 'bust':
            print('You busted!')
            chip_diff = self.player_hand.bet * -1
        elif action == 'bj':
            print('Blackjack!')
            chip_diff = self.player_hand.bet * 1.5
        else:
            result = self.player_hand.check_hand(self.dealer_hand)
            if result > 0:
                print('You won!')
                chip_diff = self.player_hand.bet
            elif result == 0:
                print('Push.')
                chip_diff = 0
            else:
                print('You lost.')
                chip_diff = self.player_hand.bet * -1

        self.player_chips += chip_diff
        return chip_diff
Exemplo n.º 4
0
def create_hand(cards):
    hand = Hand()
    for card in cards:
        hand.add_card(card)
    return hand