-
Notifications
You must be signed in to change notification settings - Fork 0
/
session6.py
152 lines (134 loc) · 4.74 KB
/
session6.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import random
vals = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace']
suits = ['spades', 'clubs', 'hearts', 'diamonds']
def get_card_deck_default(kind_list=suits, value_list=vals):
if len(kind_list) != 4:
raise ValueError("kind_list must have exactly 4 entries")
if len(value_list) != 13:
raise ValueError("value_list must have exactly 13 entries")
if kind_list != suits :
raise ValueError("kind_list does not meet expectation")
if value_list != vals:
raise ValueError("value_list does not meet expectation")
deck = []
for k in kind_list:
for v in value_list:
deck.append([k, v])
return deck
def get_card_deck_using_zip(kind_list=suits, value_list=vals):
if len(kind_list) != 4:
raise ValueError("kind_list must have exactly 4 entries")
if len(value_list) != 13:
raise ValueError("value_list must have exactly 13 entries")
if kind_list != suits :
raise ValueError("kind_list does not meet expectation")
if value_list != vals:
raise ValueError("value_list does not meet expectation")
return list([x,*y] for x,y in zip(kind_list, value_list) for y in zip(value_list))
def get_random_cards_from_deck(deck, num_cards):
if num_cards < 3 or num_cards > 5:
raise ValueError("Can not draw less than 3 or more than 5 cards at a time")
cards = random.sample(deck, num_cards)
return cards
def check_royal_flush(cards):
if len(cards) != 5:
return False
unexpected_values = list(filter(lambda x: x[1] not in ['ace', 'king', 'queen', 'jack', '10'], cards))
print(unexpected_values)
if (len(unexpected_values) !=0):
print("Cards contain values < 10")
return False
different_color = list(filter(lambda x: x[0] != cards[0][0], cards))
print(different_color)
if (len(different_color) != 0):
print("Cards contain different colors")
return False
return True
def check_straight_flush(cards):
# TODO
return False
def check_four_of_a_kind(cards):
# TODO
return False
def check_full_house(cards):
if len(cards) < 5:
return False
maps = []
for v in vals:
maps.append(list(filter(lambda x: x[1] == v, cards)))
non_empty_lists = [item for item in maps if item]
if len(non_empty_lists) > 2:
print("Cards of more than two ranks")
return False
if (len(non_empty_lists[0]) == 3 and len(non_empty_lists[1]) == 2):
return True
if (len(non_empty_lists[1]) == 3 and len(non_empty_lists[0]) == 2):
return True
def check_flush(cards):
if len(cards) < 5:
return False
different_kind = list(filter(lambda x: x[0] != cards[0][0], cards))
if (len(different_kind) > 0):
print("Cards of different kind")
return False
return True
def check_straight(cards):
# TODO
return False
def check_three_of_a_kind(cards):
# TODO
return False
def check_two_pairs(cards):
# TODO
return False
def check_on_pair(cards):
#TODO
return False
def check_high_card(cards):
# TODO
return False
def check_poker_winner(player1, player2):
if len(player1) < 2 or len(player1) > 5:
raise ValueError("Player can not have less than 3 or more than 5 cards at a time")
if len(player2) < 2 or len(player2) > 5:
raise ValueError("Player can not have less than 3 or more than 5 cards at a time")
if len(player1) != len(player2):
raise ValueError("Players can not have differnt number of cards")
if (check_royal_flush(player1)):
return player1
elif(check_royal_flush(player2)):
return player2
elif(check_four_of_a_kind(player1)):
return player1
elif(check_four_of_a_kind(player2)):
return player2
elif(check_full_house(player1)):
return player1
elif(check_full_house(player2)):
return player2
elif(check_flush(player1)):
return player1
elif(check_flush(player2)):
return player2
elif(check_straight(player1)):
return player1
elif(check_straight(player2)):
return player2
elif(check_three_of_a_kind(player1)):
return player1
elif(check_three_of_a_kind(player2)):
return player2
elif(check_two_pairs(player1)):
return player1
elif(check_two_pairs(player2)):
return player2
elif(check_one_pair(player1)):
return player1
elif(check_one_pair(player2)):
return player2
elif(check_high_card(player1)):
return player1
elif(check_high_card(player2)):
return player2
else:
return None