Кажется, что Python рассматривает атрибут экземпляра как атрибут класса - PullRequest
1 голос
/ 16 апреля 2019

Я хотел бы предвосхитить это, сказав, что я очень плохо знаком с Python и вообще программирую, поэтому вполне возможно, что я неправильно использую некоторые термины. Я пытаюсь сделать базовую версию карточной игры «Взрывающиеся котята» на Python. Я пытаюсь раздать карты из колоды (без котят, карты, которую вы хотите избежать вытягивания) для атрибута экземпляра «рука» для каждого объекта класса Player, а затем удалить карту из колоды. Моя проблема в том, что я не могу сделать атрибут экземпляра рукой, если он не выглядит как атрибут класса. Мой код и результат отображаются ниже:

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']

Я ожидал, что каждая рука для каждого объекта будет отличаться, но каждая сделка добавляет универсальную руку. Любая помощь / предложения приветствуются.

Ответы [ 2 ]

0 голосов
/ 16 апреля 2019

Проблема здесь в том, что вы используете один и тот же «объект» start_hand для создания обоих игроков.Вы сохраняете ссылку на тот же список start_hand во внутренней переменной hand.

Когда вы вносите изменения в hand в одном игроке - его видит другой игрок.

Чтобы решить эту проблемувместо этого создайте новый список.

self.hand = list(start_hand)
0 голосов
/ 16 апреля 2019

Атрибуты 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 = False
...