我打算创建称为order的此方法,该方法需要以下功能。
对列表或排序的卡片组进行排序时,它从最低到最高为2C(三叶草中的2个)。
import random
class Card(object):
def __init__(self, num, suit):
self.num = num
self.suit = suit
er.num) 返回t1 == t2
def num_rank(num):
if num[0] == "A":
return 14
if num[0] == "J":
return 11
if num[0] == "Q":
return 12
if num[0] == "K":
return 13
return int(num)
class Deck(object):
def __init__
self.m for s in self.suit]
def isOrdered(self):
if self. str('2C'):
return True
答案 0 :(得分:2)
您可以将self.deck
和sorted(self.deck)
中的列表进行比较。如果它们相等,则对牌组进行排序:
from functools import total_ordering
@total_ordering
class Card(object):
def __init__(self, num, suit):
self.num = num
self.suit = suit
def __str__(self):
return '%s%s' % (self.num,
self.suit)
def __repr__(self): return str(self)
def __lt__(self, other):
t1 = self.suit, self.num_rank
t2 = other.suit, other.num_rank
return t1 < t2
def __eq__(self, other):
t1 = self.suit, self.num_rank
t2 = other.suit, other.num_rank
return t1 == t2
@property
def num_rank(self):
if self.num[0] == "A":
return 14
if self.num[0] == "J":
return 11
if self.num[0] == "Q":
return 12
if self.num[0] == "K":
return 13
return int(self.num)
class Deck(object):
def __init__(self):
self.num = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
self.suit = ['C', 'D', 'H', 'S']
self.deck = [Card(r, s) for r in self.num for s in self.suit]
def isOrdered(self):
print('My deck :', self.deck)
print('My sorted deck :', sorted(self.deck))
return self.deck == sorted(self.deck)
d = Deck()
print('Deck.isOrdered() ==', d.isOrdered())
打印:
My deck : [2C, 2D, 2H, 2S, 3C, 3D, 3H, 3S, 4C, 4D, 4H, 4S, 5C, 5D, 5H, 5S, 6C, 6D, 6H, 6S, 7C, 7D, 7H, 7S, 8C, 8D, 8H, 8S, 9C, 9D, 9H, 9S, 10C, 10D, 10H, 10S, JC, JD, JH, JS, QC, QD, QH, QS, KC, KD, KH, KS, AC, AD, AH, AS]
My sorted deck : [2C, 3C, 4C, 5C, 6C, 7C, 8C, 9C, 10C, JC, QC, KC, AC, 2D, 3D, 4D, 5D, 6D, 7D, 8D, 9D, 10D, JD, QD, KD, AD, 2H, 3H, 4H, 5H, 6H, 7H, 8H, 9H, 10H, JH, QH, KH, AH, 2S, 3S, 4S, 5S, 6S, 7S, 8S, 9S, 10S, JS, QS, KS, AS]
Deck.isOrdered() == False
注意:
我使用了functools.total_ordering
(doc),因此只需__eq__
和__lt__
即可实现
通过num_rank
装饰器制作@property
属性
排序现在通过(suit, num_rank)
进行-这就是__eq__
和__lt__
的定义方式。也许应该考虑参数化isOrdered()
-isOrdered(by suit or by num etc...)
答案 1 :(得分:0)
因此您可能需要做的是在卡座中循环浏览,看看是否有任何卡出现故障。换句话说,
in_order = True
for c in range(len(self.deck)-1):
if self.deck[c] > self.deck[c+1]:
in_order = False
break
return in_order
答案 2 :(得分:0)
如果必须实现这一点,我会将所有卡都放置为数字,这样可以简化比较,并在必要时使用__str__
函数将数字转换为名称。在卡座对象中,不需要num
或rank
。
# only the relevant code portion
from itertools import product
FIGURES = {11: 'J', 12: 'Q', 13: 'K', 14: 'A'}
class Deck(object):
def __init__(self):
self.deck = [Card(r, s) for r, s in product(range(2, 15), ['C', 'D', 'H', 'S'])]
def isOrdered(self):
print('My deck :', self.deck)
print('My sorted deck :', sorted(self.deck))
return self.deck == sorted(self.deck)
class Card(object):
def __init__(self, num, suit):
self.num = num
self.suit = suit
def __str__(self):
if num < 11:
return '%s%s' % (self.num, self.suit)
else:
return '%s%s' % (FIGURES[self.num], self.suit)
def __repr__(self): return str(self)
# with number only comparison are easier
def __lt__(self, other):
return (self.suit, self.num) < (other.suit, other.num)
def __gt__(self, other):
return (self.suit, self.num) > (other.suit, other.num)
def __eq__(self, other):
return (self.suit, self.num) == (other.suit, other.num)
PS:如果您使用的是python 3,则对象的继承不是强制性的。