-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcard.py
109 lines (78 loc) · 2.96 KB
/
card.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from copy import copy
import numpy as np
class BaseCard(object):
"""docstring for Card"""
CARD_NUMBER = xrange(2, 15)
CARD_SHAPE = ['H', 'S', 'C', 'D'] # H(红心), S(黑桃), C(草花), D(方块)
PIG = ('S', 12)
STAR = ('H', 2) # - H14
def __init__(self):
super(Card, self).__init__()
self.num_func = lambda x: x[1]
self._cards_db = []
for num in self.CARD_NUMBER:
for shape in self.CARD_SHAPE:
self._cards_db.append((shape, num))
self._cards_db = np.array(self._cards_db)
class PlayerCards(BaseCard):
"""docstring for PlayerCard"""
def __init__(self, cards_list):
super(PlayerCard, self).__init__()
self.raw_cards_list = cards_list
self.FIRST_MASTER = ('C', 2)
self.ONSHOWN = [('H', 14), ('S', 12), ('C', 10)] #
self.current_cards_list = copy(self.raw_cards_list)
self._flush()
self.onshown_cards_list = []
def _flush(self):
self.current_cards_list.sort(key=self.num_func)
# get current dict
self.current_cards_dict = {
'H': [],
'S': [],
'C': [],
'D': []
}
for shape, num in self.current_cards_list:
self.current_cards_dict[shape].append((shape, num))
for shape in self.current_cards_dict:
self.current_cards_dict[shape].sort(self.num_func)
# get length dict
self.current_length_list = [(shape, len(self.current_length_dict[shape])) for shape in self.current_cards_dict]
self.current_length_list.sort(self.num_func)
def drop_one_card(self, card):
self.current_cards_list.remove(card)
self._flush()
def drop_cards(self, cards):
for card in cards:
self.current_cards_list.remove(card)
self._flush()
def add_one_card(self, card):
self.current_cards_list.append(card)
self._flush()
def add_cards(self, cards):
self.current_cards_list.extend(cards)
self._flush()
def is_first_master(self):
return True if self.FIRST_MASTER in self.current_cards_list else False
def cards_can_show(self):
return [card for card in self.ONSHOWN if card in self.current_cards_list]
def add_shown_cards(self, cards):
self.onshown_cards_list.extend(cards)
def get_opposite_cards(self, cards_list):
cards_opposite = copy(self._cards_db)
for card in cards_list:
cards_opposite.remove(card)
return cards_opposite
class MasterCards(BaseCard):
"""docstring for MasterCards"""
def __init__(self):
super(MasterCards, self).__init__()
def deal(self, num_of_player=4):
np.random.shuffle(self._cards_db)
self.dealed_cards = np.split(self._cards_db, num_of_player)
def output_cards(self, round):
"""
round: 1-4
"""
return self.dealed_cards[round:] + self.dealed_cards[:round]