当前位置: 首页>>代码示例>>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;未经允许,请勿转载。