簡體   English   中英

二十一點游戲:確定勝利者

[英]Blackjack game: Determining the winner

我做了一個二十一點游戲,除了確定勝利者之外完全有效。 任何人都可以幫我解決我做錯的事嗎? 這是一個關於運行程序時會發生什么的示例。

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

許多邏輯問題。 我不知道我是否抓住了它們。

如果您在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')

Taesu的答案很接近,但是你不能通過先玩兩只手然后再看看總數來正確地玩二十一點牌,就像你在這里一樣。 手的游戲和手的結果交織在一起,並且必須按照以下順序放入相同的功能中,該功能執行以下所有操作:

  1. 向經銷商和玩家發放兩張牌。
  2. 如果經銷商的upcard是ace,則提供保險投注(為簡單起見,您可能希望消除這種情況)
  3. 如果upcard是ace或10,經銷商偷看。 如果他有自然,游戲就結束了。 球員有自然領帶,其他人失敗。
  4. 支付/參加保險投注。
  5. 經銷商不自然,所以如果玩家有自然,支付獎金(3 / 2,2 / 5,無論你決定了什么)游戲結束。
  6. 允許玩家擊中:這是一個循環。 如果他摔倒,突破循環並結束游戲,玩家便會失敗。 為了簡單起見,你可能想要省去加倍和分裂之類的東西。
  7. 玩家通過站立結束循環,所以現在按照適當的規則玩經銷商的手。 如果經銷商破產,游戲結束,玩家獲勝。
  8. 最后,如果既沒有垮掉,那么平等的推手,更大的勝利。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM