2017-12-27 442 views
0

我试图使用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 
+0

get_value函数是什么?和'show_dealerHand'?等,好像有在'Hand' – noamgot

+0

类缺少一些方法@noamgot我编辑的代码,以手类 – user2899245

回答

1

注意,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,根据您的需要。

+0

所有方法谢谢你的。但在修复后,似乎问题仍然发生时,我称之为胸围,还有当我打电话给dealerHand.add_card(deck1.drawCard())。 – user2899245

+0

需要明确的是,似乎每次dealerHand.add_card(deck1.drawCard())的范围内,而dealerHand.get_value()<17,它打印经销商的手叫 – user2899245

+0

没关系。我刚刚发现我有一块调试代码,我忘了注释掉,导致它不断打印卡... * face palm * – user2899245