Exemple #1
0
def compare_hands(players_hand, dealers_hand):
    player_value = cards.get_points(players_hand)
    dealer_value = cards.get_points(dealers_hand)
    print("YOUR POINTS:     ", player_value)
    print("DEALER'S POINTS: ", dealer_value)
    print()
    if player_value > 21:
        print(" Oh no! You busted, you lose!")
        return "lose"
    elif player_value == 21 and len(players_hand) == 2:
        if dealer_value == 21 and len(dealers_hand) == 2:
            print("Dang, dealer got blackjack too. You push.")
            return "push"
        else:
            print("Blackjack! You win!")
            return "blackjack"
    elif dealer_value > 21:
        print("Yay! Dealer busted, you win!")
        return "win"
    elif player_value > dealer_value:
        print(" Yay. You win.")
        return "win"
    elif dealer_value > player_value:
        print("Sorry, you lose")
        return "lose"
    elif dealer_value == player_value:
        print("Push")
        return "push"
    else:
        print("Sorry, I don't know what happened")
Exemple #2
0
def play(deck, player_hand, money, bet):
    while True:
        can_double_down = False
        if (money >= bet * 2) and (len(player_hand) == 2):
            can_double_down = True

        if can_double_down:
            msg = "Hit or Stand or Double Down? (h/s/d): "
        else:
            msg = "Hit or Stand? (h/s): "
        choice = input(msg)
        print()

        if choice.lower() == "h":
            cards.add_card(player_hand, cards.deal_card(deck))
            if cards.get_points(player_hand) > 21:
                break
            display_card(player_hand, "YOUR CARDS: ")
        elif choice.lower() == "d" and can_double_down:
            cards.add_card(player_hand, cards.deal_card(deck))
            display_card(player_hand, "YOUR CARDS: ")
            bet *= 2
            break
        elif choice.lower() == "d":
            print("Insufficient funds to do that.")
        elif choice.lower() == "s":
            break
        else:
            print("Not a valid choice, Try again.")
    return player_hand, bet
Exemple #3
0
def play(deck, player_hand):
    while True:
        choice = input("Hit or stand? (hit/stand): ")
        print()

        if choice == "hit":
            cards.add_card(player_hand, cards.deal_card(deck))
            display_cards(player_hand, "YOUR CARDS: ")
            if cards.get_points(player_hand) > 21:
                break
        elif choice == "stand":
            break
        else:
            print("Not a valid choice. Try again.")
    return player_hand
Exemple #4
0
def main():
    start_time = datetime.now()
    display_title(start_time)

    # Input money & bet from user
    money = get_starting_money()
    print("PLayer's money:", lc.currency(money, grouping=True))
    print()
    while True:
        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("Player's money: ", lc.currency(money, grouping=True))
                print()
                db.write_money(money)
            else:
                break
        bet = get_bet(money)
        if bet == "x":
            break
        deck = cards.get_deck()
        cards.shuffle(deck)
        players_hand = cards.get_empty_hand()
        dealers_hand = cards.get_empty_hand()
        cards.add_card(players_hand, cards.deal_card(deck))
        cards.add_card(dealers_hand, cards.deal_card(deck))
        cards.add_card(players_hand, cards.deal_card(deck))
        print()
        display_card(dealers_hand, "Dealer's SHOW CARD: ")
        display_card(players_hand, "YOUR CARDs: ")
        players_hand, bet = play(deck, players_hand, money, bet)
        while True:
            if cards.get_points(dealers_hand) >= 17:
                break
            else:
                cards.add_card(dealers_hand, cards.deal_card(deck))
        if cards.get_points(players_hand) > 21:
            display_card(dealers_hand, "Dealer's CARDs: ")
        else:
            display_card(dealers_hand, "Dealer's CARDs: ")
        result = compare_hands(players_hand, dealers_hand)

        if result.lower() == "blackjack":
            print("You got blackjack!")
            money += round(int(bet) * 1.5, 2)
        elif result.lower() == "win":
            #print("You won!")
            money += float(bet)
        elif result.lower() == "push":
            #print("You pushed.")
            money = money
        elif result.lower() == "lose":
            #print(" You lost.")
            money -= float(bet)
        # Printing out the money
        print("Player's Money: ", lc.currency(money, grouping=True))
        print()
        db.write_money(money)
        again = input("Play again? (y/n): ")
        if again.lower() != "y":
            print()
            print("Come again soon!")
            break

    stop_time = datetime.now()
    display_end(start_time, stop_time)
Exemple #5
0
def main():
    display_title()
    start_time = datetime.now()
    print("Start time:  ", start_time.strftime("%I:%M:%S"))
    print()

    # 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 = cards.get_deck()
        cards.shuffle(deck)

        dealer_hand = cards.get_empty_hand()
        player_hand = cards.get_empty_hand()

        # deal two cards to player and one to the dealer
        cards.add_card(player_hand, cards.deal_card(deck))
        cards.add_card(player_hand, cards.deal_card(deck))
        cards.add_card(dealer_hand, cards.deal_card(deck))

        # 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 cards.get_points(dealer_hand) < 17:
            cards.add_card(dealer_hand, cards.deal_card(deck))
        display_cards(dealer_hand, "DEALER'S CARDS: ")

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

        # check result of hand
        player_points = cards.get_points(player_hand)
        dealer_points = cards.get_points(dealer_hand)
        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

    end_time = datetime.now()
    print("End time:  ", end_time.strftime("%I:%M:%S"))
    elapsed_time = end_time - start_time
    days = elapsed_time.days
    minutes = elapsed_time.seconds // 60
    seconds = elapsed_time.seconds % 60
    hours = minutes // 60
    minutes = minutes % 60
    time_object = time(hours, minutes, seconds)
    print("Elapsed time:  ", time_object)
    print("Come back soon!")
    print("Bye!")