def main():
    print("Cards - Tester")
    print()

    #test sorting of the cards
    testcardsList = [
        Card("Spades", "Ace"),
        Card("Hearts", "Queen"),
        Card("Clubs", "10"),
        Card("Diamonds", "3"),
        Card("Hearts", "Jack"),
        Card("Spades", "7")
    ]
    testcardsList.sort()
    print("TEST CARDS LIST AFTER SORTING.")
    for c in testcardsList:
        print(c)
    print()

    # test deck
    print("DECK")
    deck = Deck()
    print("Deck created.")
    deck.shuffle()
    print("Deck shuffled.")
    print("Deck count:", len(deck))
    print()

    # test hand
    hand = Hand()
    for i in range(10):
        hand.addCard(deck.dealCard())

    print("SORTED HAND")
    for c in sorted(hand):
        print(c)

    print()
    print("Hand points:", hand.points)
    print("Hand count:", len(hand))
    print("Deck count:", len(deck))
class Blackjack:
    def __init__(self, startingBalance):
        self.money = startingBalance
        self.deck = Deck()
        self.playerHand = Hand()
        self.dealerHand = Hand()
        self.bet = 0

    def displayCards(self, hand, title):
        ''' Print the title and display the cards in the given hand in a sorted order'''
        print(title.upper())
        for c in sorted(hand):
            print(c)
        print()

    def handle_winner(self, winner, message):
        ''' print the player's hand's points
        print the message
        Update self.money according to the winner
        '''
        print("Your Points: ", self.playerHand.points)
        print("Dealer Points: ", self.dealerHand.points)
        print(message)
        if winner == "player":
            print("foo", self.money, self.bet)
            self.money += self.bet
        elif winner == "dealer":
            self.money -= self.bet
        elif winner == "tie":
            pass
        elif winner == "blackjack":
            self.money += (1.5 * self.bet)

    def getBet(self):
        ''' Method to update self.bet by prompting the user for the bet amount,
        making sure bet is less than self.money.
        '''
        while True:
            try:
                bet = int(input("What is your bet:  "))
                if bet <= self.money and bet > 0:
                    self.bet = bet
                    return False
                elif bet > self.money:
                    print("You don't have that much money")
            except ValueError:
                print("You must enter a positive number!")

    def setupRound(self):
        ''' Setup the round by doing these steps:
        Call getBet to initialize self.bet,
        initialize self.deck to a new Deck object and shuffle it
        initialize self.dealerHand and self.playerHand to new Hand objects
        deal two cards to the playerHand, and one card to the dealerHand
        finally, print dealerHand and playerHand using displayCards method
        '''
        self.getBet()
        self.deck = Deck()
        self.deck.shuffle()
        self.dealerHand = Hand()
        self.playerHand = Hand()
        self.dealerHand.addCard(self.deck.dealCard())
        self.playerHand.addCard(self.deck.dealCard())
        self.playerHand.addCard(self.deck.dealCard())
        Blackjack.displayCards(self, self.dealerHand, "Dealer")
        Blackjack.displayCards(self, self.playerHand, "Player")

    def play_playerHand(self):
        ''' Method to implement player playing his hand by
        1. Prompting the user to indicate Hit (h) or Stand (s)
        2. If user picks stand, end the player play by returning
        3. If user picks hit,
            deal a card to the playerHand.
            check if with the latest addition, the hand busts (has > 21 points), if so return
            otherwise, prompt the player again whether to hit or stand.
        4. Print playerHand points
        '''
        while True:
            pick = input('\nPress "h" to hit or "s" to stand:  ')
            if pick.lower() == "h":
                self.playerHand.addCard(self.deck.dealCard())
                Blackjack.displayCards(self, self.playerHand, "Player Hand")

                if self.playerHand.points > 21:
                    return False
                else:
                    continue
            elif pick.lower() == "s":
                return False
            else:
                print("Not a valid command! Please try again.")

    def play_dealerHand(self):
        ''' Method to play the dealer's hand.
        Continue to deal cards till the points of the dealerHand are
        less than 17. Print the dealer's hand before returning'''
        dealerHandValue = self.dealerHand.points
        while dealerHandValue < 17:
            self.dealerHand.addCard(self.deck.dealCard())
            dealerHandValue = self.dealerHand.points
        Blackjack.displayCards(self, self.dealerHand, "Dealer")

    def playOneRound(self):
        ''' Method implements playing one round of the game
        1. Checks if playerHand is a Blackjack, if so handles that case
        2. Lets player play his hand if it busts, declares player loser
        3. Else lets dealer play his hand.
        4. If dealer busts, declares the player to be the winner
        5. Otherwise declares the winner based on who has higher points:
            if Player > dealer, player is the winner
            else if player < dealer, dealer is the winner
            else it is a tie        
        '''
        playerHandValue = self.playerHand.points
        if playerHandValue == 21:
            Blackjack.handle_winner(self, "blackjack", "You got Blackjack!")

        else:
            Blackjack.play_playerHand(self)
            playerHandValue = self.playerHand.points
            if playerHandValue > 21:
                Blackjack.handle_winner(self, "dealer", "You went bust!")

            else:
                Blackjack.play_dealerHand(self)
                dealerHandValue = self.dealerHand.points
                if dealerHandValue > 21:
                    Blackjack.handle_winner(self, "player",
                                            "Dealer went bust, you win!")

                elif playerHandValue > dealerHandValue:
                    Blackjack.handle_winner(self, "player", "You win!")

                elif playerHandValue < dealerHandValue:
                    Blackjack.handle_winner(self, "dealer", "You lose!")

                elif playerHandValue == dealerHandValue:
                    Blackjack.handle_winner(self, "tie", "It's a tie!")
Example #3
0
def main():
    start_time = datetime.now()
    display_title(start_time)

    # get starting money
    money = get_starting_money()
    print("Money:", lc.currency(money, grouping=True))

    # start loop
    while True:
        # make sure player has enough money for min bet
        if money < 5:
            print("You are out of money.")
            buy_more = input(
                "Would you like to buy more chips? (y/n): ").lower()
            if buy_more == "y":
                money = buy_more_chips(money)
                print("Money:", lc.currency(money, grouping=True))
                db.write_money(money)
            else:
                break

        # get bet amount
        bet = get_bet(money)
        print()

        deck = Deck()
        deck.shuffle()

        dealer_hand = Hand()
        player_hand = Hand()

        # deal two cards to player and one to the dealer
        player_hand.addCard(deck.dealCard())
        player_hand.addCard(deck.dealCard())
        dealer_hand.addCard(deck.dealCard())

        # display cards
        display_cards(dealer_hand, "DEALER'S SHOW CARD: ")
        display_cards(player_hand, "YOUR CARDS: ")

        # get player hand
        player_hand = play(deck, player_hand)

        # get dealer hand
        while dealer_hand.points() < 17:
            dealer_hand.addCard(deck.dealCard())
        display_cards(dealer_hand, "DEALER'S CARDS: ")

        print("YOUR POINTS:     " + str(player_hand.points()))
        print("DEALER'S POINTS: " + str(dealer_hand.points()))
        print()

        # check result of hand
        player_points = player_hand.points()
        dealer_points = dealer_hand.points()
        if player_points > 21:
            print("Sorry. You busted. You lose.")
            money -= bet
        elif dealer_points > 21:
            print("Yay! The dealer busted. You win!")
            money += bet
        else:
            if player_points == 21 and len(player_hand) == 2:
                if dealer_points == 21 and len(dealer_hand) == 2:
                    print("Dang, dealer has blackjack too. You push.")
                else:
                    print("Blackjack! You win!")
                    money += bet * 1.5
            elif player_points > dealer_points:
                print("Hooray! You win!")
                money += bet
            elif player_points == dealer_points:
                print("You push.")
            elif player_points < dealer_points:
                print("Sorry. You lose.")
                money -= bet
            else:
                print("I am not sure what happened.")

        # display new money amount
        money = round(money, 2)
        print("Money:", lc.currency(money, grouping=True))
        print()

        # write money to file
        db.write_money(money)

        again = input("Play again? (y/n): ").lower()
        print()
        if again != "y":
            break

    # get stop time
    stop_time = datetime.now()
    display_end(start_time, stop_time)