本文整理汇总了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? ")
#.........这里部分代码省略.........