-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
178 lines (123 loc) ยท 4.86 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
# import all required modules
import json
import time
import random
from termcolor import colored
# update_leaderboard
def update_leaderboard(username,wpm,json_file="leaderboard.json"):
try:
with open(json_file,"r") as f:
leaderboard = json.load(f)
except FileNotFoundError: #To handle the Error of empty file
leaderboard = {}
#update leaderboard
leaderboard[username] = wpm
#sort leaderboard
leaderboard = dict(sorted(leaderboard.items(), key=lambda item: item[1], reverse=True))
# leaderboard(python dictionary) --> json (dump)
with open(json_file,"w") as f:
json.dump(leaderboard,f)
def username_exists_or_not(username,json_file="leaderboard.json"):
try:
with open(json_file,"r") as f:
Leaderboard = json.load(f)
except FileNotFoundError: #To handle the Error of empty file
Leaderboard = {}
if username in Leaderboard:
return True
else:
return False
# show_leaderboard
def show_leaderboard():
# load json file
with open("leaderboard.json","r") as file:
leaderboard = json.load(file)
#show the leaderboard in terminal
print()
print(colored("๐ LeaderBoard :- ๐","red"))
print("--------+++------------")
rank = 1
for (username,wpm) in leaderboard.items():
if rank==1:
print(colored(f"{rank}. {username} - {wpm:.2f} WPM :- Winner of The Game ๐","cyan",))
else:
print(colored(f"{rank}. {username} - {wpm:.2f} WPM","green"))
rank+=1
# load_words_from_json
def load_words_from_json_function(file_name="collectionofwords.json"):
with open(file_name,"r") as f:
words = json.load(f)
return words
#calculate wpm
def wpm_measure(random_words):
data_of_user = [] #storing wpm and count of correct words type by user.
start_time = time.time()
typed_words_by_user = [] #storing correct words typed by user
for word in random_words:
print(word,end=" ")
user_typing = input()
if user_typing == word:
typed_words_by_user.append("True")
end_time = time.time()
time_taken_by_user = end_time - start_time
WPM = ( len(typed_words_by_user) / time_taken_by_user )
WPM = WPM * 60 #to convert
correct_words_count = len(typed_words_by_user)
data_of_user.append(WPM)
data_of_user.append(correct_words_count)
data_of_user.append(time_taken_by_user)
return data_of_user
# input_taking_from_user
def input_taken():
print()
print("*โ
*โโโโ*โ
**โ
*โโโโ*โ
**โ
*โโโโ*โ
**โ
*โโโโ*โ
*")
print(colored(" Welcome to Terminal Typing Master ๐ฎ :-","magenta",attrs=["bold"]))
print("*โ
*โโโโ*โ
**โ
*โโโโ*โ
**โ
*โโโโ*โ
**โ
*โโโโ*โ
*")
print()
Username = input("Enter Your Username :- ")
while (username_exists_or_not(Username)) == True:
print("Username is already exists.")
Username = input("Enter Your Username :- ")
while True:
print()
print("Features :- ")
print("-----*------")
print("1. Start Typing Test ")
print("2. Show the LeaderBoard")
print("3. Exit the Game")
choice = input("Select Your Choice :-")
if choice == "1":
user_choice = input("How many Words you want to type(1-9) :- ")
while int(user_choice) < 1 or int(user_choice) > 9:
print(" Invalid Input ๐ฅบ")
user_choice = input("How many Words you want to type(1-9) :- ")
Words = load_words_from_json_function()
count_of_Words = len(list(Words))
random_words = random.sample(list(Words),int(user_choice))
# collect_data
data = wpm_measure(random_words) #data = [wpm,correct_words,time_taken]
# displaying_the_result
print("---------------")
print("Results ๐ :-")
print("--------------")
print()
print(f" Username ==> {Username}")
print()
print(f" Correct Words ==> {data[1]}/{len(random_words)}.")
print(f" Time Taken. ==> {data[2]:.2f} seconds")
print(f" Words Per Minute (WPM) ==> {data[0]:.2f}")
# update in leaderboard
update_leaderboard(Username,data[0])
elif choice == "2":
show_leaderboard()
elif choice == "3":
print("*โ
*โโโโ*โ
**โ
*โโโโ*โ
**โ
*โโโโ*โ
**โ
*โโโโ*โ
*")
print(colored("Thanks For Playing Game.","magenta"))
print()
print(colored(" โค๏ธ From (KISHAN KUMAR)","magenta"))
break
else:
print("Invalid Input ๐ฅบ")
continue
if __name__ == "__main__":
input_taken()