python/公开课/文档/年薪50W+的Python程序员如何写代码/code/Python/opencourse/part03/example.py
2024-12-04 00:04:56 +08:00

61 lines
1.1 KiB
Python

"""
扑克
"""
import enum
import random
@enum.unique
class Suite(enum.Enum):
"""花色(枚举)"""
SPADE, HEART, CLUB, DIAMOND = range(4)
class Card:
""""""
def __init__(self, suite, face):
self.suite = suite
self.face = face
def __repr__(self):
suites = '♠♥♣♦'
faces = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
return f'{suites[self.suite.value]}{faces[self.face]}'
class Poker:
"""扑克"""
def __init__(self):
self.cards = [Card(suite, face) for suite in Suite
for face in range(1, 14)]
self.current = 0
def shuffle(self):
"""洗牌"""
self.current = 0
random.shuffle(self.cards)
def deal(self):
"""发牌"""
card = self.cards[self.current]
self.current += 1
return card
@property
def has_next(self):
"""还有没有牌可以发"""
return self.current < len(self.cards)
def main():
"""主函数(程序入口)"""
poker = Poker()
poker.shuffle()
print(poker.cards)
if __name__ == '__main__':
main()