【问题标题】:Blackjack game: Determining the winner二十一点游戏:确定获胜者
【发布时间】:2015-04-16 12:17:00
【问题描述】:

我做了一个二十一点游戏,除了确定获胜者之外,它完全可以工作。谁能帮我解决我做错了什么?这是我运行程序时发生的情况的示例。

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

【问题讨论】:

  • 遵循逻辑。打印“You busted”后会发生什么?注意ifelif 之间的区别!

标签: python oop blackjack


【解决方案1】:
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

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

【讨论】:

  • 这行得通,但是每当我击中时,我就会一直发生这种情况。 The dealer drew a Eight of Spades . The dealer now has a total of 22 You beat the Dealer! You got lucky punk. Your total is 18, Would you like to take a hit or stay? stay The dealer drew a Queen of Clubs . The dealer now has a total of 24 You beat the Dealer! You got lucky punk. Your total is 18, Would you like to take a hit or stay? hit You drew a Seven of Clubs . You now have a total of 25 You busted! Dealer wins! Better luck next time loser.
【解决方案2】:

如果您在rules 方法中将您的print 更改为return,它将达到您的预期。您希望在一项检查为真时立即检查其他条件。

但是,在您调用主 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')

【讨论】:

  • 感谢您的帮助!还有一个问题,在我获胜后如何让比赛重播?谢谢
  • 你必须有一个外部控制循环,在游戏结束后简单地调用庄家逻辑循环。 continue_game = raw_input('Do you want to continue? '); if continue_game[0].upper() == 'Y': start_game() 之类的东西,start_game() 是您的经销商方法。
【解决方案3】:

Taesu 的答案很接近,但根本不可能通过先玩两只手然后再查看总数来正确玩二十一点,就像你在这里做的那样。手牌的玩法和手牌的结果是交织在一起的,必须放在同一个函数中,该函数按顺序执行以下所有操作:

  1. 向庄家和闲家发两张牌。
  2. 如果庄家的明牌是 A,则提供保险赌注(您可能想要 为简单起见消除这种情况)
  3. 如果正面牌是 ace 或 10,庄家偷看。如果他有天赋,游戏就是 超过。自然平局的玩家,其他人输。
  4. 支付/接受保险赌注。
  5. 庄家不是天生的,所以如果玩家有天生的,支付奖金 (3/2、6/5,随便你决定)游戏结束。
  6. 允许玩家击球:这是一个循环。如果他破产了,就跳出来 循环和游戏结束,玩家输了。你可能想离开 为了简单起见,去掉了加倍和拆分之类的东西。
  7. 玩家以站立结束循环,所以现在玩庄家的手 适当的规则。如果庄家破产,游戏结束,玩家获胜。
  8. 最后,如果双方都没有破产,那么平等的手推,更大的胜利。

【讨论】:

    猜你喜欢
    • 2018-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 2012-07-13
    • 1970-01-01
    相关资源
    最近更新 更多