-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcribbage.py
More file actions
118 lines (101 loc) · 3.62 KB
/
cribbage.py
File metadata and controls
118 lines (101 loc) · 3.62 KB
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
110
111
112
113
114
115
116
117
118
import itertools
import pydealer
class CribbageHand(pydealer.stack.Stack):
"""Class for handling cribbage hands"""
def __init__(self, cards):
self._cards = list(cards)
self.assign_card_values()
self.assign_card_nums()
def __lt__(self, other):
return self.score() < other.score()
def __le__(self, other):
return self.score() <= other.score()
def __eq__(self, other):
return self.score() == other.score()
def __ne__(self, other):
return self.score() != other.score()
def __gt__(self, other):
return self.score() > other.score()
def __ge__(self, other):
return self.score() >= other.score()
def assign_card_nums(self):
for card in self._cards:
if card.value == 'Jack':
card.num = 11
elif card.value == 'Queen':
card.num = 12
elif card.value == 'King':
card.num = 13
elif card.value == 'Ace':
card.num = 1
else:
card.num = int(card.value)
def assign_card_values(self):
for card in self._cards:
if card.value in ['Queen', 'Jack', 'King']:
card.count_value = 10
elif card.value == 'Ace':
card.count_value = 1
else:
card.count_value = int(card.value)
def score(self):
pairs = self.score_pairs()
fifteens = self.score_fifteens()
runs = self.score_runs()
flush = self.score_flush()
knobs = self.score_knobs()
return pairs + fifteens + runs + flush + knobs
def score_pairs(self):
nums = [card.num for card in self._cards]
pairs = len(set([card for card in nums
if nums.count(card) == 2]))
trips = len(set([card for card in nums
if nums.count(card) == 3]))
quads = len(set([card for card in nums
if nums.count(card) == 4]))
pair_score = (pairs * 2) + (trips * 6) + (quads * 12)
return pair_score
def score_fifteens(self):
values = [card.count_value for card in self._cards]
fifteens = 0
for i in range(2, 6):
for seq in itertools.combinations(values, i):
if sum(seq) == 15:
fifteens += 1
return fifteens * 2
def score_runs(self):
values = [card.num for card in self._cards]
threes, fours = 0, 0
for i in range(2, 6):
for seq in itertools.combinations(values, i):
set_seq = set(seq)
is_run = ((max(set_seq) - min(set_seq) + 1) == i
and len(set_seq) == i)
if is_run and len(set_seq) == 5:
return 5
if is_run and len(set_seq) == 4:
fours += 1
if is_run and len(set_seq) == 3:
threes += 1
if fours:
return fours * 4
return threes * 3
def score_knobs(self):
if len(self._cards) == 5:
test_suits = []
crib_suit = self._cards[4].suit
for card in self._cards[:4]:
if card.value == 'Jack':
test_suits.append(card.suit)
if crib_suit in test_suits:
return 1
return 0
def score_flush(self):
test_suit = self._cards[3].suit
for card in self._cards[:3]:
if card.suit != test_suit:
return 0
if len(self._cards) == 5:
if self._cards[4].suit == test_suit:
return 5
return 4