-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
executable file
·266 lines (182 loc) · 6.62 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
""" importing required modules """
from random import choice
import sqlite3
""" making connection to the local database """
db = sqlite3.connect("movieman.db")
mc = db.cursor()
# fetching all the distinct genres from the database
mc.execute("select distinct genre from movies order by genre asc")
genres = mc.fetchall()
newGen = []
# storing the genres in the newGen variable
for x in genres:
newGen.append(x[0])
# fetching all the distinct genres from the database
mc.execute("select distinct language from movies order by language asc")
languages = mc.fetchall()
newLang = []
# storing the languages in the newLang variable
for x in languages:
newLang.append(x[0])
""" Displaying the user a choice before starting the game """
print("1. Completely random movie.")
print("2. Movie from specific genres like (Action, Fantasy)")
print("3. Movie from specific language like (English, Spanish)")
try:
# storing user's initial choice
userChoice = int(input("How do you want to guess the movie?"))
except ValueError as _:
print("Please enter a correct number and try again.")
quit()
# If user wants movies from specific genres
if userChoice == 2:
print("Select any genre that you want:")
count = 1
for genre in newGen:
print(count,":",genre)
count += 1
try:
# storing user's genre choice
genreChoice = int(input("Enter your choice:"))
# doing some checks
if genreChoice > count or genreChoice < 1:
print("Please enter a proper number from the given list and try again")
quit()
except ValueError as _:
print("Please enter a number from the given list and try again.")
quit()
# a dictionary to store genre names with their respective code number
genreMap = {}
count = 1
for genre in newGen:
genreMap[count] = genre
count += 1
targetGenre = genreMap[genreChoice]
print(targetGenre)
mc.execute("select movieID from movies where genre= ? order by movieID asc",(targetGenre,))
# a list of all the movieIDs from that specific genre
movieIDs = []
for x in mc.fetchall():
movieIDs.append(x[0])
# If user wants movies from specific languages
elif userChoice == 3:
print("Select any language that you want:")
count = 1
for language in newLang:
print(count,":",language)
count += 1
print()
print("(NOTE: You can scroll up or down to see more languages.)")
try:
# storing user's language choice
languageChoice = int(input("Enter your choice:"))
# doing some checks
if languageChoice > count or languageChoice < 1:
print("Please enter a proper number from the given list and try again")
quit()
except ValueError as _:
print("Please enter a number from the given list and try again.")
quit()
# a dictionary to store languages with their respective code number
langMap = {}
count = 1
for lang in newLang:
langMap[count] = lang
count += 1
targetLanguage = langMap[languageChoice]
print(targetLanguage)
mc.execute("select movieID from movies where language= ? order by movieID asc",(targetLanguage,))
# a list of all the movieIDs from that specific language
movieIDs = []
for x in mc.fetchall():
movieIDs.append(x[0])
# If user wants movies to be completely random
elif userChoice == 1:
mc.execute("select distinct movieID from movies order by movieID asc")
# a list of all the movieIDs from that specific genre
movieIDs = []
for x in mc.fetchall():
movieIDs.append(x[0])
# If wrong choice
else:
print("Please select correct option and try again.")
quit()
""" Selecting a random movie from the list and getting all details about it """
movieID = choice(movieIDs)
# Storing movieName
mc.execute("select movieName from movies where movieID=?",(movieID,))
for x in mc.fetchall():
movieName = x[0]
# Storing movieSummary
mc.execute("select movieSummary from movies where movieID=?",(movieID,))
for x in mc.fetchall():
movieSummary = x[0]
# Closing the connnection
db.close()
# Storing the original movie name in a variable
actualMovie = movieName
# Movie name in string and it's length
movieName = movieName.lower()
movieLen = len(movieName)
# Movie Link
movieLink = "https://www.imdb.com/title/tt"
movieLink = movieLink + "0"*(7-len(str(movieID))) + str(movieID)
""" Implementation of the actual game starts from here """
# Using a hashmap to store all characters of the movie
hashMap = {}
for x in movieName:
hashMap[x] = False
# Printing hints about the movie title
print("Okay so your movie is now ready, it is",movieLen,"characters long with",movieName.count(" "),"spaces, ...you have 8 tries to guess the movie.")
# Asking the user for choice
choice = input("Do you want to start guessing? (y/n)")
# If choice is "no"
if choice.lower() == "n":
print("Please dont waste my TIME")
# If choice is not "no"
else:
# No of tries.
tries = 8
# Checking if all the tries are used.
while tries > 0 :
# Using a flag to check for values in our hashMap
flag = 0
for x in hashMap:
if hashMap[x] == False:
flag=1
if flag == 0:
print()
print("Congratulations, you have succesfully guessed the movie:",actualMovie)
break
# Guessing the letter and doing checks
character_guessed = input("Enter the next letter of the movie:").lower()
# If guess was correct
if character_guessed in movieName and len(character_guessed) == 1:
# If already guessed this
if hashMap[character_guessed] == True:
print("You have already guessed this.")
continue
# If not guessed before
else:
hashMap[character_guessed] = True
for c in movieName:
if hashMap[c] == True:
print(c,sep = '',end = '')
else:
print('_',sep = '',end = '')
print()
# If guess is incorrect
else:
print("Your guess was incorrect, you have",tries-1,"tries left.")
tries -= 1
# If all the tries are over.
if flag == 1:
print()
print("You have lost the game...the movie was:", actualMovie)
print()
# Printing a short summary about the movie
print("Here's a short summary of the movie:")
print()
print(movieSummary)
print("To learn more about the movie, you can go to:",movieLink)
print("BYE!")