创建一个纸牌类,并实现随机抽牌的方法,的python代码
时间: 2023-03-10 16:35:30 浏览: 84
这里有一个示例:import randomclass Card:
suits = ["黑桃", "红桃", "梅花", "方片"]
values = [None, None, "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"]
def __init__(self, v, s):
"""suit 和 value 是整数值"""
self.value = v
self.suit = s
def __lt__(self, c2):
if self.value < c2.value:
return True
if self.value == c2.value:
if self.suit < c2.suit:
return True
else:
return False
return False
def __gt__(self, c2):
if self.value > c2.value:
return True
if self.value == c2.value:
if self.suit > c2.suit:
return True
else:
return False
return False
def __repr__(self):
v = self.values[self.value] + " of " + self.suits[self.suit]
return vclass Deck:
def __init__(self):
self.cards = []
for i in range(2, 15):
for j in range(4):
self.cards.append(Card(i, j))
random.shuffle(self.cards)
def draw(self):
if len(self.cards) == 0:
return
return self.cards.pop()
deck = Deck()
for card in deck.cards:
print(card)