-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·198 lines (147 loc) · 5.53 KB
/
main.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/python3
# -----------------------------------------------------------------
# this project is one of the 50 python project challenge.
#
#
#
# Author:N84.
#
# Create Date:Sun Oct 10 17:22:22 2021.
# ///
# ///
# ///
# -----------------------------------------------------------------
# NOTE!!!:
# My English language is not great, don't be surprised if you find something strange or a new word.
from random import randint
from os import system
from os import name as OS_NAME
from time import sleep as delay
# TODO: let the user change the dice char.
# TODO: add colors to the dices and the text in this game using term-color module and add some ascii art.
# TODO: Make the code paradigm oop make a dice object.
def clear():
"""wipe the terminal screen."""
if OS_NAME == "posix":
# for *nix machines.
system("clear")
else:
# for windows machines.
system("cls")
clear()
def welcome_screen():
"""asking the user about the name and getting,
the name also using this function."""
print("(Rolling Dice)".center(120, "-"))
return input("\nEnter your name please: ".strip())
def ask(msg: str):
"""ask the players,if they want to play again, or if they want to exit.
this function will ask question, and depending on that question,
the answer will be "yes" or "no", or 'n', 'y'.
and after that return True if the answer, is "yes", and return False if the answer,
is "no", any thing except "yes", 'y', will consider as "no".
first remove the colon, and then add it.
if the user forget about it we will ad it."""
msg = msg.strip(':')+": "
return True if input(msg).strip().lower() in ("yes", 'y') else False
def dice_generator():
"""this function will generate and print,
random dices."""
# note i use some unicode characters
DICE_C = DICE_CHAR = '●' # dice_char
# note: i draw this rectangle using some special,
# character in other words (unicode) from wikipedia,
# the link:https://en.wikipedia.org/wiki/Box-drawing_character
# create the dice template.
# notice that the dice is function not a string.
# dice is callable-object not a string-object.
dice = """
┌─────────┐
│ {0} {1} {2} │
│ {3} {4} {5} │
│ {6} {7} {8} │
└─────────┘
""".format
# note: any dice_char position will contain only,
# a dice_char or space to fill the place.
# we only have six positions, but if we flip,
# ths number six for example we get two six,
# and the same thing for two and three.
dice_positions = (
# one
(" ", " ", " ", " ", DICE_C, " ", " ", " ", " "),
# two
(" ", " ", DICE_C, " ", " ", " ", DICE_C, " ", " "),
(DICE_C, " ", " ", " ", " ", " ", " ", " ", DICE_C),
# three
(" ", " ", DICE_C, " ", DICE_C, " ", DICE_C, " ", " "),
(DICE_C, " ", " ", " ", DICE_C, " ", " ", " ", DICE_C),
# four
(DICE_C, " ", DICE_C, " ", " ", " ", DICE_C, " ", DICE_C),
# five
(DICE_C, " ", DICE_C, " ", DICE_C, " ", DICE_C, " ", DICE_C),
# six
(DICE_C, " ", DICE_C, DICE_C, " ", DICE_C, DICE_C, " ", DICE_C),
(DICE_C, DICE_C, DICE_C, " ", " ", " ", DICE_C, DICE_C, DICE_C)
)
random_dice = randint(0, len(dice_positions)-1)
return dice(*dice_positions[random_dice])
def dice_dot_count(dice: str):
"""calculate the overall,
dots that show in the dice."""
DICE_CHAR = '●' # dice_char
return dice.count(DICE_CHAR)
def dice_game(dice_count: int = 1, usr_choice=None, usr_name=None):
"""show to the user,
the dice rolling animation."""
period = 0
# keep generate random dice ever randint(1, 50) or for 25 times,
# for 2 seconds.
while period < randint(1, 50):
period += 1
delay(100e-3)
clear()
dices = [dice_generator() for _ in range(dice_count)]
print(*dices)
dices_sum = sum(dice_dot_count(dice) for dice in dices)
# check out if user choice is correct or not.
print(f"your choice:'{usr_choice}', dice sum:'{dices_sum}'")
print(f"Nice choice '{usr_name.capitalize()}'." if usr_choice ==
dices_sum else f"Good luck next time '{usr_name.capitalize()}'.")
def usr_choice_input(msg: str = ""):
"""return int value,
depend on the user input."""
usr_input = input(msg).strip()
try:
usr_input = int(usr_input)
return usr_input if usr_input in range(2, 13) else -1
except ValueError:
# if the user input was string or any type except numbers.
return -1
def usr_input_in_range(usr_choice: int):
"""make sure that the users will give us,
number between 1 to 12 or we will keep,
asking them to enter number between 1 to 12,
and this will also handle other types that,
the user can give for us.
this work as Guard-Condition."""
while usr_choice < 1:
clear()
usr_choice = usr_choice_input(
"Choose only numbers between '2' ~ '12': ")
return usr_choice
def main():
""""""
user_name = welcome_screen()
while ask("Start Rolling the Dices? [Y]es/[N]o: "):
clear()
usr_choice = usr_choice_input("Choose number between 2 and 12: ")
# guard condition.
usr_choice = usr_input_in_range(usr_choice)
dice_game(2, usr_choice=usr_choice, usr_name=user_name)
if ask("\nRolling Again? [Y]es/[N]o, enter[E]xit if you want to quit:"):
continue
else:
break
if __name__ == "__main__":
main()