2017-12-27 503 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