2013-05-09 50 views
0

我們應該使用下面的代碼打印出其中列出的參數,但是目前我們無法做到這一點,並且正在使用圍繞方法的一輪。這應該是打印出來的東西,而不是我們在遊戲類的playturn功能打印出Python中的類時遇到問題

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) 

這裏是我們的實際代碼打印出來,如果你還看到任何其他問題,您的輸入將不勝感激

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'%(self.number) 

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) 
      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(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() 
+2

你準確的問題是什麼?出了什麼問題,如果你有一個錯誤信息,你預期會發生什麼? – 2013-05-09 19:06:00

+0

看起來像某種學校/作業,根據你的問題。究竟是什麼,你不明白或不像你期望的那樣工作? – 2013-05-09 19:07:14

+0

一個問題是'從隨機導入*';不要使用通配符導入。 – 2013-05-09 19:21:33

回答

1

的問題是,至少在一些地方,你想打印list

雖然打印任何東西,包括一個list,呼籲str就可以了,list.__str__方法調用它的元素repr。 (如果你不知道strrep之間的區別,見Difference between __str__ and __repr__ in Python。)

如果你要打印的每一個元素的str在列表中,你必須這樣做明確的,具有map或列表理解。

例如,而不是這樣的:

print(self.player.name + ' , your hand has:' + str(self.player.hand)) 

...這樣做:

print(self.player.name + ' , your hand has:' + [str(card) for card in self.player.hand]) 

但是,這仍可能不是你想要的。你會得到['8', '9']而不是[<__main__.Card object at 0x1007aaad0>, <__main__.Card object at 0x1007aaaf0>],但你可能想要更像'8H 9C'的東西。要做到這一點,你想要的東西,如:

print(self.player.name + ' , your hand has:' + 
     ' '.join(str(card) for card in self.player.hand)) 

你已經有類似(但更詳細)代碼中Player.__str__

for y in range(len(self.hand)): 
    x +=self.hand[y].face + self.hand[y].suit + " " 

該代碼可以在幾個方面加以改進。

首先,它將增加AttributeError,因爲您使用的是face而不是number。但是真的,你根本不需要這麼做 - 你創建Card.__str__方法的全部原因是你可以使用str(Card),對吧?

其次,你幾乎從不想循環range(len(foo)),特別是如果你在循環內部做foo[y]。直接循環播放foo即可。

把它們一起:

for card in self.hand: 
    x += str(card) + " " 

在任何情況下,你需要做同樣的事情,在這兩個地方。

使用join方法和生成器表達式的版本比顯式循環稍微簡單一些,但需要更多的Python知識來理解。這裏是你怎麼會在這裏使用它:

x += " ".join(str(card) for card in self.hand) + " " 

你的下一個問題是:Card.__str__不包括訴訟。所以,而不是8H 9C,你會得到8 9。這應該是一個簡單的解決方案,可以自己完成。


同時,如果你發現自己編寫相同的代碼不止一次,你可能要抽象出來。您可以只是寫一個函數,一個手list,並把它變成一個字符串:

def str_hand(hand): 
    return " ".join(str(card) for card in self.hand) 

但它可能是更好的創建Hand類,包裝了的卡的列表,並通過圍繞,而不是直接使用list

0

由於您引用了player.hand,因此您沒有運行所需的功能。嘗試改變

str(self.player.hand) 

​​
+0

所以你想說「你已經有:」然後打印出一個「玩家」對象,顯示玩家的名字,他的手和他的錢? – abarnert 2013-05-09 19:09:53

+0

@abarnert'我們應該使用下面的代碼來打印它列出的參數',是的,這就是我的目標。 – John 2013-05-09 19:15:12

+0

@abarnert這是我們正在努力完成的。 – 2013-05-09 19:15:22