1+ import random , time , enchant
2+ import data
3+
4+ def WordCheck (word ):
5+ d = enchant .Dict ('en_US' )
6+ if d .check (word ):
7+ return True
8+ else :
9+ return False
10+
11+ def botTurn (letter , used_words ):
12+ import data
13+ word = None
14+ try :
15+ while True :
16+ computer = (data .words [letter ])[random .randint (0 , len (data .words [letter ]) - 1 )]
17+ if computer not in used_words :
18+ word = computer
19+ break
20+ except :
21+ pass
22+ return word
23+
24+ def Main ():
25+ # Welcome message and instructions
26+ print ("🎮 Welcome to Word Building! 🎮" )
27+ print ('''🪙 bot(computer) will start with a word, you will give a word that starts with the last letter of bot's word,
28+ and bot will have to give a word that starts with the last letter of your word.
29+ The game will continue until one of you can't think of a word or gives an invalid word. You should not repeat words. \n ''' )
30+ time .sleep (5 )
31+
32+ user_word = None
33+ bot_word = None
34+ used_words = set ()
35+ win = False
36+
37+ # Game loop
38+ while True :
39+ # User input and validation
40+ user_word = input ("Your word: " )
41+ if (bot_word is not None ) and (user_word [0 ].lower () != bot_word [- 1 ].lower ()):
42+ print ("\n Invalid word! Your word must start with the last letter of bot's word." )
43+ win = False
44+ break
45+ if not WordCheck (user_word ):
46+ print ("\n Invalid word! Your word is not a valid English word." )
47+ win = False
48+ break
49+ if user_word in used_words :
50+ print ("\n Invalid word! You have already used this word." )
51+ win = False
52+ break
53+ used_words .add (user_word )
54+
55+ # Add user's word to the data if it's not already there
56+ if user_word not in data .words [user_word [0 ]]:
57+ data .DataAdding (user_word )
58+ time .sleep (1 )
59+
60+ # Bot's turn
61+ bot_word = botTurn (user_word [- 1 ].lower (), used_words )
62+ if bot_word is None :
63+ print ("\n Bot can't think of a word!" )
64+ win = True
65+ break
66+ used_words .add (bot_word )
67+ print (f"Bot's word: { bot_word } " )
68+
69+
70+ # End of game message
71+ if win :
72+ print ("\n Congratulations! You win! 🎉" )
73+ else :
74+ print ("\n Game over! Bot wins! 🤖" )
75+
76+ if __name__ == "__main__" :
77+ Main ()
0 commit comments