Exemple #1
0
 chips = Chips(int(chip))
 player = Player()
 dealer = Dealer()
 player.chips = 200
 while True:
     print(f'Your total Chips is {chips.total}')
     deck = Deck()
     deck.shuffle()
     player.cards = []
     dealer.cards = []
     player.value = 0
     dealer.value = 0
     bet = 0
     bet += take_bet(chips.total - bet)
     player.add_card(deck.deal())
     dealer.add_card(deck.deal())
     player.add_card(deck.deal())
     dealer.add_card(deck.deal())
     show_some(player, dealer)
     playing = True
     while playing:
         hit_or_stand(deck, player)
         show_some(player, dealer)
         if player.adjust_for_ace() > 21:
             print('Player is a Bust')
             break
     show_all(player, dealer)
     while dealer.adjust_for_ace() < 17 and player.adjust_for_ace() <= 21:
         hit(deck, dealer)
         show_all(player, dealer)
     if player.adjust_for_ace() < dealer.adjust_for_ace() <= 21 or player.adjust_for_ace() > 21:
Exemple #2
0
class Game:
    def __init__(self):
        # self.players_cards = []
        # self.dealers_cards = []

        self.player = Player()
        self.dealer = Dealer()
        self.deck = Deck()
        self.deck.build_deck()
        self.deck.shuffle()

    #This runs the game
    def game_running(self):
        in_play = True
        while in_play:
            deck = Deck()
            # If either hand has 0 cards, deal a random card from the card list
            while len(self.player.hand) < 1 and len(self.player.hand) < 1:
                player_first_card = deck.deal()
                self.player.add_card(player_first_card)
                dealer_first_card = deck.deal()
                self.dealer.add_card(dealer_first_card)
            # Show the hand and ask player to hit or stay
            print(f" Your hand is: {self.player.show_hand()}")
            # print(f"Dealer's hand is: {self.dealer.display()}")
            choice = input("Please choose [Hit / Stay] ").lower()
            while choice not in ["h", "s", "hit", "stay"]:
                choice = input(
                    "Please enter 'hit' or 'stay' (or H/S) ").lower()
            # If they hit, add card to their hand and update the score
            if choice == "hit" or choice == "h":
                #Deal a card to both player and dealer
                self.player.add_card(deck.deal())
                self.dealer.add_card(deck.deal())
                #This checks if dealer or player has blackjack
                player_blackjack, dealer_blackjack = self.check_blackjack()
                #If blackjack is True, this ends the game
                if player_blackjack or dealer_blackjack:
                    return player_blackjack, dealer_blackjack
            elif choice == "stay" or choice == "s":
                #If player has higher score, player wins
                if self.player.get_hand() > self.dealer.get_hand():
                    print("Your hand was higher, you win!")
                    print("Final Results")
                    print("Dealer's hand:", self.dealer.reveal())
                else:
                    #If dealer has higher score, dealer wins, and reveals their cards
                    print("You busted. The dealer wins")
                    print("Final Results")
                    print("Dealer's hand:", self.dealer.reveal())

    # Checks if player or dealer has reached 21 or if the player has higher hand
    def check_blackjack(self):
        player_blackjack = False
        dealer_blackjack = False
        players_hand = self.player.calculate_hand()
        dealers_hand = self.dealer.calculate_hand()
        if players_hand == 21 or dealers_hand > 21:
            player_blackjack = True
        if dealers_hand == 21 or players_hand > 21:
            dealer_blackjack = True
        return player_blackjack, dealer_blackjack

    # When game is over ask to play
    # This method returns true or false if the player wants to play again
    def is_game_over(self, player_has_blackjack, dealer_has_blackjack):
        game_over = False
        # If this returns True then the game is over
        if player_has_blackjack and dealer_has_blackjack:
            game_over = True
            print("Draw!")
        if player_has_blackjack:
            game_over = True
            print("dealer busted. you win!")
            print(self.dealer.reveal())
            print(self.player.show_hand())
        if dealer_has_blackjack:
            game_over = True
            print(self.dealer.reveal())
            print(self.player.show_hand())
            print("You busted. The dealer wins")
        if game_over == True:
            play_again = input("Would you like to play again? Y/N ").lower()
            if play_again == "n":
                print("Thanks for playing BlackJack!")
                return True
            else:
                return False
Exemple #3
0
            "{}, please place your bet. You have {} chips. Minimum bet 1, Maximum bet 500:\n"
            .format(player.name, player.purse))
        if False == player.validate_bet(bet):
            print("Please enter a valid bet")
        else:
            player.decrease_purse(bet)
            print("You bet {} chips. You have {} chips left.".format(
                bet, player.purse))
            VALID_BET = True

    player.add_card(game_deck.deal_card(player.get_total()))
    player.add_card(game_deck.deal_card(player.get_total()))
    #show the palyer's cards
    print(player)

    dealer.add_card(game_deck.deal_card(player.get_total()))
    #show the dealer's card. We'll add the second card after because we don't want to show the dealer's turned card
    print(dealer)
    #this is our 'face down' card, we won't show it until the player goes again
    dealer.add_card(game_deck.deal_card(player.get_total()))

    #we'll prompt the player to hit or stay, until they bust or decide to stay
    DEALER_TURN = False
    PLAYER_TURN = True
    while PLAYER_TURN:
        player_move = input("Would you like to hit or stay?\n")
        if player_move.lower() == "hit":
            #deal another card, check if bust, continue loop
            player.add_card(game_deck.deal_card(player.get_total()))
            if player.get_total() == 21:
                print("{}. Blackjack! You win {}".format(player, int(bet) * 3))