-
Notifications
You must be signed in to change notification settings - Fork 1
/
hw05_yahtzee_rules.py
183 lines (134 loc) · 4.44 KB
/
hw05_yahtzee_rules.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
# Yahtzee Game
import random
# Die class
class Die:
def __init__(self):
self.face = None
def roll(self):
self.face = random.randint(1, 6)
def __str__(self):
return str(self.face)
# Yahtzee Class
class YahtzeeDice:
def __init__(self):
self.dices = [Die() for _ in range(5)]
def roll_dices(self, dices):
for index in dices:
self.dices[index-1].roll()
def show_faces(self):
for index, die in enumerate(self.dices):
print(f"주사위 {index+1} 눈: {die.face}")
print()
def get_faces(self):
return [die.face for die in self.dices]
def count_faces(self, target_face):
count = 0
for die in self.dices:
if die.face == target_face:
count += 1
return count
def sum_faces(self):
return sum(self.get_faces())
def __str__(self):
return ', '.join([str(die.face) for die in
self.dices]) + f"\nCounts: {', '.join(str(self.count_faces(i)) for i in range(1, 6))}\nSum: {self.sum_faces()}\n"
from abc import ABC, abstractmethod
from abc import ABC, abstractmethod
class Rule(ABC):
def __init__(self, name):
self.name = name
@abstractmethod
def points(self, dice):
pass
def __str__(self):
return self.name
class SameValue(Rule):
def __init__(self, name, value):
super().__init__(name)
self.value = value
def points(self, dice):
return sum(face for face in dice.get_faces() if face == self.value)
def __str__(self):
return f"{self.name} ({self.value})"
class ThreeOfAKind(Rule):
def __init__(self):
super().__init__("Three of a Kind")
def points(self, dice):
counts = dice.get_faces()
for face in counts:
if counts.count(face) >= 3:
return sum(dice.get_faces())
return 0
def __str__(self):
return self.name
class FourOfAKind(Rule):
def __init__(self):
super().__init__("Four of a Kind")
def points(self, dice):
counts = dice.get_faces()
for face in counts:
if counts.count(face) >= 4:
return sum(dice.get_faces())
return 0
def __str__(self):
return self.name
class FullHouse(Rule):
def __init__(self):
super().__init__("Full House")
def points(self, dice):
counts = dice.get_faces()
if len(set(counts)) == 2 and (counts.count(counts[0]) == 3 or counts.count(counts[0]) == 2):
return 25
return 0
def __str__(self):
return self.name
class Yahtzee(Rule):
def __init__(self):
super().__init__("Yahtzee")
def points(self, dice):
counts = dice.get_faces()
if counts.count(counts[0]) == 5:
return 50
return 0
def __str__(self):
return self.name
class Chance(Rule):
def __init__(self):
super().__init__("Chance")
def points(self, dice):
return sum(dice.get_faces())
def __str__(self):
return self.name
class Straight(Rule):
def __init__(self, name, straight):
super().__init__(name)
self.straight = straight
def is_straight(self, faces):
faces.sort()
return faces == self.straight
def points(self, dice):
if self.is_straight(dice.get_faces()):
return sum(dice.get_faces())
return 0
def __str__(self):
return self.name
class SmallStraight(Straight):
def __init__(self):
super().__init__("Small Straight", [1, 2, 3, 4, 5])
def __str__(self):
return self.name
class LargeStraight(Straight):
def __init__(self):
super().__init__("Large Straight", [2, 3, 4, 5, 6])
def __str__(self):
return self.name
#1. test code
## rules = [Rule]
if __name__ == '__main__':
y_dice = YahtzeeDice()
y_dice.roll_dices([1, 2, 3, 4, 5])
print(y_dice)
rules = [SameValue('Aces', 1), SameValue('Twos', 2), SameValue('Threes', 3), SameValue('Fours', 4), SameValue('Fives', 5), SameValue('Sixes', 6),
ThreeOfAKind(), FourOfAKind(), FullHouse(), Yahtzee(), Chance(), SmallStraight(), LargeStraight()]
for rule in rules:
print(f"{rule}: {rule.points(y_dice)} points")