Python纸牌游戏 - 尝试将弹出的对象从一个类附加到另一个类
Posted
技术标签:
【中文标题】Python纸牌游戏 - 尝试将弹出的对象从一个类附加到另一个类【英文标题】:Python card game - trying to append a popped object from one class to another 【发布时间】:2021-12-22 02:59:03 【问题描述】:以下python代码是我正在处理的:
import random
ranks = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
suits = ["Spades", "Clubs", "Hearts", "Diamonds"]
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def print(self):
print(self.rank, "of", self.suit)
class Deck:
def __init__(self):
self.cards = []
def build(self):
for x in ranks:
for y in suits:
self.cards.append(Card(x,y))
def shuffle(self):
random.shuffle(self.cards)
def print(self):
for x in self.cards:
x.print()
def draw(self):
self.cards.pop()
y = Deck()
y.build()
y.shuffle()
class Player:
def __init__(self):
self.hand = []
def take(self):
self.hand.append()
def print(self):
for x in self.hand:
x.print()
z = Player()
z.take()
z.print()
对于我的生活,我似乎无法让 z.take() 和 z.print() 工作。我可以让 Class Deck 弹出一张卡片,然后使用 Class 方法显示它,但我似乎无法将卡片放到 Class Player 的手中。我如何将卡附加到职业玩家的手上?如果有人可以帮助我,我将不胜感激。谢谢。
【问题讨论】:
你可能不应该为你的函数命名 print 在take()
函数中,您没有在手上添加任何内容。
@BrutusForcus 如果它是一个实例方法就没有那么糟糕了,这样它就不能在没有引用实例的情况下被调用
card = y.pop()
z.take(card)
。但它需要return self.cards.pop()
和def take(self, card): self.hand.append(card)
【参考方案1】:
首先你应该使用return
def draw(self):
return self.cards.pop()
接下来你应该在
中使用参数(即card
)
def take(self, card):
self.hand.append(card)
然后你就可以了
card = y.draw()
z.take(card)
或
z.take(y.draw())
顺便说一句:
您可以使用更好的名称来代替 x
、y
、z
- 使代码更具可读性。
import random
# --- constants --- # `UPPER_CASE_NAMES`
RANKS = ["Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"]
SUITS = ["Spades", "Clubs", "Hearts", "Diamonds"]
# --- classes ---
class Card:
def __init__(self, rank, suit):
self.rank = rank
self.suit = suit
def print(self):
print(self.rank, "of", self.suit)
class Deck:
def __init__(self):
self.cards = []
def build(self):
for rank in RANKS:
for suit in SUITS:
self.cards.append(Card(rank, suit))
def shuffle(self):
random.shuffle(self.cards)
def print(self):
for card in self.cards:
card.print()
def draw(self):
return self.cards.pop()
class Player:
def __init__(self):
self.hand = []
def take(self, card):
self.hand.append(card)
def print(self):
for card in self.hand:
card.print()
# --- functions ---
# empty
# --- main ---
deck = Deck()
deck.build()
deck.shuffle()
player1 = Player()
player2 = Player()
for _ in range(5):
card = deck.draw()
player1.take(card)
card = deck.draw()
player2.take(card)
print('--- player 1 ---')
player1.print()
print('--- player 2 ---')
player2.print()
带有列表和for循环的版本
因为名称(单个)player
和(多个)players
非常相似,有时会在代码中出错,所以我使用前缀all_
使all_players
和player
更易于识别。
# --- main ---
deck = Deck()
deck.build()
deck.shuffle()
all_players = [Player(), Player(), Player(), Player()]
for player in all_players:
for _ in range(5):
card = deck.draw()
player.take(card)
for number, player in enumerate(all_players, 1):
print('--- player', number, '---')
player.print()
【讨论】:
以上是关于Python纸牌游戏 - 尝试将弹出的对象从一个类附加到另一个类的主要内容,如果未能解决你的问题,请参考以下文章
将弹出框添加到以编程方式添加的 UIBarButtonItem 并将图像添加到相同的弹出框 Swift