2013-05-09 161 views
0

我們有麻煩了gettot()方法在我們的遊戲類的方一道類的內部方法。當它作爲一個整體在我們的遊戲中被調用時,它會返回一個錯誤信息,說我們沒有正確數量的輸入以使函數正常運行。目前,這些功能的第一次迭代被註釋掉了,所以我們可以在項目沒有停止每次運行時停止遊戲。麻煩與蟒蛇

這是我們遇到問題的gettot方法:


這種方法的目的是爲了得到總的一個球員或經銷商已在他們手中的牌,如果有一個王牌,它決定它是否應該是1或11,這取決於哪一個更接近21個總分而不會超過。

def gettot(self,hand): 
     total=0 
     for x in self.hand: 
      if x==Card('H','A'): 
       b=total+x 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      if x==Card('D','A'): 
       b=total+x 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      if x==Card('S','A'): 
       b=total+x 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      if x==Card('C','A'): 
       b=total+x #changed 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      else: 
       total+=x 
     return(total) 

from random import* 
#do we need to address anywhere that all face cards are worth 10? 
class Card(object): 
    def __init__(self,suit,number): 
     self.number=number 
     self.suit=suit 
    def __str__(self): 
     return '%s %s'%(self.number,self.suit) 

class DeckofCards(object): 
    def __init__(self,deck): 
     self.deck=deck 
     self.shuffledeck=self.shuffle() 

    def shuffle(self): 
     b=[] 
     count=0 
     while count<len(self.deck): 
      a=randrange(0,len(self.deck)) 
      if a not in b: 
       b.append(self.deck[a]) 
       count+=1 
     return(b) 

    def deal(self): 
     if len(self.shuffledeck)>0: 
      return(self.shuffledeck.pop(0)) 
     else: 
      shuffle(self.deck) #need to refill deck 
      return(self.shuffledeck.pop(0)) 
class Player(object): 
    def __init__(self,name,hand,inout,money,score,bid): 
     self.name=name 
     self.hand=hand 
     self.inout=inout 
     self.money=money 
     self.score=score 
     self.bid=bid 

    def __str__(self): 
     x = self.name + ":\t" 
     x += "Card(s):" 
     for y in range(len(self.hand)): 
      x +=self.hand[y].face + self.hand[y].suit + " " 
     if (self.name != "dealer"): 
      x += "\t Money: $" + str(self.money) 
     return(x) 

class Game(object): 
    def __init__(self,deck, player): 
     self.player=Player(player,[],True,100,0,0) 
     self.dealer=Player("Dealer",[],True,100,0,0) 
     self.deck=DeckofCards(deck) 
     self.blackjack= False 
    def blackjacksearch(self): 
     if Game.gettot(self.player.hand)==21:#changed 
      return True 
     else: 
      return False  
    def firstround(self): 
     #self.player.inout=True#do we need this since this is above 
     #self.player.hand=[]#do wee need this.... 
     #self.dealer.hand=[]#do we need this .... 
     self.player.hand.append(DeckofCards.deal(self.deck)) 
     for card in self.player.hand: 
      a=card 
     print(self.player.name + ' ,you were dealt a '+str(a)) 
     self.dealer.hand.append(DeckofCards.deal(self.deck)) 
     for card in self.dealer.hand: 
      a=card 
     print('The Dealer has '+str(a)) 
     playerbid=int(input(self.player.name + ' how much would you like to bet? ')) 
     self.player.money-=playerbid 
     self.player.bid=playerbid 
    def playturn(self): #should this be changed to inout instead of hit.....we never use inout 
     #for player in self.player: 
     # a=player 
     #print(str(a)) 
     hit=input('Would you like to hit? ') #should input be in loop? 
     while self.player.inout==True: #and self.blackjack!=True:#changed 
      #print(self.player.name + ' , your hand has:' + str(self.player.hand)) #do we want to make this gettot? so it prints out the players total instead of a list....if we want it in a list we should print it with out brakets 
      self.player.hand.append(DeckofCards.deal(self.deck)) 
      for card in self.player.hand: 
       a=card 
      print('The card that you just drew is: ' + str(a))    
      print(self.player.name + ' , your hand has:' + str([str(card) for card in self.player.hand])) 
      #print(Game.gettot(self.player.hand)) 
      hit=input('Would you like to hit? ') 
      if hit=='yes': 
       (self.player.hand.append(DeckofCards.deal(self.deck)))#changed 
       self.player.inout==True# 
      else: 
       (self.player.hand) #changed 
       self.player.inout==False #changed 
     if self.player.blackjack==True: 
      print(self.player.name + " has blackjack ") 
     if hit=='no': 
      print (self.player.hand.gettot()) 
    def playdealer(self): 
     while Game.gettot(self.dealer.hand)<17:#changed 
      self.dealer.hand.append(DeckofCards.deal(self.deck)) 
      dealerhand=Game.gettot(self.dealer.hand) #changed 
      print(dealerhand) 
     if Game.gettot(self.dealer.hand)==21:#changed 
      self.dealer.blackhjack=True 
     dealerhand1=Game.gettot(self.dealer.hand)#changed 
     print(dealerhand1) 

    def gettot(self,hand): 
     total=0 
     for x in self.hand: 
      if x==Card('H','A'): 
       b=total+x 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      if x==Card('D','A'): 
       b=total+x 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      if x==Card('S','A'): 
       b=total+x 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      if x==Card('C','A'): 
       b=total+x #changed 
       if b>21: 
        total+=1 
       else: 
        total+=11 
      else: 
       total+=x 
     return(total) 

    def playgame(self): 
     play = "yes" 
     while (play.lower() == "yes"): 
      self.firstround() 
      self.playturn() 
      if self.player.blackjack == True: 
       print(self.player.name + " got BLACKJACK! ") 
       self.player.money += self.player.bid * 1.5 
       print (self.player.name + " now has " + str(self.player.money)) 
       print("\n") 
       self.player.inout = False 
      if self.player.score > 21: 
       print(self.player.name + " lost with a tot of " + str(self.player.score)) 
       self.player.money -= self.player.bid 
       print (self.player.name + " now has " + str(self.player.money)) 
       print ("\n\n") 
       self.player.inout = False 
      self.playdealer() 
      if self.dealer.blackjack == True: 
       print("Dealer got blackjack, dealer wins\n") 
       self.player.money -= self.player.bid 
       print("Round\n") 
       print("\t",self.dealer) 
       print("\t",self.player) 
       print("\t Dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score)) 
      elif self.player.inout == True: 
       print("Round\n") 
       print("\t",self.dealer) 
       print("\t",self.player) 
       print("\n\t Dealer has " + str(self.dealer.score) + ", " + self.player.name + " has " + str(self.player.score)) 
       if self.dealer.score > 21: 
        print("\t Dealer lost with a total of " + str(self.dealer.score)) 
        self.player.money += self.player.bid 
        print(self.player.name + " now has " + str(self.player.money)) 
       elif self.player.score > self.dealer.score: 
        print("\t" +self.player.name + " won with a total of " + str(self.player.score)) 
        self.player.money += self.player.bid 
        print("\t"+self.player.name + " now has " + str(self.player.money)) 
       else: 
        print("\t Dealer won with a total of " + str(self.dealer.score)) 
        self.player.money -= self.player.bid 
        print("\t"+self.player.name + " now has " + str(self.player.money)) 
      else: 
       print("Round") 
       print("\t",self.dealer) 
       print("\t",self.player) 
       if self.player.blackjack == False: 
        print("\t "+ self.player.name + " lost") 
       else: 
        print("\t "+self.player.name + " Won!") 

      if self.player.money <= 0: 
       print(self.player.name + " out of money - out of game ") 
       play = "no" 
      else: 
       play = input("\nAnother round? ") 
       print("\n\n") 
     print("\nGame over. ") 
     print(self.player.name + " ended with " + str(self.player.money) + " dollars.\n") 
     print("Thanks for playing. Come back soon!") 



ls= [Card('H','A'),Card('H','2'),Card('H','3'),Card('H','4'),Card('H','5'),Card('H','6'),Card('H','7'),Card('H','8'),Card('H','9'),Card('H','10'), 
Card('H','J'),Card('H','Q'),Card('H','K'), 
Card('S','A'),Card('S','2'),Card('S','3'),Card('S','4'),Card('S','5'), 
Card('S','6'),Card('S','7'),Card('S','8'),Card('S','9'),Card('S','10'), 
Card('S','J'),Card('S','Q'),Card('S','K'), 
Card('C','A'),Card('C','2'),Card('C','3'),Card('C','4'),Card('C','5'), 
Card('C','6'),Card('C','7'),Card('C','8'),Card('C','9'),Card('C','10'), 
Card('C','J'),Card('C','Q'),Card('C','K'), 
Card('D','A'),Card('D','2'),Card('D','3'),Card('D','4'),Card('D','5'), 
Card('D','6'),Card('D','7'),Card('D','8'),Card('D','9'),Card('D','10'), 
Card('D','J'),Card('D','Q'),Card('D','K')] 


def main(): 
    x = input("Player's name? ") 
    blackjack = Game(ls,x) 
    blackjack.playgame() 
main() 
+1

您需要說明的問題是什麼。不要只說「不行」。它做什麼,你不希望它做什麼? – BrenBarn 2013-05-09 19:41:19

+0

我只是想添加一些信息,但其實我不確定什麼樣的問題是 – 2013-05-09 19:43:56

+1

gettot()有兩個參數:'self'和'hand'。 'hand'永遠不會被使用,當你調用函數時,你只傳遞一個參數('self.dealer.hand')。此外,這似乎是一類以外定義的函數,所以它真的是沒有意義它有在首位'self'說法。 – 2013-05-09 19:48:34

回答

2

TL; DR但我注意到的一件事是:x==Card('H','A')。除非你定義Card類型來處理的有效途徑相等比較

這是行不通的。默認情況下,它會檢查,如果他們都是相同的對象,並作爲創建一個新卡,也不會是相同的對象x

class Card(object): 
    # ... 
    def __eq__ (self, other): 
     return self.number == other.number and self.suit == other.suit 

此外,這個:b=total+x。如果x是一個卡片對象,你怎麼想象它被添加到一個數字?你必須定義這個,或者改爲​​。

另一件事是您定義gettot採取hand參數,但在該函數中,您遍歷self.hand。所以,你傳遞給函數的任何另一方面正在悄悄地忽略,self.hand來代替。

同樣在此:

def blackjacksearch(self): 
    if Game.gettot(self.player.hand)==21: 
    # ... 

此方法屬於遊戲類型;這是一個實例方法(參數爲self)。但實際上你將它稱爲來自Game類型的靜態方法而不是實例。它應該是類似於self.gettot()的東西(可以按照上面的說明忽略參數)。

你在做其他一些地方也一樣,試圖通過使用TypeName.method調用實例方法。你需要有你打電話給他們的物品。

我覺得你可以讓你的gettot方法也短了很多:

def gettot(self,hand): 
    total=0 
    for x in self.hand: 
     if x.number == 'A': 
      if total + 11 > 21: 
       total += 1 
      else: 
       total += 11 
     elif x.number == 'J' or x.number == 'Q' or x.number == 'K': 
      pass # what to do with these? 
     else: 
      total += x.number 
    return(total) 

重寫你的代碼的某些部分:

class Card (object): 
    def __init__ (self, suit, number): 
     self.suit = suit 
     self.number = number 

    def getValue (self): 
     if self.number in ('J', 'Q', 'K'): 
      return 10 
     elif self.number == 'A': 
      return 11 
     else 
      return int(self.number) 

    def isAce (self): 
     return self.number == 'A' 

    def __eq__ (self, other): 
     return self.suit == other.suit and self.number == other.number 

    def __str__ (self): 
     return '%s %s' % (self.number,self.suit) 

class DeckOfCards (object): 
    def __init__ (self, deck): 
     self.fullDeck = deck 
     self.shuffle() 

    def shuffle (self): 
     self.deck = self.fullDeck[:] # copy the full deck 
     random.shuffle(self.deck) 

    def deal (self): 
     if not len(self.deck): # need to refill deck 
      self.shuffle() 
     return self.deck.pop(0) 

ls = [] 
for suit in ('H', 'S', 'C', 'D'): 
    for number in ('A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'): 
     ls.append(Card(suit, number)) 

gettot方法應該屬於玩家,而應該是這樣的:

def gettot(self): 
    total = 0 
    for card in self.hand: 
     if card.isAce() and total > 10: 
      total += 1 
     else: 
      total += card.getValue() 
    return total 
+0

當我添加你的建議,這就是我得到的。 '回溯(最近通話最後一個): ........ 文件 「C:\用戶\伊恩\下載\ proj2.1.py」 55行,在__init__ self.deck = DeckofCards(甲板) 文件 「C:\用戶\伊恩\下載\ proj2.1.py」,第15行,在__init__ self.shuffledeck = self.shuffle() 文件「C:\用戶\伊恩\下載\ proj2.1 py」爲22行,在洗牌 如果不是b: 文件 「C:\用戶\伊恩\下載\ proj2.1.py」,10號線,在__eq__ 回報self.number == other.number和self.suit ==其他。套裝 AttributeError:'int'對象沒有'number''屬性 – 2013-05-09 19:50:45

+0

@TammyLogger這是因爲你正在比較一個數字與一個卡片對象,你不斷地將類型處理卡片作爲你可以添加到數字中的數字或者檢查一個數字數字在卡片的列表中(這就是你的錯誤來自的地方:'如果在'shuffle'方法中,一個不在b中列出卡片'b'和一個int'a')。 – poke 2013-05-09 19:58:24

0

有很多問題

  1. 我會爲Card添加一個函數,詢問它是否是Ace。這樣做會更好,然後構建一堆並查看它們是否相同。
  2. 您正在使用==代替=對於一些分配給你的。 ==僅供比較。 =是分配。
  3. 不要說inout == True只是說inout(因爲inout已經是bool了)
  4. 你不斷彈出甲板。當你用完時,你再次洗牌,但此時它沒有任何內容。
  5. 頂部gettot你參考self.hand,但self沒有hand它應該只是hand,因爲它被傳遞到gettot
  6. player.blackjack是從來沒有定義,但我想補充一個檢查那裏是否gettot == 21,並移動gettot到modual功能現在
  7. self.player.hand.gettot()應該gettot(self.player.hand)
  8. total+=x這裏一共是一個int和X是一張卡,除非你定義瞭如何給一張卡添加一個int,否則你不能添加這些卡。我現在只需添加一個get_value函數來獲取面值。 (你可以處理的10牌面作爲值也在這裏),然後使用total += x.get_value()
  9. 你也想調整在gettot結束王牌,否則你不知道,如果你要超過21即。如果你手中的第一張牌是王牌會發生什麼?

例如:

class Card(object): 
    def __init__(self,suit,number): 
     self.number=number 
     self.suit=suit 

    def __str__(self): 
     return '%s %s'%(self.number,self.suit) 

    def get_value(self): 
     if self.number == 'A': 
      return 11 
     if self.number in 'JQK': 
      return 10 
     return int(self.number) 

    def is_ace(self): 
     return self.number == 'A' 


def gettot(hand): 
    total = 0 
    aces = 0 
    for x in hand: 
     if x.is_ace(): 
      aces += 1 
     total += x.get_value() 
    while aces and total > 21: 
     aces -= 1 
     total -= 10 
    return total 

這應該讓你開始,只是不停地調試

0

您定義gettot()採取兩個參數:自我,和手。然後,你從來不用hand,當你調用該函數,你只能傳遞一個參數(self.dealer.hand)。

消除self參數(無論如何,因爲它是在類的外部定義的函數),並將for x in self.hand行替換爲for x in hand。更好的是,使函數成爲Player對象的一個​​方法。