-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path扑克牌游戏.py
93 lines (86 loc) · 2.49 KB
/
扑克牌游戏.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import random
class Card(object):
'''第一张牌'''
def __init__(self,suite,face):
self._suite = suite
self._face = face
'''属性只读'''
@property
def face(self):
return self._face
@property
def suite(self):
return self._suite
def __str__(self):
'''返回字符串'''
if self._face == 1:
face_str = 'A'
elif self._face == 11:
face_str = 'J'
elif self._face == 12:
face_str = 'Q'
elif self._face == 13:
face_str = 'K'
else:
face_str = str(self._face)
return '%s %s'%(self._suite, face_str)
def __repr__(self):
return self.__str__()
class Poker(object):
'''一副牌 suite 牌的图案'''
def __init__(self):
self._cards =[Card(suite,face)
for suite in '♠♥♣♦'
for face in range(1,14) ]
self._current = 0
@property
def cards(self):
'''属性 只读,不可修改'''
return self._cards
def shuffle(self):
"""随机洗牌 random.shuffle(list)"""
self._current = 0
random.shuffle(self._cards)
@property
def next(self):
'''从一副牌中 发牌'''
card = self._cards[self._current]
self._current += 1
return card
@property
def has_next(self):
'''还有没有牌 13*4张'''
return self._current < len(self._cards)
class Player(object):
'''玩家'''
def __init__(self,name):
self._name = name
self._cards_on_hand = []
@property
def name(self):
return self._name
@property
def cards_on_hand(self):
return self._cards_on_hand
def get(self,card):
'''摸牌'''
self.cards_on_hand.append(card)
def arrange(self,card_key):
'''整理牌'''
self._cards_on_hand.sort(key=card_key)
def get_key(card):
return (card.suite,card.face)
def main():
p = Poker()
p.shuffle()
players = [Player('东邪'), Player('西毒'),Player('南帝'), Player('北丐')]
'''四个人摸牌,直到摸完,每人13张牌'''
for _ in range(13):
for player in players:
player.get(p.next)
for player in players:
print(player.name + ':',end = ' ')
player.arrange(get_key)
print(player.cards_on_hand)
if __name__ == '__main__':
main()