-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyahtzee_hand.py
183 lines (159 loc) · 5.49 KB
/
yahtzee_hand.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class Hand:
import time
def __init__(self):
self.hand = self.dieRoll(5)
def main():
x = True
while x:
start_time = time.time()
for x in range(0,1000):
hand = dieRoll(5)
hand.sort()
if is3ofaKind(hand):
print("3 of a Kind {} - {}".format(x,hand))
if is4ofaKind(hand):
print("4 of a Kind {} - {}".format(x,hand))
if isFullHouse(hand):
print("Full House {} - {}".format(x,hand))
if isLargeStraight(hand):
print("Large Straight {} - {}".format(x,hand))
if isSmallStraight(hand):
print("Small Straight {} - {}".format(x,hand))
if isYahtzee(hand):
print("Yahtzee!! {} - {}".format(x,hand))
print("time={}".format(time.time()-start_time))
x = False
def sortHand(self):
self.hand.sort()
def discard(self, discard_list):
looper = list(range(0,5))
looper.reverse()
for x in looper:
if discard_list[x]:
del(self.hand[x])
new_roll = self.dieRoll(5-len(self.hand))
self.hand += new_roll
def showHand(self):
return list(self.hand)
def sumHand(self, hand):
"""Sums all dice in hand for socring chance, 3 of a Kind, or 4 of a Kind
Parameters:
hand (list): List of integers that represent a hand of dice
Returns:
int: Sum of list of integers
"""
handSum = 0
for x in hand:
handSum += x
return handSum
def dieRoll(self, dieCount):
"""Returns maximum of 5 random integers from 1-6 as a list.
Parameters:
dieCount (int): The number of dice required to be returned
Returns:
list(ints[]): List of dice
"""
import random
#2 checks to make sure the inpute is a positive integer
if type(dieCount) != int:
raise TypeError('dieCount must be a positive integer =< 5')
if dieCount < 1 or dieCount > 5:
raise ValueError('dieCount must be a positive integer =< 5')
#empty return set
roll = []
i=0
#For each 'Die' append a random int from 1-6 to the list
while i < dieCount:
roll.append(random.randint(1,6))
i += 1
#return list object with random numbers
return roll
def isFullHouse(self):
"""Checks if the current Hand object matches criteria for a Full House and returns a bool
Returns:
bool: The Hand is a Full House
"""
hand = self.hand
if len(hand) != 5:
return False
count_unique = set()
for x in hand:
count_unique.add(x)
if len(count_unique) == 2:
y = next(iter(count_unique))
if hand.count(y) == 2 or hand.count(y) == 3:
return True
return False
def isLargeStraight(self):
"""Checks if the current Hand object matches criteria for a Large Straight and returns a bool
Returns:
bool: The Hand is a Large Straight
"""
hand = self.hand
if len(hand) != 5:
return False
hand.sort()
count_unique = set()
for x in hand:
count_unique.add(x)
if len(count_unique) == 5:
if hand[4] - hand[0] == 4:
return True
return False
def isYahtzee(self):
"""Checks if the current Hand object matches criteria for a Yahtzee and returns a bool
Returns:
bool: The Hand is a Yahtzee
"""
hand = self.hand
if len(hand) != 5:
return False
count_unique = set()
for x in hand:
count_unique.add(x)
if len(count_unique) == 1:
return True
return False
def isSmallStraight(self):
"""Checks if the current Hand object matches criteria for a Small Straight and returns a bool
Returns:
bool: The Hand is a Small Straight
"""
hand = self.hand
if len(hand) != 5:
return False
count_unique = set()
for x in hand:
count_unique.add(x)
if len(count_unique) < 4:
return False
if hand.count(3) and hand.count(4):
if hand.count(5) and (hand.count(2) or hand.count(6)):
return True
if hand.count(2) and (hand.count(1) or hand.count(5)):
return True
return False
def is3ofaKind(self):
"""Checks if the current Hand object matches criteria for a 3 of a Kind and returns a bool
Returns:
bool: The Hand is a 3 of a Kind
"""
hand = self.hand
if len(hand) != 5:
return False
if hand.count(hand[0]) >= 3 or hand.count(hand[1]) >= 3 or hand.count(hand[2]) >= 3:
return True
return False
def is4ofaKind(self):
"""Checks if the current Hand object matches criteria for a 4 of a Kind and returns a bool
Returns:
bool: The Hand is a 4 of a Kind
"""
hand = self.hand
if len(hand) != 5:
return False
if hand.count(hand[0]) >= 4 or hand.count(hand[1]) >= 4:
return True
return False
if __name__ == "__main__":
main()