Search code examples
pythonoopblackjack

Blackjack game: Determining the winner


I made a blackjack game which works entirely except for determining the winner. Could anyone help me on what I am doing wrong? Here's an example on what happens when I run the program.

Cooper 's cards are : Five of Spades and King of Hearts
The Dealer's cards are: Eight of Diamonds and Ten of Spades
Your total is 15, Would you like to take a hit or stay? hit
You drew a Eight of Hearts .
You now have a total of 23
You busted!
You beat the Dealer! You got lucky punk.

Process finished with exit code 0

It says I busted, but then say I won.

Also if you guys know anyway I could clean this up let me know!

from Deck import Deck
from Card import Card
from Hand import Hand
import sys

deck = Deck()
deck.shuffle()

def rules(playerTotal, dealerTotal):
    if int(playerTotal) > 21:
        print "You busted!"
        if int(dealerTotal) == 21:
            print 'To make it worse, dealer has 21.'
    if int(dealerTotal) > 21:
        print "The Dealer has busted. You win!"
    if int(playerTotal) == 21:
        print " You got 21! So you win!"
        if int(dealerTotal) == 21:
            print "The Dealer also got 21. Tough Break."
    elif int(dealerTotal) == 21:
        print "The Dealer got 21! Tough Break, you lose!"
    else:
        if int(playerTotal) > int(dealerTotal):
            print "You beat the Dealer! You got lucky punk."
        elif int(playerTotal) == int(dealerTotal):
            print "It is a push, no one wins!"
        elif int(playerTotal) < int(dealerTotal):
            print "Dealer wins! Better luck next time loser."

def game():

    player = raw_input("What is your name? ")
    print "        /////////////////////////////////////////\n" \
          "       /////////////////////////////////////////\n" \
          "      ///////// LET'S PLAY BLACKJACK //////////\n" \
          "     /////////////////////////////////////////\n" \
          "    /////////////////////////////////////////\n"
    pCard1 = deck.deal()
    pCard2 = deck.deal()
    print player, "'s cards are :", pCard1, "and", pCard2
    dCard1 = deck.deal()
    dCard2 = deck.deal()
    print "The Dealer's cards are:", dCard1, "and", dCard2
    playerTotal = (Card.getCardValue(pCard1) + Card.getCardValue(pCard2))
    dealerTotal = (Card.getCardValue(dCard1) + Card.getCardValue(dCard2))
    if playerTotal == 21:
        rules(playerTotal,dealerTotal)
    elif dealerTotal == 21:
        rules(playerTotal,dealerTotal)
    else:
        option = raw_input("Your total is " + str(playerTotal) + ", Would you like to take a hit or stay? ")
        if option == "Hit" or option == "hit":
            pCard = deck.deal()
            playerTotal = playerTotal + (Card.getCardValue(pCard))
            print "You drew a", pCard, "."
            print "You now have a total of", playerTotal
            while playerTotal < 21:
                option = raw_input("Your total is " + str(playerTotal) + ", Would you like to take a hit or stay? ")
                if option == "stay" or option == "Stay":
                    dealersTurn(dealerTotal)
                    dealerHit(playerTotal,dealerTotal)
                    rules(playerTotal, dealerTotal)
                else:
                    pCard = deck.deal()
                    playerTotal = playerTotal + (Card.getCardValue(pCard))
                    print "You drew a", pCard, "."
                    print "You now have a total of", playerTotal
            rules(playerTotal, dealerTotal)
            sys.exit
        if option == "stay" or option == "Stay":
            dealersTurn(dealerTotal)
            dealerHit(playerTotal,dealerTotal)
            rules(playerTotal, dealerTotal)



def dealerHit(playerTotal,dealerTotal):
    while dealerTotal < playerTotal:
        dcard = deck.deal()
        dealerTotal = dealerTotal + (Card.getCardValue(dcard))
        print "The dealer drew a", dcard, "."
        print "The dealer now has a total of", dealerTotal
    return dealerTotal



def dealersTurn(dealerTotal):
    while dealerTotal < 17:
        dcard = deck.deal()
        dealerTotal = dealerTotal + (Card.getCardValue(dcard))
    return dealerTotal




game()

Solution

  • def rules(playerTotal, dealerTotal):
        if int(playerTotal) > 21:
            print "You busted!"
            if int(dealerTotal) == 21:
                print 'To make it worse, dealer has 21.'
            print "Dealer wins! Better luck next time loser."
            return
    
        if int(dealerTotal) > 21:
            print "The Dealer has busted. You win!"
            return
    
        if int(playerTotal) == 21:
            print " You got 21! So you win!"
            if int(dealerTotal) == 21:
                print "The Dealer also got 21. Tough Break."
            return
        elif int(dealerTotal) == 21:
            print "The Dealer got 21! Tough Break, you lose!"
            return
        else:
            if int(playerTotal) > int(dealerTotal):
                print "You beat the Dealer! You got lucky punk."
            elif int(playerTotal) == int(dealerTotal):
                print "It is a push, no one wins!"
            elif int(playerTotal) < int(dealerTotal):
                print "Dealer wins! Better luck next time loser."
        return
    

    Many logic problems. I don't know if I caught them all.