forked from JunheeChoi/oop_class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw04_yahtzee.py
57 lines (39 loc) · 1.21 KB
/
hw04_yahtzee.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
# 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"
#1. test code
if __name__ == '__main__':
y_dice = YahtzeeDice()
y_dice.roll_dices([1,2,3,4,5])
print(y_dice)