【问题标题】:How to create a new instance using a class method如何使用类方法创建新实例
【发布时间】:2018-12-31 00:09:41
【问题描述】:

我正在尝试为一个类编写一个方法,该方法将为一个已经存在的类实例创建一个新实例。问题是当我尝试 new_handname 时,我无法在控制台中访问新实例。

这是用于在 python 中创建二十一点游戏。代码的想法是,当手被拆分时,将创建一个新实例以创建新手

import random


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

suit = ['Hearts','Spades','Clubs','Diamonds']
value = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
nvalue = [2,3,4,5,6,7,8,9,10,10,10,10,11]


class Hand(object):
    def __init__(self,current_hand):
        self.current_hand = current_hand

    def hand_total(self):
        current_sum = 0
        for i in range(0,len(self.current_hand)):
            current_sum += self.current_hand[i].nvalue
        return current_sum

    def hand_type(self):
        if self.current_hand[0].value == self.current_hand[1].value:
            return('pair')
        elif self.current_hand[0].value == 'A' or self.current_hand[1].value == 'A':
            return('soft')
        else:
            return('hard')

    def append(self,current_hand,some_card):
        self.current_hand = self.current_hand + some_card

    def hit(self):
        self.current_hand.append(deck[0])
        deck.pop(0)

    def double(self,new_handname):  
        new_handname = Hand(self)


def deal_start_hand():
    player_hand.append(deck[0])
    deck.pop(0)
    dealer_hand.append(deck[0])
    deck.pop(0)
    player_hand.append(deck[0]) #### player gets two cards ### assuming europe no hole card rules
    deck.pop(0)

def gen_deck():
    for v,n in zip(value,nvalue):
        for s in suit:
            deck.append(Card(v,s,n))


### variable initiation ###

deck = []
player_hand = []
dealer_hand = []


##program start ##

gen_deck()
random.shuffle(deck)
deal_start_hand()

p1 = Hand(player_hand)
p1.double('p2')
p2   ### I expect p2 to return an instance but does not 

>>> p1 
<__main__.Hand object at 0x00000006A80F0898>
>>> p2
Traceback (most recent call last):
  File "<pyshell#182>", line 1, in <module>
    p2
NameError: name 'p2' is not defined

注意:current_hand 是卡片对象的列表。

我希望 p2 返回一个类的实例,但是变量 p2 没有定义

【问题讨论】:

  • return new_handname 如果要返回值。但是您的代码中也确实存在其他问题...
  • 你还没有创建任何一个类的单个实例
  • self.new_handname = Hand(self) 我认为可能会有所帮助。
  • 您在哪里创建实例?您能否提供一个示例来说明如何创建实例以使您的问题更清楚。请提供一些例子
  • 另外,我猜这可能是另一种编程语言(C++、Java 等)的移植

标签: python


【解决方案1】:

您的split 例程可能如下所示,其中返回了一个新的类实例:

class Hand(object):
    def __init__(self, current_hand):
        self.current_hand = current_hand

    def split(self):
        return Hand(self.current_hand)

只需创建一个实例,然后再拆分它:

# You need to define "some_default" somewhere
myhand = Hand(some_default)
second_hand = myhand.split()

但是,您的 split 例程需要考虑哪些牌已经打出,哪些牌仍在牌组中,您的代码都没有考虑这些。我可能会建议绘制游戏的“状态”(将其视为状态机),将其绘制在纸上,然后考虑如何编码每个状态和转换。像这样的纸牌游戏的代码比乍看之下要复杂。

【讨论】:

  • 我知道拆分函数没有很好地定义,但我试图在一个类的方法中创建一个实例,然后在二十一点中加入正确的拆分游戏逻辑。
猜你喜欢
  • 2011-08-24
  • 2012-10-26
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-10
相关资源
最近更新 更多