问题描述
我用以下代码构建了一副纸牌.我正在运行一个 while 循环来尝试从牌组中抽牌,但是在牌组的整个长度上重复抽了同一张牌.很明显,我想要的是每次都画一张不同的卡片.
I have built a deck of cards in the following code. I'm running a while loop to try drawing cards from the deck, but the same card is repeatedly drawn for the entire length of the deck. Obviously, what I want is to draw a different card each time.
我做错了什么?
import random
class Card(object):
def __init__(self, suit, value):
self.suit = suit
self.value = value
def show(self):
print("{} of {}".format(self.value, self.suit))
return self.value
class Deck(object):
def __init__(self):
self.cards = []
self.build()
def build(self):
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(1, 14):
self.cards.append(Card(s, v))
#print("{} of {}". format(v, s))
def show(self):
for cards in self.cards:
print(cards.show())
def shuffle(self):
random.shuffle(self.cards)
def draw_card(self):
return self.cards.pop()
class Player(object):
def __init__(self, name):
self.name = name
self.hand = []
def draw(self, Deck):
self.hand.append(Deck.draw_card())
return self
def show_hand(self):
for card in self.hand:
card.show()
return card.value
Computer = Player("Computer")
deck = Deck()
deck.shuffle()
while len(deck.cards) > 0:
Computer.draw(deck)
Computer.show_hand()
推荐答案
你的 show_hand
有一个循环,它在第一次迭代时立即退出,所以它只打印 first卡然后退出......无论手上有多少张卡.所以绘制卡片是没有问题的.问题出在打印...
Your show_hand
has a loop that exits immediately upon its first iteration, so it just prints the first card and then exits... no matter how many cards are in the hand. So there is no problem with drawing the cards. The problem is in the printing...
既然 card.show()
prints 一些东西,show_hand
真的不应该返回任何东西,你应该让循环完成所有它的迭代.
Since card.show()
prints something, show_hand
really shouldn't return anything, and you should just let the loop make all its iterations.
所以我建议把它改成这样:
So I would suggest changing it to this:
def show_hand(self):
for card in self.hand:
card.show()
然后只调用一次,就像这样:
And then call it only once, like this:
while len(deck.cards) > 0:
Computer.draw(deck)
Computer.show_hand()
一些改进
我会避免调用 print
的方法.由于关注点分离的原则,这真的不应该在一个类中完成,而是留给主驱动程序代码.
Some improvements
I would avoid methods that call print
. Because of the principle of separation of concerns, this really should not be done in a class, but left to the main driver code.
此外,如果玩家的手也是 Deck
的实例,那就太好了.这样你就可以将那个职业的力量也应用到玩家的手上.
Also, it would be nice if the player's hand was also an instance of Deck
. That way you could apply the power of that class also to the player's hand.
最后,使用 __repr__
方法来控制类的实例如何以字符串格式表示.
Finally, make use of __repr__
methods to control how instances of your classes are represented in string format.
例如:
import random
class Card(object):
def __init__(self, suit, value):
self.suit = suit
self.value = value
def __repr__(self):
return "{} of {}".format(self.value, self.suit)
class Deck(object):
def __init__(self):
self.cards = []
def fill(self):
self.cards = []
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(1, 14):
self.add(Card(s, v))
return self
def __repr__(self):
return ", ".join(repr(card) for card in self.cards)
def shuffle(self):
random.shuffle(self.cards)
return self
def draw(self):
return self.cards.pop()
def add(self, card):
self.cards.append(card)
return self
class Player(object):
def __init__(self, name):
self.name = name
self.hand = Deck()
def draw(self, deck):
self.hand.add(deck.draw())
return self
def __repr__(self):
return "{} has {}".format(self.name, self.hand)
Computer = Player("Computer")
deck = Deck().fill().shuffle()
k = 3
for _ in range(k):
Computer.draw(deck)
print("After drawing {} cards:\n{}".format(k, Computer))
把它变成一个游戏
这里后一种想法用于运行较低/较高"的游戏:
Making it a game
Here the latter idea is used to run a game of "lower/higher":
import random
class Card(object):
def __init__(self, suit, value):
self.suit = suit
self.value = value
def __repr__(self):
return "{} of {}".format(self.value, self.suit)
class Deck(object):
def __init__(self):
self.cards = []
def fill(self):
self.cards = []
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(1, 14):
self.add(Card(s, v))
return self
def __repr__(self):
return ", ".join(repr(card) for card in self.cards)
def size(self):
return len(self.cards)
def shuffle(self):
random.shuffle(self.cards)
return self
def draw(self):
return self.cards.pop()
def add(self, card):
return self.cards.append(card)
class Player(object):
def __init__(self, name):
self.name = name
self.hand = Deck()
def draw(self, deck):
self.hand.add(deck.draw())
return self
def __repr__(self):
return "{} has {}".format(self.name, self.hand)
def play():
deck = Deck().fill().shuffle()
computer = Player("Computer")
computer.draw(deck)
print(str(computer))
while deck.size() > 0:
guess = "?"
while guess not in "hl":
guess = str(input("Will the next card be higher or lower than {} (h or l)?\n".format(computer.hand.cards[-1]))).lower()
computer.draw(deck)
print(str(computer))
diff = computer.hand.cards[-1].value - computer.hand.cards[-2].value
if diff <= 0 and guess == "h" or diff >= 0 and guess == "l":
print("Ah... wrong guess! Game over.")
return
print("That was well guessed!")
print("Unbelievable: you guesses were right for the whole deck!")
play()
这篇关于为什么在这个while循环中为卡片组抽取了同一张卡片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!