-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rolling Game
209 lines (136 loc) · 6.4 KB
/
Rolling Game
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
## Name: Jaewon Son
## Date: October 10 2023
## Honor Statement: I have not given or received any unauthorized assistance on this assignment.
## Link: https://youtu.be/opiRLSolpdI
import random
from JaewonSon_Assignment_0501_Dice_and_Cups import Cup
def greet_user():
"""Greet to a user.
"""
print("\nWelcome to the Rolling Game!")
def ask_name():
"""Ask a username.
Returns:
str: Returns a username that will be used for the Rolling Game.
"""
user_name = input("\nPlease enter you name: ")
return user_name
def whether_play_first():
"""Asking a user whether to play the Rolling Game or not.
Returns:
int: Returns 1 when a user is going to play, else returns 0.
"""
while True:
whether_play = input("\nWould you like to participate in the Rolling Game? ['1': Yes / '0': No]: ")
if whether_play == '0': # 0 means a user is not going to play the game
return 0
try:
whether_play = eval(whether_play)
assert type(whether_play) == int # Checking an input is whether valid or not
assert int(whether_play) == 1
return whether_play
except:
print("\nInvalid input. Choose '1' or '0'.")
def whether_play_again():
"""Asking a user whether to play the Rolling Game again or not.
Returns:
int: Returns 1 when a user is going to play again, else returns 0.
"""
while True:
whether_play = input("\nWould you like to play the Rolling Game again? ['1': Yes / '0': No]: ")
if whether_play == '0': # 0 means a user is not going to play the game
return 0
try:
whether_play = eval(whether_play)
assert type(whether_play) == int # Checking an input is whether valid or not
assert int(whether_play) == 1
return whether_play
except:
print("\nInvalid input. Choose '1' or '0'.")
def goal():
"""Generate a random number between 1 to 100 which will be used for the Rolling Game.
Returns:
int: Returns any random number between 1 to 100.
"""
goal = random.randint(1, 101) # Random number generator from 1 to 100
return goal
def number_of_six_sided_dice():
"""Asking a user how many dice would like to roll.
Returns:
int: Returns a number entered by a user.
"""
while True:
first_dice = input("\nHow many six-sided dice would you like to roll?: ")
try:
first_dice = eval(first_dice)
assert type(first_dice) == int # Checking an input is whether valid or not
assert first_dice >= 0
return first_dice
except:
print("\nInvalid input. Please enter again.")
def number_of_ten_sided_dice():
"""Asking a user how many dice would like to roll.
Returns:
int: Returns a number entered by a user.
"""
while True:
second_dice = input("\nHow many ten-sided dice would you like to roll?: ")
try:
second_dice = eval(second_dice)
assert type(second_dice) == int # Checking an input is whether valid or not
assert second_dice >= 0
return second_dice
except:
print("\nInvalid input. Please enter again.")
def number_of_twenty_sided_dice():
"""Asking a user how many dice would like to roll.
Returns:
int: Returns a number entered by a user.
"""
while True:
third_dice = input("\nHow many twenty-sided dice would you like to roll?: ")
try:
third_dice = eval(third_dice)
assert type(third_dice) == int # Checking an input is whether valid or not
assert third_dice >= 0
return third_dice
except:
print("\nInvalid input. Please enter again.")
def rolling_game():
"""Main function for the Rolling Game.
"""
greet_user()
user_name = ask_name()
play_or_not = whether_play_first()
rolling_game_score = 0 # Starting game score
while True:
if play_or_not == 0: # Exit the program
break
else:
target_point = goal() # Target value
print(f"\n{user_name}, your goal is to get as close as possible to the {target_point}. Good luck!")
six_sided_dice = number_of_six_sided_dice() # The number of 6-sided dice a user wants to roll
ten_sided_dice = number_of_ten_sided_dice() # The number of 10-sided dice a user wants to roll
twenty_sided_dice = number_of_twenty_sided_dice() # The number of 20-sided dice a user wants to roll
rolling_game = Cup(six_sided_dice, ten_sided_dice, twenty_sided_dice) # Creates a cup filled with dice according to a user's input
rolling_game.roll() # Roll the cup
point = rolling_game.get_sum()
print(f"\nThe result of Dice and Cup is {point}: ", rolling_game) # Report the result to a user
if target_point == point: # A condition for updating the rolling_game_score
rolling_game_score += 10 # Update rolling_game_score
print("\nPerferct! You got 10 points.")
elif 0 <= target_point - point <= 3: # A condition for updating the rolling_game_score
rolling_game_score += 5 # Update rolling_game_score
print("\nNice! You got 5 points.")
elif 0 <= target_point - point <= 10: # A condition for updating the rolling_game_score
rolling_game_score += 2 # Update rolling_game_score
print("\nNot bad! You got 2 points.")
else: # When the value difference is more than 10 or the point is larger than target_point
rolling_game_score += 0
print("\nOops! You got 0 point.")
print(f"\n{user_name}, your current score is {rolling_game_score}!") # Report the result to a user
if whether_play_again() == 0: # Exit the program
break
print(f"\nThank you for playing the Rolling Game, {user_name}! Your final score is {rolling_game_score}.\n") # Report the result to a user
if __name__ == "__main__":
rolling_game()