Python的酒杯OOP - 呼叫胸部和大酒杯类功能不断重印卡值

我试图使用OOP创建二十一点游戏和每一个我称之为21点或半身功能时,它重印玩家的手和值经销商。我想它与Hand类正在初始化有关,但我不确定那里的工作是什么。Python的酒杯OOP - 呼叫胸部和大酒杯类功能不断重印卡值

代码下面的手+类变量:

class Hand(object): 

def __init__(self):

self.hand = []

def add_card(self, card):

self.hand.append(card)

return self.hand

def __str__(self):

hand = [str(card) for card in self.hand]

print 'Current hand is: ',hand

def get_value(self):

value = 0

for card in self.hand:

print(card)

value += card.value

for card in self.hand:

if card.card == 'Ace' and value > 21:

value -= 10

return value

def bust(self):

if self.get_value() > 21:

print ('Busted!')

else:

pass

def hit_or_stand(self):

self.hit = 'Hit'

self.stand = 'Stand'

self.prompter = raw_input('Would you like to hit or stand? ').lower()

return self.prompter

def show_hand(self):

for card in self.hand:

print(str(card))

def show_dealerHand(self):

print('Dealers Hand: '+ str(self.hand[0]) + ' ' + '[X]')

def blackjack(self):

self.bj = False

if self.get_value() == 21:

self.bj = True

print ('Blackjack!')

else:

pass

def dealerHit(self):

if self.get_value < 17:

self.add_card(card)

else:

pass

这里是运行游戏的主要功能:

def runGame(): 

player1 = Player()

deck1 = Deck()

playerHand = Hand()

dealerHand = Hand()

# deal out cards to player

i = 0

while i < 2:

playerHand.add_card(deck1.drawCard())

dealerHand.add_card(deck1.drawCard())

i += 1

playerHand.__str__()

dealerHand.show_dealerHand()

player_move = playerHand.hit_or_stand()

if player_move == playerHand.hit.lower():

playerHand.add_card(deck1.drawCard())

while dealerHand.get_value() < 17:

dealerHand.add_card(deck1.drawCard())

if playerHand.bust():

print('Dealer wins!')

elif dealerHand.bust():

print('Player wins!')

if playerHand.blackjack() and dealerHand.blackjack():

print('Both the Player and the Dealer have a blackjack!')

player1.winnings += player1.amount

elif playerHand.get_value() > dealerHand.get_value():

print('Player wins')

player1.winnings += player1.amount * 2

elif playerHand.get_value() < dealerHand.get_value():

print('Dealer wins')

player1.winnings -= player1.amount

回答:

注意,bust函数返回什么:

def bust(self): 

if self.get_value() > 21:

print ('Busted!')

else:

pass

但是,您主要得到它的返回值:

if playerHand.bust(): 

print('Dealer wins!')

elif dealerHand.bust():

print('Player wins!')

在这种情况下,你得到的是None。那么这里发生的是playerHand.bust()返回None,其值为False。因此,elif dealerHand.bust()也正在执行,这就是为什么经销商的手也正在打印。

为了解决这个问题,你必须返回TrueFalse,根据您的需要。

以上是 Python的酒杯OOP - 呼叫胸部和大酒杯类功能不断重印卡值 的全部内容, 来源链接: utcz.com/qa/267230.html

回到顶部