-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (137 loc) · 4.52 KB
/
app.py
File metadata and controls
149 lines (137 loc) · 4.52 KB
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
import random, time, math
settings = {
"min": 0, # Default minimu
"max": 10, # Default maximum
"tries": 5, # Default maximum tries
"caracters": 120 # Change this to adapt the maximal length of displayed messages
}
def displayMessage(message, returnToTheLine = False):
if message:
message = " " + message + " "
if returnToTheLine:
print('')
numberOfStars = math.floor((settings["caracters"] - len(message)) / 2)
for i in range(numberOfStars):
print("*", end="")
if message:
print(message,end="")
for i in range(numberOfStars):
print("*", end="")
print('')
displayMessage('', True)
displayMessage('randomNumberGame')
displayMessage('')
def showMenu(preChoice = 0, showMenuV = True):
choice = 0
if preChoice == 0:
if showMenuV:
displayMessage('MENU', True)
displayMessage('')
displayMessage('1 - Play | 2 - Settings | 3 - Credits | 4 - Exit')
choice = displayInput()
if isNumber(choice): choice = int(choice)
else :
print("⚠ [WARNING] - The number must be 1 or 2\n")
return showMenu(0, False)
if not preChoice == 2 and choice == 1:
secretNumberGame(settings["min"],settings["max"])
if choice == 2 or preChoice == 2:
displayMessage("SETTINGS", True)
displayMessage('1 - Minimum | 2 - Maximum | | 3 - Tries | 4 - Go Back')
choice1 = displayInput()
if choice1 == "1":
newMin = input("New minimum: ")
if isNumber(newMin): newMin = int(newMin)
else:
print("⚠ [WARNING] - Minimal value must be a number !\n")
showMenu(2)
if newMin < settings["max"]:
settings["min"] = newMin
else:
print("The minimum can't be bigger than the maximum !")
showMenu(2)
return
showMenu()
if choice1 == "2":
newMax = input("New maximum: ")
if isNumber(newMax): newMax = int(newMax)
else:
print("⚠ [WARNING] - Maximal value must be a number !\n")
showMenu(2)
if newMax > settings["min"]:
settings["max"] = newMax
else:
print("The maximum can't be smaller than the minimum !")
showMenu(2)
return
showMenu()
if choice1 == "3":
newTriesMax = input("New tries maximum: ")
if isNumber(newTriesMax): newTriesMax = int(newTriesMax)
else:
print("⚠ [WARNING] - Maximal tries value must be a number !\n")
showMenu(2)
if newTriesMax < 0:
return print("⚠ [WARNING] - Maximal tries value can't be negative !\n")
else:
settings["tries"] = newTriesMax
return showMenu()
if choice1 == "4":
return showMenu()
else:
return showMenu(2)
if choice == 3:
message = "\n***************************************************************\n"
message += "********** Made by Nostlix (https://github.com/Nostlix) *******\n"
message += "*** Repository: https://github.com/Nostlix/randomNumberGame ***\n"
message += "***************************************************************\n"
writeSlowly(message)
return showMenu()
if choice == 4:
message = "\n***************************************************************\n"
message += "***************** Thanks for playing ! ❤ ❤ ❤ ****************\n"
message += "***************************************************************"
writeSlowly(message)
exit()
else:
showMenu()
def secretNumberGame(min, max):
numberToFind = random.randint(min , max)
print("\nThe number is between",min,"and", max, "!")
totalTries = 0
def tryToGetTheNumber(totalTries):
if totalTries == settings["tries"]:
print("\nYou used all your tries !...")
return showMenu()
n = input("\nYour choice: ")
if n == "000":
print("You gave up..")
return showMenu()
if isNumber(n): n = int(n)
else:
print("You choice wasn't a number !")
return tryToGetTheNumber(totalTries)
totalTries += 1
if n == numberToFind:
print("You won in",totalTries,"tries !")
showMenu()
else:
print("You failed... try again (attempt",str(totalTries)+"/",str(settings["tries"])+") ! | Write '000' to give up...")
if numberToFind > n: print("Tip: The number is bigger :)")
elif numberToFind < n: print("Tip: The number is smaller :D")
if totalTries + 1 == settings["tries"]:
writeSlowly("\nBe careful ! LAST TRY\n")
tryToGetTheNumber(totalTries)
tryToGetTheNumber(totalTries)
def writeSlowly(message):
for i in message:
print(i, end = "")
time.sleep(0.001)
time.sleep(1)
def isNumber(string):
if string[0] == '-':
string = str(string).replace('-', '')
return(string.isnumeric() == True)
def displayInput():
return input('---> ')
showMenu()