0

オブジェクトのリスト (これはゲーム用です) があり、リスト自体をオブジェクトにしたいと考えています。

最初のオブジェクトはカードです。

class card (object):
    def __init__ (self,suit,value):
        self.suit = suit
        self.value = value


    def cardpointvalue(self):
    #not relevant, basically says if card is this return this value for it
    # there is also a __str__ function and a ___repr___ function, don't think
    # they are important

これが私のカード オブジェクトです。問題が発生しているハンド オブジェクトもあります。

class hand (object):
    def __init__ (self,list_cards):
         self.list_cards = list_cards

    def hand_value (self):
        for i in list:
           handpointvalue += (i.cardpointvalue())
        return handpointvalue

私が問題を抱えている私のmain()では。

デッキ内の各カードをカード オブジェクトにするリストがあります。次に、リスト 1 とリスト 2 という人のハンドにそれらを渡します (各プレイヤーについて、簡単にするためにリスト 1 のみを扱います)。

各プレイヤーのカードをリストに渡したら。handpointvalueリストをハンドオブジェクトにしてから、それらに対して関数を実行しようとします。

そう

for i in range(10):
    one_card = card_deck.pop(0) #each card is an object at this point
   list1.append(one_card)

print (list1) #just testing, get a list of each card object

ここが私が困っているところです。私は複数のことを試しましたが、最新のものは次のとおりです。

hand(list1)
print (list1.handpointvalue())

なぜこれが機能しないのですか?このオブジェクト リストをオブジェクト自体にするにはどうすればよいですか?

4

3 に答える 3

3

まず第一に、組み込みキーワードを変数の名前として使用しないでください (例: list)。次に、 にhand.hand_valueは udefined 変数 ( handpointvalue) があり、機能しないはずです。第三に、あなたは呼び出しさえしていませんhand.hand_value():

print (list1.handpointvalue())
于 2012-12-08T15:57:07.637 に答える
1

あなたは動作する理由のあるトレースバックやコードを投稿していないので、あなたの問題は 3 つあると思います: 1) あなたが私たちに提供してくれた handpointvalue と呼ばれるメソッドはありませんself を使用してオブジェクトの属性を取得し、3) コンストラクターの仕組みを理解していないようです。

説明を考えると、これはあなたがやろうとしていることだと思います:

cards = [card_deck.pop(0) for i in range(10)]

class Hand(object):
    def __init__(self, cards):
        self.cards = cards
    def handpointvalue(self):
        return sum([c.cardpointvalue() for c in self.cards])

hand = Hand(cards)
v = hand.handpointvalue() 
# v will now be some number, assuming you've implemented cardpointvalue correctly
于 2012-12-08T15:59:25.117 に答える
-1
class hand (object):
    def __init__ (self,list_cards):
       self.list_cards = list_cards
       self.handpointvalue = ""

    def hand_value (self):
        for i in list:
            self.handpointvalue += (i.cardpointvalue())
        return self.handpointvalue

問題は、クラスの手または他の方法で handpointvalue を初期化する必要があることです

于 2012-12-08T16:02:11.063 に答える