二十一点游戏:确定胜利者

时间:2015-02-16 02:37:35

标签: python oop blackjack

我做了一个二十一点游戏,除了确定胜利者之外完全有效。任何人都可以帮我解决我做错的事吗?这是我运行程序时会发生什么的一个例子。

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

它说我破坏了,但后来说我赢了。

另外,如果你们知道我可以清理它,请告诉我!

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()

3 个答案:

答案 0 :(得分:0)

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

许多逻辑问题。我不知道我是否抓住了它们。

答案 1 :(得分:0)

如果您在print方法中将return更改为rules,它将按照您的预期进行操作。只要一次检查成立,你就想要,而不是去检查其他条件。

但是,在主game方法中调用方法之前,您已经在进行一些检查:

if playerTotal == 21:
    rules(playerTotal, dealerTotal)

如果玩家达到21,你就不需要检查规则,因为玩家获胜;同样你有:

elif dealerTotal == 21:
    rules(playerTotal, dealerTotal)
再次,如果经销商点击21,房子赢了 - 不需要特定的支票。

您需要删除此检查,并且无论何时发卡,您都需要使用rules方法进行计算;或者很简单,继续玩游戏,直到玩家达到21或以上,或者房子(经销商)达到21或以上:

 player_total = Card.getCardValue(pCard1) + Card.getCardValue(pCard2)
 dealer_total = Card.getCardValue(dCard1) + Card.getCardValue(dCard2)

 while player_total <= 21 or dealer_total <= 21:
    # Your game logic
    player_total += Card.getCardValue(pCard1) + Card.getCardValue(pCard2)
    dealer_total += Card.getCardValue(dCard1) + Card.getCardValue(dCard2)

 if player_total == 21:
     print('You win')
 else:
     print('You lose')

答案 2 :(得分:0)

Taesu的答案很接近,但是你不可能通过先玩两只手然后再观看总数来正确地玩二十一点牌,就像你在这里一样。手的游戏和手的结果交织在一起,并且必须按顺序放入相同的功能中,以下所有功能:

  1. 向经销商和玩家发放两张牌。
  2. 如果经销商的名片是王牌,则提供保险投注(您可能需要) 为简单起见消除这种情况)
  3. 如果upcard是ace或10,经销商偷看。如果他有自然,那就是游戏 过度。自然领带的球员,其他人输了。
  4. 支付/承担保险金。
  5. 经销商不自然,所以如果玩家有自然,支付奖金 (3 / 2,2 / 5,无论你决定了什么)游戏结束。
  6. 允许玩家点击:这是一个循环。如果他破产,就会爆发 循环和游戏结束,玩家输了。你可能想离开 为了简单起见,加倍和分裂等事情。
  7. 玩家通过站立结束循环,所以现在玩经销商的手 适当的规则。如果经销商破产,游戏结束,玩家获胜。
  8. 最后,如果既没有垮掉,那么也是平等的推手,更大的胜利。