-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInteractive_WordSearch.py
More file actions
139 lines (125 loc) · 4.99 KB
/
Interactive_WordSearch.py
File metadata and controls
139 lines (125 loc) · 4.99 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
"""
Interactive Word Search Game Module
This module provides functionality to generate and play an interactive word search game.
Players can find hidden words in a grid and receive hints through clues.
Features:
- Dynamically generates word search grids with hidden words
- Supports both predefined and random word selections
- Provides clues to help players find words
- Interactive gameplay with user input and feedback
Author: Santosh Goteti, Co-Authored by GPT-5 mini
Version: 1.0
"""
import random
import string
def build_word_search(words, size=10):
"""
Build a word search grid with hidden words.
Args:
words (list): List of words to hide in the grid.
size (int, optional): Dimension of the square grid. Defaults to 10.
Returns:
list: A 2D grid (list of lists) with words placed and remaining cells filled with random letters.
Raises:
ValueError: If a word cannot be placed in the grid.
"""
grid = [['' for _ in range(size)] for _ in range(size)]
directions = [(0, 1), (1, 0)]
for word in words:
placed = False
random.shuffle(directions)
for dr, dc in directions:
max_row = size - len(word) + 1 if dr != 0 else size
max_col = size - len(word) + 1 if dc != 0 else size
start_positions = [(r, c) for r in range(max_row) for c in range(max_col)]
random.shuffle(start_positions)
for r, c in start_positions:
if all(grid[r + i * dr][c + i * dc] in ('', char) for i, char in enumerate(word)):
for i, char in enumerate(word):
grid[r + i * dr][c + i * dc] = char
placed = True
break
if placed:
break
if not placed:
raise ValueError(f"Could not place the word: {word}")
for r in range(size):
for c in range(size):
if grid[r][c] == '':
grid[r][c] = random.choice(string.ascii_uppercase)
return grid
def print_grid(grid):
for row in grid:
print(' '.join(row))
print()
def play_word_search():
def generate_random_words_and_clues(count=5, min_len=3, max_len=8):
predefined_clues = {
'PYTHON': 'Famous AI-powered programming language.',
'JAVA': 'Most popular programming language for enterprise applications.',
'JAVASCRIPT': 'Language of the web browser.',
'ALGORITHM': 'Step-by-step procedure to solve a problem.',
'DATABASE': 'Organized collection of data.',
'NETWORK': 'Connected computers communicating together.',
'SECURITY': 'Protecting systems from attacks.',
'COMPILER': 'Translates code to machine language.',
'VARIABLE': 'Container for storing data values.',
'FUNCTION': 'Reusable block of code.'
}
clues = {}
available_words = list(predefined_clues.keys())
random.shuffle(available_words)
for i in range(min(count, len(available_words))):
word = available_words[i]
clues[word] = predefined_clues[word]
return clues
use_random = input("Generate random words & clues? (y/N): ").strip().lower() == 'y'
if use_random:
try:
count = int(input("How many words (default 5): ").strip() or 5)
except ValueError:
count = 5
clues = generate_random_words_and_clues(count)
else:
clues = {
'PYTHON': 'Famous AI-powered programming language.',
'CODE': 'What programmers write.',
'SEARCH': 'What you do in this puzzle.',
'GAME': 'A fun activity with rules.',
'FUN': 'Enjoyment or pleasure.'
}
words = list(clues.keys())
grid = build_word_search(words)
found = set()
remaining_clues = dict(clues)
print("Welcome to the interactive word search game!")
print("Find all words hidden in the grid.")
print("Clues:")
for clue in remaining_clues.values():
print(f" - {clue}")
print()
while len(found) < len(words):
print_grid(grid)
remaining_count = len(words) - len(found)
print(f"Words remaining: {remaining_count}")
guess = input("Enter a word you found (or 'quit'): ").strip().upper()
if guess == 'QUIT':
print("Thanks for playing.")
return
if guess in words:
if guess in found:
print("You already found that word.")
else:
found.add(guess)
print(f"Great! You found {guess}.")
if guess in remaining_clues:
del remaining_clues[guess]
print("Remaining clues:")
for clue in remaining_clues.values():
print(f" - {clue}")
else:
print("Not in the list. Try again.")
print()
print("Congratulations! You found all the words.")
if __name__ == "__main__":
play_word_search()