當前位置: 首頁>>代碼示例>>Python>>正文


Python Deck.reset方法代碼示例

本文整理匯總了Python中Deck.reset方法的典型用法代碼示例。如果您正苦於以下問題:Python Deck.reset方法的具體用法?Python Deck.reset怎麽用?Python Deck.reset使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Deck的用法示例。


在下文中一共展示了Deck.reset方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: BlackJack

# 需要導入模塊: import Deck [as 別名]
# 或者: from Deck import reset [as 別名]
class BlackJack(object):
    '''
    classdocs
    '''

    def __init__(self, playerName):
        '''
        Initializes the game by
        - shuffling the deck
        - initializing a player1 (with given playerName) and dealer object (both are Player objects)
        '''
        self.gamedeck = Deck()
        self.gamedeck.reset()
        self.gamedeck.shuffle()
        self.playerName = playerName

        self.user = Player(self.playerName)
        self.dealer = Player("Dealer")


    def getWinner(self):
        '''
        returns the player object (either the class's player1 or dealer object) that is the winner
        based on their hands (closest to 21 without going over)

        return - Player object
        '''
        print ""
        print "WINNER:"

        #if the you got 21
        if self.user.getBJScore() == 21 and self.dealer.getBJScore() != 21:
            return self.user

        #if the dealer got 21
        if self.dealer.getBJScore() == 21 and self.user.getBJScore() != 21:
            return self.dealer

        #if both players got the same score
        if self.user.getBJScore() == self.dealer.getBJScore():
            return "No one Won. Both players got the same score!"

        #if dealer busted
        if self.dealer.getBJScore() > 21 and self.user.getBJScore() < 21:
            return self.user

        #if you busted
        elif self.dealer.getBJScore() < 21 and self.user.getBJScore() > 21:
            return self.dealer

        #if both players were under 21
        if self.user.getBJScore() < 21 and self.dealer.getBJScore() < 21:
            if self.user.getBJScore() > self.dealer.getBJScore():
                return self.user
            else:
                return self.dealer

        #if both player busted
        if self.user.getBJScore() > 21 and self.dealer.getBJScore() > 21:
            return "No one won. Both players busted!"

    def play(self):

        '''
        play the game using the class's deck, player1, and dealer objects
        '''

        '''
        initialize player1 and dealer hands (2 cards each)
        show player 1 hand
        player1 decision (hit or stand)
        dealer complete hand
        present game outcome - player1 win or lose
        '''


        """player logic"""
        #draw 2 cards for the dealer and the user
        for i in range(2):
            self.user.addToHand(self.gamedeck.takeCard())
        for i in range(2):
            self.dealer.addToHand(self.gamedeck.takeCard())

        print "{0}".format(self.user.nameStr)
        print "-"*30

        #print the user's hand
        for i in range(len(self.user.hand)):
            print self.user.hand[i]


        print "Your current score is {0}".format(self.user.getBJScore())

        #ask the user if they want to hit or stand
        hit_or_stand = raw_input("Would you like to hit or stand? ")
        hit_or_stand = hit_or_stand.lower()

        #error handling - make sure they actually put in "hit" or "stand"
        while hit_or_stand != "hit" and hit_or_stand != "stand":
            hit_or_stand = raw_input("Would you like to hit or stand? ")
#.........這裏部分代碼省略.........
開發者ID:James-Harris14,項目名稱:Blackjack-Game,代碼行數:103,代碼來源:BlackJack.py


注:本文中的Deck.reset方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。