Python 纸牌与纸牌 OOP
Python Card & Deck OOP
我正在尝试编写一个程序:
创建一副纸牌的函数,默认情况下为 52 张,或者在提供花色时仅为 13 张。
和
一个函数,它从一副牌中移除前 card_count 张牌,returns 它们作为一个列表,并且 returns 当没有牌时出现适当的错误。
预期结果如下。
>>> deck2 = Deck('♠')
>>> deck2.shuffle_deck()
>>> print(deck2.cards)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠, J of ♠, 9 of ♠, Q of ♠, 6 of ♠, 2 of ♠, K of ♠]
>>> deck2.deal_card(7)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠]
>>> deck2.deal_card(7)
Cannot deal 7 cards. The deck only has 6 cards left!
我的问题是每次 运行 我的代码都返回一个空列表。我想我已经正确设置了第一个 class (PlayingCard) 和花色的可选参数。但是,我不太确定如何实现交易功能或为什么我的列表返回空。我是不是漏掉了什么小东西?
import random
class PlayingCard():
def __init__(self, rank, suit):
acc_suit = ("♠", "♥", "♦", "♣")
acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
assert rank in acc_rank, "Not a valid rank for a playing card."
assert suit in acc_suit, "Not a valid suit for a playing card."
self.suit = suit
self.rank = rank
def __repr__(self):
return self.rank + ' of ' + self.suit
class Deck():
def __init__(self, *suit):
acc_suit = ("♠", "♥", "♦", "♣")
self.cards = []
if suit == None:
for suit in range(4):
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
if suit in acc_suit:
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
def shuffle_deck(self):
random.shuffle(self.cards)
def deal_card(self):
return self.cards.pop(0)
def __str__(self):
res = []
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
您的列表为空的原因是,如果您提供一套花色,您永远不会创建套牌。打电话
deck2 = Deck('♠')
将导致 suit = ('♠',)
- 一个 1 元组 (包含 1 个字符串),因此它永远不会统计 if suit in acc_suit:
=> self.deck
为空。
还有很多其他错误,使您的代码可运行 - 在内联注释中指出:
import random
class PlayingCard():
def __init__(self, rank, suit):
# you try to create cards by range(2,14) but you assert that suit must
# be 2 to 10 or "J"-"A" for picture cards
# also: range(2,14) is [2..13] only so you miss a card
r = {11:"J",12:"Q",13:"K",14:"A"}
rank = r.get(rank,rank) # translate 10 to 14 to "JQMA"
acc_suit = ("♠", "♥", "♦", "♣")
acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
assert rank in acc_rank, "Not a valid rank for a playing card."
assert suit in acc_suit, "Not a valid suit for a playing card."
self.suit = suit
self.rank = rank
def __repr__(self):
# can not add a string to an integer (rank is an int for 2-10 !)
return f"{self.rank} of {self.suit}"
和你的套牌class:
class Deck():
# change this to a named param with default so you need to supply suit=' '
# omit the ,* if you do not like to force suit=" " when calling this
def __init__(self, *, suit = None):
acc_suit = ("♠", "♥", "♦", "♣")
self.cards = []
if not suit:
for suit in range(4):
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
if suit in acc_suit:
for rank in range (2,15):
card = PlayingCard(rank, suit)
self.cards.append(card)
def shuffle_deck(self):
random.shuffle(self.cards)
# this function does not accept parameters like you posted its usage
def deal_card(self, amount = 1):
# always returns a list of cards, even if you only request one
rv, self.cards = self.cards[:amount], self.cards[amount:]
# this does NOT do the evaluation of enough cards are left, do it yourself:
# if len(rv) != amount: raise whatever
return rv
def __str__(self):
return '\n'.join(str(c) for c in self.cards)
测试:
deck2 = Deck(suit = "♠")
print(deck2.cards)
deck2.shuffle_deck()
print(deck2.cards)
print(deck2.deal_card(7))
print(deck2.deal_card(7))
输出:
# orignal
[2 of ♠, 3 of ♠, 4 of ♠, 5 of ♠, 6 of ♠, 7 of ♠, 8 of ♠, 9 of ♠, 10 of ♠,
J of ♠, Q of ♠, K of ♠, A of ♠]
# shuffeled
[3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠, 4 of ♠, J of ♠,
Q of ♠, 9 of ♠, 2 of ♠, K of ♠]
# first 7
[3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠]
# next 7 (only 6 remain)
[4 of ♠, J of ♠, Q of ♠, 9 of ♠, 2 of ♠, K of ♠]
我正在尝试编写一个程序:
创建一副纸牌的函数,默认情况下为 52 张,或者在提供花色时仅为 13 张。
和
一个函数,它从一副牌中移除前 card_count 张牌,returns 它们作为一个列表,并且 returns 当没有牌时出现适当的错误。
预期结果如下。
>>> deck2 = Deck('♠')
>>> deck2.shuffle_deck()
>>> print(deck2.cards)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠, J of ♠, 9 of ♠, Q of ♠, 6 of ♠, 2 of ♠, K of ♠]
>>> deck2.deal_card(7)
[A of ♠, 10 of ♠, 3 of ♠, 7 of ♠, 5 of ♠, 4 of ♠, 8 of ♠]
>>> deck2.deal_card(7)
Cannot deal 7 cards. The deck only has 6 cards left!
我的问题是每次 运行 我的代码都返回一个空列表。我想我已经正确设置了第一个 class (PlayingCard) 和花色的可选参数。但是,我不太确定如何实现交易功能或为什么我的列表返回空。我是不是漏掉了什么小东西?
import random
class PlayingCard():
def __init__(self, rank, suit):
acc_suit = ("♠", "♥", "♦", "♣")
acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
assert rank in acc_rank, "Not a valid rank for a playing card."
assert suit in acc_suit, "Not a valid suit for a playing card."
self.suit = suit
self.rank = rank
def __repr__(self):
return self.rank + ' of ' + self.suit
class Deck():
def __init__(self, *suit):
acc_suit = ("♠", "♥", "♦", "♣")
self.cards = []
if suit == None:
for suit in range(4):
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
if suit in acc_suit:
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
def shuffle_deck(self):
random.shuffle(self.cards)
def deal_card(self):
return self.cards.pop(0)
def __str__(self):
res = []
for card in self.cards:
res.append(str(card))
return '\n'.join(res)
您的列表为空的原因是,如果您提供一套花色,您永远不会创建套牌。打电话
deck2 = Deck('♠')
将导致 suit = ('♠',)
- 一个 1 元组 (包含 1 个字符串),因此它永远不会统计 if suit in acc_suit:
=> self.deck
为空。
还有很多其他错误,使您的代码可运行 - 在内联注释中指出:
import random
class PlayingCard():
def __init__(self, rank, suit):
# you try to create cards by range(2,14) but you assert that suit must
# be 2 to 10 or "J"-"A" for picture cards
# also: range(2,14) is [2..13] only so you miss a card
r = {11:"J",12:"Q",13:"K",14:"A"}
rank = r.get(rank,rank) # translate 10 to 14 to "JQMA"
acc_suit = ("♠", "♥", "♦", "♣")
acc_rank = (2, 3, 4, 5, 6, 7, 8, 9, 10, "J", "Q", "K", "A")
assert rank in acc_rank, "Not a valid rank for a playing card."
assert suit in acc_suit, "Not a valid suit for a playing card."
self.suit = suit
self.rank = rank
def __repr__(self):
# can not add a string to an integer (rank is an int for 2-10 !)
return f"{self.rank} of {self.suit}"
和你的套牌class:
class Deck():
# change this to a named param with default so you need to supply suit=' '
# omit the ,* if you do not like to force suit=" " when calling this
def __init__(self, *, suit = None):
acc_suit = ("♠", "♥", "♦", "♣")
self.cards = []
if not suit:
for suit in range(4):
for rank in range (1,14):
card = PlayingCard(rank, suit)
self.cards.append(card)
if suit in acc_suit:
for rank in range (2,15):
card = PlayingCard(rank, suit)
self.cards.append(card)
def shuffle_deck(self):
random.shuffle(self.cards)
# this function does not accept parameters like you posted its usage
def deal_card(self, amount = 1):
# always returns a list of cards, even if you only request one
rv, self.cards = self.cards[:amount], self.cards[amount:]
# this does NOT do the evaluation of enough cards are left, do it yourself:
# if len(rv) != amount: raise whatever
return rv
def __str__(self):
return '\n'.join(str(c) for c in self.cards)
测试:
deck2 = Deck(suit = "♠")
print(deck2.cards)
deck2.shuffle_deck()
print(deck2.cards)
print(deck2.deal_card(7))
print(deck2.deal_card(7))
输出:
# orignal
[2 of ♠, 3 of ♠, 4 of ♠, 5 of ♠, 6 of ♠, 7 of ♠, 8 of ♠, 9 of ♠, 10 of ♠,
J of ♠, Q of ♠, K of ♠, A of ♠]
# shuffeled
[3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠, 4 of ♠, J of ♠,
Q of ♠, 9 of ♠, 2 of ♠, K of ♠]
# first 7
[3 of ♠, 5 of ♠, 10 of ♠, 8 of ♠, A of ♠, 7 of ♠, 6 of ♠]
# next 7 (only 6 remain)
[4 of ♠, J of ♠, Q of ♠, 9 of ♠, 2 of ♠, K of ♠]