2013-05-09 166 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对象的一个​​方法。