CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/公开课/文档/年薪50W+的Python程序员如何写代码/code/Python/opencourse/part03/example.py
Views: 729
1
"""
2
扑克
3
"""
4
import enum
5
import random
6
7
8
@enum.unique
9
class Suite(enum.Enum):
10
"""花色(枚举)"""
11
SPADE, HEART, CLUB, DIAMOND = range(4)
12
13
14
class Card:
15
"""牌"""
16
17
def __init__(self, suite, face):
18
self.suite = suite
19
self.face = face
20
21
def __repr__(self):
22
suites = '♠♥♣♦'
23
faces = ['', 'A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
24
return f'{suites[self.suite.value]}{faces[self.face]}'
25
26
27
class Poker:
28
"""扑克"""
29
30
def __init__(self):
31
self.cards = [Card(suite, face) for suite in Suite
32
for face in range(1, 14)]
33
self.current = 0
34
35
def shuffle(self):
36
"""洗牌"""
37
self.current = 0
38
random.shuffle(self.cards)
39
40
def deal(self):
41
"""发牌"""
42
card = self.cards[self.current]
43
self.current += 1
44
return card
45
46
@property
47
def has_next(self):
48
"""还有没有牌可以发"""
49
return self.current < len(self.cards)
50
51
52
def main():
53
"""主函数(程序入口)"""
54
poker = Poker()
55
poker.shuffle()
56
print(poker.cards)
57
58
59
if __name__ == '__main__':
60
main()
61
62