我想在开始之前说,我对Python和编码是非常陌生的,所以我很可能使用了一些不正确的术语。我正在尝试用Python制作一个基本版本的纸牌游戏“炸开小猫”。我正在尝试从一副牌(没有小猫,你想要避免绘制的牌)到每个类玩家的对象的实例属性"hand“中发牌,然后从牌组中移除这张牌。我的问题是,如果不让实例属性看起来像一个类属性,我就不能让它变成hand。我的代码和结果如下所示:
import random
# Deck without kittens to deal out to the Players
deck_no_kittens = ["Attack","Attack","Attack","Attack","Steal","Steal","Steal","Steal","Favor","Favor","Favor","Favor","See the future","See the future","See the future","See the future","See the future","Alter the future","Alter the future","Shuffle","Shuffle","Shuffle","Shuffle","Skip","Skip","Skip","Skip"]
# Default starting hand for each player
start_hand = ["Defuse"]
class Player():
  def __init__(self, hand, alive, knowskitten):
    # Hand of each Player
    self.hand = hand
    # Determines if Player is alive
    self.alive = True
    # Determines if the Player knows if there is a kitten
    self.knowskitten = False
# Defines function that deals to Player while also removing it from deck_no_kittens
def deal(player):
  random_index = random.randint(0,len(deck_no_kittens)-1)
  card_to_add = deck_no_kittens.pop(random_index)
  player.hand.append(card_to_add)
# Initialize objects
computer1 = Player(start_hand, True, False)
computer2 = Player(start_hand, True, False)
user = Player(start_hand, True, False)
# Below is where my issue seems to be - the hand remains the same throughout each deal
# Deals 5 times, alternating, to computer1 and computer2, and prints the hand each time
for i in range(5):
  deal(computer1)
  print("\ncomputer1 hand is "+str(computer1.hand))
  deal(computer2)
  print("\ncomputer2 hand is"+str(computer2.hand))
# Prints deck_no_kittens
print("\n"+str(deck_no_kittens))结果:
computer1 hand is ['Defuse', 'Attack']
computer2 hand is['Defuse', 'Attack', 'Skip']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle']
computer1 hand is ['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle', 'Favor']
computer2 hand is['Defuse', 'Attack', 'Skip', 'See the future', 'Steal', 'Skip', 'Attack', 'Attack', 'Shuffle', 'Favor', 'Shuffle']
['Attack', 'Steal', 'Steal', 'Steal', 'Favor', 'Favor', 'Favor', 'See the future', 'See the future', 'See the future', 'See the future', 'Alter the future', 'Alter the future', 'Shuffle', 'Shuffle', 'Skip', 'Skip']我预计每个对象的每一只手都是不同的,但每一笔交易都会增加一只通用的手。如有任何帮助/建议,我们不胜感激。
发布于 2019-04-16 20:53:44
两个实例的hand属性都是对同一列表的引用,因此当该列表被修改时,它会同时影响这两个实例。
一种简单的解决方法是将列表复制到类的__init__中
from copy import copy
class Player():
  def __init__(self, hand, alive, knowskitten):
    # Hand of each Player
    self.hand = copy(hand)
    # Determines if Player is alive
    self.alive = True
    # Determines if the Player knows if there is a kitten
    self.knowskitten = Falsehttps://stackoverflow.com/questions/55708537
复制相似问题