2014-03-02 39 views
-2

我們正在製作這個黑傑克程序來測試卡片計數方法。我們試圖讓自動播放功能正常工作,但是當我們在while循環中運行它時,循環永遠不會結束並退出。爲什麼主while循環不能執行?它只是掛在某個地方

""" 
Eli Byers 
Josh Rondash 

Black_Jack.py 
""" 
import random 

#---------- CLASSES ----------------------------------------------------------- 
class Card(object): 
    def __init__(self, rank, suit): 
     self.rank = rank 
     self.suit = suit 
     self.value = 0 
     if self.rank is "Ace": 
      self.value = 11 
     if self.rank.isdigit(): 
      self.value = int(self.rank) 
     if self.rank in ["Jack", "Queen", "King"]: 
      self.value = 10 
    def __str__(self): 
     return "["+str(self.rank)+" "+str(self.suit)+"]" 

class Deck(object): 
    def __init__(self, numofdecks): 
     self.deck = [] 
     self.suit = [" Clubs", " Hearts", " Spades", " Diamonds"] 
     self.rank = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"] 
     self.numofdecks = numofdecks 
     for i in range(self.numofdecks): 
      for r in self.rank: 
       for s in self.suit: 
        self.deck.append(Card(r,s)) 
    def __str__(self): 
     deck_str = "" 
     for card in self.deck: 
      deck_str += str(card)+" " 
     deck_str = deck_str[:-1] 
     return deck_str 
    def __len__(self): 
     return len(self.deck) 
    def __getitem__(self,i): 
     return self.deck[i] 
    def __delitem__(self, i): 
     del self.deck[i]       
    def draw(self): 
      top_card = self.deck[0] 
      del self.deck[0] 
      return top_card 
    def addcard(self,card): 
     self.deck.append(card) 
    def shuffle(self):    #Random shuffle function 
     a = len(self.deck) 
     b = a-1 
     for d in range(b,0,-1): 
      e = random.randint(0,d) 
      if e == d: 
       continue 
      self.deck[d],self.deck[e] = self.deck[e],self.deck[d] 
     return self.deck 

class Player(object): 
    def __init__(self, bankroll): 
     self.hand = [] 
     self.bankroll = bankroll 
     self.score = 0 
     self.bet = 0 
     self.count = 0 
     self.aces = 0 
     self.dealer_hand = [] 
    def __str__(self): 
     hand = "" 
     for card in self.hand: 
      hand += str(card)+" " 
     return "Hand: "+hand+" Bank: "+str(self.bankroll)+" Bet: "+str(self.bet)+" Ct: "+str(self.count)+" A: "+str(self.aces) 
    def __getitem__(self, i): 
     return self.hand[i] 
    def getcard(self,Card): 
     self.hand.append(Card) 
     self.score = 0 
     ace = 0 
     for card in self.hand: 
      if card.rank == "Ace": 
       ace += 1 
       self.score += 1 
      else: 
       self.score += card.value 
     for a in range(ace):  
      if (self.score + 10) <= 21: 
       self.score += 10 
     self.updateCount(Card,"P") 
    def placebet(self, b=0): 
     if b != 0: 
      self.bankroll -= b 
      self.bet += b 
     else: 
      self.bet += input("Bankroll: "+str(self.bankroll)+" Ct: "+str(self.count)+" A: "+str(self.aces)+" Place bet: ") 
      self.bankroll -= self.bet 
    def updateCount(self, card, player): 
     if card.value in range(2,6): 
      self.count += 1 
     elif card.value is 10: 
      self.count -= 1 
     elif card.rank is "Ace": 
      self.aces += 1 
     if player == "D": 
      self.dealer_hand.append(card) 
    def makeBet(self): 
     bet = 0.1*self.bankroll 
     if self.count > 3: 
      c = 0 
      for i in range(self.count): 
       c += 1 
       if c == 3: 
        bet += 0.5 * bet 
        c = 0 
     elif self.count < -3: 
      bet -= 0.5 * bet 
     return bet 
    def Play(self): 
     if self.score < 17: 
      choice = 1  #hit 
     else: 
      choice = 2  #stand 
     return choice 


class Dealer(object): 
    def __init__(self, Deck, discardpile, Player): 
     self.deck = Deck 
     self.discardpile = discardpile 
     self.player = Player 
     self.hand = [] 
     self.score = 0 
    def __str__(self): 
     hand = "" 
     for card in self.hand: 
      hand += str(card)+" " 
     return "Dealer Hand: "+hand 
    def __getitem__(self, i): 
     return self.deck[i] 
    def draw(self): 
     cardval = self.deck.draw() 
     self.hand.append(cardval) 
     self.score = 0 
     ace = 0 
     for card in self.hand: 
      if card.rank == "Ace": 
       ace += 1 
       self.score += 1 
      else: 
       self.score += card.value 
     for a in range(ace):  
      if (self.score + 10) <= 21: 
       self.score += 10 
     player.updateCount(cardval,"D") 
    def deal(self, Player): 
     for i in range(2): 
      self.player.getcard(self.deck.draw()) 
      self.draw() 
    def burn(self): 
     self.discardpile.addcard(self.deck.draw()) 
    def blackjack(self): 
     if self.score == 21: 
      return True 
     else: 
      return False 


class Table(Dealer, Player): 
    def __init__(self, Dealer, Player, Deck , discardpile): 
     self.dealer = Dealer 
     self.player = Player 
     self.deck = Deck 
     self.discardpile = discardpile 
     self.betplaced = 0 
    def initGame(self): 
     self.clearTable() 
     Deck.shuffle(self.deck) 
     self.dealer.burn() 
    def clearTable(self): 
     for card in self.player.hand: 
      self.discardpile.addcard(card) 
     for card in self.dealer.hand: 
      self.discardpile.addcard(card) 
     self.player.hand = [] 
     self.dealer.hand = []  
    def playGame(self): 
     self.betplaced = self.player.placebet() 
     self.dealer.deal(self.player) 
     print self.player 
     print self.dealer 
     if self.dealer.blackjack(): 
      print("Dealer Black Jack!") 
     elif self.player.score <= 21: 
      stand = 0 
      while self.player.score < 21 and stand == 0: 
       print("Use number Keys> Hit: 1  Stand: 2") 
       choice = input() 
       if choice == 1:  # Hit 
        self.player.getcard(self.deck.draw()) 
       elif choice == 2:  # Stand 
        stand = 1 
       print self.player 
       print ("Your score is "+str(self.player.score)) 
      while self.dealer.score <= 17 and self.player.score <= 21: 
       if self.dealer.score == 17: 
        for card in self.dealer.hand: 
         if card.rank == "Ace": 
          self.dealer.draw() 
       else: 
        self.dealer.draw() 
     print self.dealer 
     print ("Dealer score is "+str(self.dealer.score)) 
     if self.dealer.score <= 21: 
      if (self.player.score > self.dealer.score) and (self.player.score <= 21) : 
       if self.player.score == 21: 
        self.player.bankroll += self.player.bet*2.5 
       else: 
        self.player.bankroll += self.player.bet*2 
       print ("Win") 
      elif self.player.score == self.dealer.score: 
       self.player.bankroll += self.player.bet 
       print("Push") 
      else: 
       print("You Lose") 
     elif (self.dealer.score > 21) and (self.player.score <= 21): 
      if self.player.score == 21: 
       self.player.bankroll += self.player.bet*2.5 
      else: 
       self.player.bankroll += self.player.bet*2 
      print ("Win") 
     else: 
      print("You Lose.") 
     self.player.bet = 0 
     self.player.dealer_hand = [] 
     print 
    def autoPlay(self): 
     self.betplaced = self.player.placebet(int(self.player.makeBet())) 
     self.dealer.deal(self.player) 
     if (self.dealer.blackjack() == False) and (self.player.score <= 21): 
      stand = 0 
      while self.player.score < 21 and stand == 0: 
       choice = player.Play() 
       if choice == 1:  # Hit 
        self.player.getcard(self.deck.draw()) 
       elif choice == 2:  # Stand 
        stand = 1 
      while self.dealer.score <= 17 and self.player.score <= 21: 
       if self.dealer.score == 17: 
        for card in self.dealer.hand: 
         if card.rank == "Ace": 
          self.dealer.draw() 
       else: 
        self.dealer.draw() 
     if self.dealer.score <= 21: 
      if (self.player.score > self.dealer.score) and (self.player.score <= 21): 
       if self.player.score == 21: 
        self.player.bankroll += self.player.bet*2.5 
       else: 
        self.player.bankroll += self.player.bet*2 
       print ("Win") 
      elif self.player.score == self.dealer.score: 
       self.player.bankroll += self.player.bet 
       print("Push") 
      else: 
       print("Lose") 
     elif (self.dealer.score > 21) and (self.player.score <= 21): 
      if self.player.score == 21: 
       self.player.bankroll += self.player.bet*2.5 
      else: 
       self.player.bankroll += self.player.bet*2 
      print ("Win") 
     else: 
      print("Lose") 
     self.player.bet = 0 
     self.player.dealer_hand = [] 
     print self.player.bankroll 


#-----------  MAIN  ----------------------------------- 

deck = Deck(6) 
player = Player(500) 
discardpile = Deck(0) 
dealer = Dealer(deck, discardpile, player) 
table = Table(dealer, player, deck, discardpile) 

table.initGame() 
while (player.bankroll > 0) and (player.bankroll < 1000): 
    table.autoPlay() 
    table.clearTable() 

print "Game Over." 
+0

你做了什麼調試?你認爲這個問題可能在哪裏?你可以減少這個[最小的,完整的例子](http://stackoverflow.com/help/mcve)?這是太多的代碼,以期望其他人通過。 –

+0

Python有一個函數來隨機隨機洗出一個值列表...我認爲它也是在隨機模塊中,並且被稱爲random.shuffle(list)...此外,代替3行來在卡片中繪製卡片類,只使用'return self.deck.pop()'Pop()刪除並返回列表中的最後一項。 – RGS

+0

我試過打印出重要的變量,並單獨測試零件,但並不總是不起作用。它只是當我在一個while循環中反覆運行代碼時,就會導致代碼中斷。我會盡量簡化它,但一切都非常重要。你知道一個好的python調試應用程序嗎? – user3371988

回答

-1

只需添加一些調試語句。您的方法中有多個while循環。我確信一個簡單的打印語句會捕捉你的邏輯中的錯誤。

DUDE,我在1分鐘內發現了無限循環。幸運的是我的盒子上安裝了python。 LINE 251

if (self.dealer.blackjack() == False) and (self.player.score <= 21): 
     stand = 0 
     while self.player.score < 21 and stand == 0: 
      print "in player score" 
      choice = player.Play() 
      if choice == 1:  # Hit 
       self.player.getcard(self.deck.draw()) 
      elif choice == 2:  # Stand 
       stand = 1 
     while self.dealer.score <= 17 and self.player.score <= 21: 
      print "in dealer score" 
      print self.dealer.score 
      if self.dealer.score == 17: 
       for card in self.dealer.hand: 
        if card.rank == "Ace": 
         self.dealer.draw() 
      else: 
       self.dealer.draw() 
+0

這並不直接回答這個問題。它應該發佈爲評論或刪除。 –

+0

@JohnKugelman它能回答這個問題。 stackoverflow不適合人們調試其他人的代碼。 – jcalloway

+0

謝謝!我會尋找它! – user3371988