'Adding elements from a random list to another list [duplicate]
I am trying to create a simple and silly game of cards. For that, I created 4 different classes:
Card:
class card():
def __init__(self, name, power) -> None:
self.name = name
self.power = power
def __str__(self):
return f'Card: {self.name}\nPower: {self.power}\n'
Deck:
class deck():
cards = []
def __init__(self) -> None:
pass
def add_card_to_deck(self,card):
self.cards.append(card)
def __str__(self) -> str:
s = 'Cards:'
for c in self.cards:
s+=str(c)
return s
Player:
from deck import *
class player():
def __init__(self, name) -> None:
self.name = name
self.deck = deck()
def __str__(self) -> str:
return f'Player:{self.name}\n'
def print_cards(self):
st = f'{self.name} cards:\n'
for c in self.deck.cards:
st += str(c)
return st
Game logic:
from card import *
from deck import *
from player import *
import random
card_one = card("Ghost", 700)
card_two = card('Wizard of Oz', 950)
card_three = card('Hunter', 1000)
card_four = card('Jason', 600)
card_five = card('Creepy clown', 700)
card_six = card('dwarf', 690)
cards_list = [card_five, card_four, card_one, card_three, card_two, card_six]
def start_game():
# gets the names of the two players
first_player_name= input('First player\'s name: ')
first_player = player(first_player_name)
second_player_name = input('Second player\'s name: ')
second_player = player(second_player_name)
# gives random cards to the players
give_cards_to_decks(first_player, second_player)
#choosing a card
# choose_card(player_on=second_player)
def give_cards_to_decks(player_one, player_two):
random.shuffle(cards_list)
for index in range(6):
if index<3:
print(' -- adding to player one --')
player_one.deck.add_card_to_deck(cards_list[index])
print(cards_list[index])
else:
print(' -- adding to player two --')
player_two.deck.add_card_to_deck(cards_list[index])
print(cards_list[index])
print('player one deck cards')
print(player_one.deck)
print('player two deck cards')
print(player_two.deck)
def choose_card(player_on):
print(' ----- testing -----')
print(player_on.print_cards())
def win_play():
pass
When I run the give_cards_to_decks() function, instead of adding only three elements to each player's deck, it adds all 6 elements.
First player's name: m
Second player's name: j
-- adding to player one --
Card: Wizard of Oz
Power: 950
-- adding to player one --
Card: dwarf
Power: 690
-- adding to player one --
Card: Jason
Power: 600
-- adding to player two --
Card: Creepy clown
Power: 700
-- adding to player two --
Card: Ghost
Power: 700
-- adding to player two --
Card: Hunter
Power: 1000
player one deck cards
Cards:Card: Wizard of Oz
Power: 950
Card: dwarf
Power: 690
Card: Jason
Power: 600
Card: Creepy clown
Power: 700
Card: Ghost
Power: 700
Card: Hunter
Power: 1000
player two deck cards
Cards:Card: Wizard of Oz
Power: 950
Card: dwarf
Power: 690
Card: Jason
Power: 600
Card: Creepy clown
Power: 700
Card: Ghost
Power: 700
Card: Hunter
Power: 1000
Does anyone have any idea of what might be causing this outcome? When I print each card being added to the deck, it prints the cards individually, but the result is not what I'm trying to achieve.
Solution 1:[1]
cards is a class-level variable of the Deck class. This state is shared across all instances of Deck. So when you're appending to both player's decks, you're appending to the same list.
The deck class should look like the following. Notice that the cards is now instance-level, rather than class-level:
class deck():
def __init__(self) -> None:
self.cards = []
def add_card_to_deck(self,card):
self.cards.append(card)
def __str__(self) -> str:
s = 'Cards:'
for c in self.cards:
s+=str(c)
return s
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BrokenBenchmark |
