Skip to content

Commit 84b4588

Browse files
committed
add: word building
1 parent 0091d79 commit 84b4588

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

games/Word-Building/data.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import pickle
2+
3+
def DataAdding(word):
4+
file = open('words.bat', 'rb')
5+
words = pickle.load(file)
6+
a = words[word[0]]
7+
a.append(word)
8+
b = word[0]
9+
words[b] = a
10+
file.close()
11+
12+
file = open('words.bat', 'wb')
13+
pickle.dump(words, file)
14+
file.close()
15+
16+
file = open('words.bat', 'rb')
17+
words = pickle.load(file)
18+
file.close()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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("\nInvalid 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("\nInvalid word! Your word is not a valid English word.")
47+
win = False
48+
break
49+
if user_word in used_words:
50+
print("\nInvalid 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("\nBot 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("\nCongratulations! You win! 🎉")
73+
else:
74+
print("\nGame over! Bot wins! 🤖")
75+
76+
if __name__ == "__main__":
77+
Main()

games/Word-Building/words.bat

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)