真的很奇怪,我制作了一个纸牌游戏并制作了这个功能:
def draw_card_from_top(destination, departure):
if len(departure.cards) > 0:
destination.add_card(departure.cards.pop(0))
如果我放入目的地甲板和出发甲板,它会按预期作业。目的地甲板从出发甲板的顶部获取卡片。
但我也做了这个功能,应该给每个玩家发 8 张牌:
def deal_new_game(players, departure):
for i in range(len(players)):
for j in range(CARDS_PER_ROUND):
draw_card_from_top(players[i].deck, departure)
现在我有以下问题;每位玩家应将出发牌组中的 8 张牌添加到他自己的目标牌组中。但出于某种原因,每个玩家现在都有一套完整的套牌副本,所以所有 32 张牌都是 1 个而不是 8 个和 2 个。每个人都有相同的顺序
------ 完整原始码-------
主要.py:
import _card, _deck, _player, game
is_invalid = True
while is_invalid:
try:
player_count = int(input('How many players? (3 or 4): '))
if player_count == 3 or player_count == 4:
is_invalid = False
except ValueError:
pass
player = []
for i in range(player_count):
player.append(_player.Player())
game_deck = _deck.Deck(game.create_new_deck())
game_deck.shuffle_deck()
_player.py:
import _deck
class Player():
def __init__(self):
self.deck = _deck.Deck()
self.score = 0
_deck.py:
from random import shuffle
class Deck():
# Ein Deck ist eine Ansammlung von einer oder mehreren Karten ('Card()''s)
def __init__(self, _cards=[]):
self.cards = _cards
def print_deck(self):
for i in range(0, len(self.cards)):
print(self.cards[i].term)
def shuffle_deck(self):
shuffle(self.cards)
def deck_size(self):
return len(self.cards)
def add_card(self, card):
self.cards.append(card)
_card.py:
class Card():
# Eine Karte besteht aus folgenden 3 Attributen:
# 1. color, die Color gibt an, um welche Farbe es sich handelt, 'Kreuz', 'Pik', 'Herz', 'Raute'
# 2. value, die Value gibt an, welchen Wert eine Karte hat. ZB hat die Herz 10 die value 10 und ein Kreuz Bube die value 11
# 3. term, der Term gibt an wie die Karte im Spiel bereichnet wird, so ist die Herz 7 die 'H7' oder die Pik Dame 'PQ'
def __init__(self, _color, _value, _term):
self.color = _color
self.value = _value
self.term = _term
游戏.py:
import _card
CARDS_PER_ROUND = 8
def create_new_deck():
cards = []
for i in range(4):
if i == 0:
color = 'Kreuz'
term_color = 'K'
if i == 1:
color = 'Pik'
term_color = 'P'
if i == 2:
color = 'Herz'
term_color = 'H'
if i == 3:
color = 'Raute'
term_color = 'R'
for value in range(7, 15):
if value <= 10:
term_value = str(value)
if value == 11:
term_value = 'B'
if value == 12:
term_value = 'D'
if value == 13:
term_value = 'K'
if value == 14:
term_value = "A"
cards.append(_card.Card(color, value, term_color term_value))
return cards
def draw_card_from_top(destination, departure):
if len(departure.cards) > 0:
destination.add_card(departure.cards.pop(0))
def draw_card_by_index(destination, departure, index):
if len(departure.cards) >= index:
destination.add_card(departure.cards.pop(index))
def deal_new_game(players, departure):
for i in range(len(players)):
for j in range(CARDS_PER_ROUND):
draw_card_from_top(players[i].deck, departure)
# TODO irgendwas buggt hier voll rum. Spieler-Decks bestehen nach Aufruf der Funktion komplett aus vollst?ndigen Decks.
uj5u.com热心网友回复:
造成这个问题可变默认自变量你的_deck.py
模块。
当您创建一个新的 Deck() 时,它的 self.card 属性对于所有实体都是相同的(因为card=[]
在 init 定义中)。
如果你把它改成这样,它应该可以正常作业:
def __init__(self, _cards=None):
self.cards = [] if _cards is None else _cards
这个想法是为了避免对自变量使用可变默认值。当使用卡片串列(制作副本或使用参考)创建卡组时,根据您希望卡组执行的操作,您可能需要以不同的方式实作它。
第一个示例使用对 _cards 串列的参考。要使用副本,您可以保留可变默认值,因为您不会将其保存在物件实体中:
def __init__(self, _cards=[]):
self.cards = _cards.copy()
0 评论