Python 如何使用类方法创建新实例
我正在尝试为一个类编写一个方法,该方法将创建一个类的现有实例的新实例。问题是,当我尝试new_handname时,我无法访问控制台中的新实例 这是用于用python创建21点游戏的。代码的思想是当手被分割时,将创建一个新实例来创建一个新手Python 如何使用类方法创建新实例,python,Python,我正在尝试为一个类编写一个方法,该方法将创建一个类的现有实例的新实例。问题是,当我尝试new_handname时,我无法访问控制台中的新实例 这是用于用python创建21点游戏的。代码的思想是当手被分割时,将创建一个新实例来创建一个新手 import random class Card(object): def __init__(self, value, suit,nvalue): self.value = value self.suit = sui
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
随机导入
类别卡(对象):
定义初始值(自我、价值、适合、价值):
自我价值=价值
西服
self.nvalue=nvalue
西装=[“红心”,“黑桃”,“梅花”,“钻石”]
值=['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,11]
班手(对象):
定义初始(自身、当前手):
self.current\u hand=当前手
def手动_总计(自身):
电流和=0
对于范围内的i(0,len(自身当前手)):
当前值总和+=self.current\u hand[i]。n值
返回电流和
def手动_类型(自身):
如果self.current\u hand[0]。值==self.current\u hand[1]。值:
返回('对')
elif self.current\u hand[0]。值==“A”或self.current\u hand[1]。值==“A”:
返回('soft')
其他:
返回('hard')
def附加(自身、当前手牌、部分卡):
self.current\u hand=self.current\u hand+某张卡
def命中(自身):
self.current\u hand.append(组[0])
甲板。弹出(0)
def double(自我,新的_用户名):
新手名=手(自身)
def deal_start_hand():
玩家手牌追加(牌组[0])
甲板。弹出(0)
经销商手牌追加(牌组[0])
甲板。弹出(0)
玩家手牌。附加(牌组[0])35;###玩家得到两张牌####假设欧洲无洞牌规则
甲板。弹出(0)
def gen_甲板():
对于zip中的v,n(值,nvalue):
对于穿西装的女士:
附加(卡片(v、s、n))
###可变起始###
甲板=[]
玩家手=[]
经销商手=[]
##程序启动##
gen_甲板()
随机。洗牌(牌组)
交易开始手
p1=手牌(玩家手牌)
p1.双('p2')
p2####我希望p2返回一个实例,但不返回
>>>p1
>>>p2
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
p2
NameError:未定义名称“p2”
注意:当前手牌是一个卡片对象列表
我希望p2返回类的一个实例,但是变量p2没有定义。您的
拆分
例程可能如下所示,其中返回了类的一个新实例:
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()
但是,您的
拆分
例程需要考虑哪些牌已经打过,哪些牌仍然在牌组中,而您的代码不考虑这些。我可能建议将游戏的“状态”映射出来(把它看作一个状态机),在纸上画出来,然后考虑如何编码每个状态和转换。像这样的纸牌游戏编码起来要比乍一看复杂得多。如果要返回值,请返回新的\u handname
。但是您的代码中也存在其他问题……您没有创建任何一个类的实例self.new\u handname=Hand(self)
,我认为这可能会有所帮助。您在哪里创建实例?您能否提供一个示例,说明如何创建实例,以使您的问题更清楚。请提供一些示例同样,我猜这可能是来自另一种编程语言(C++、Java等)的某个端口。我知道split函数没有很好的定义,但我正在尝试在类的方法中创建一个实例,然后在blackjack中合并正确的游戏逻辑。