Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KenesuEXE| Word-Search-Puzzle-Generator| Word search puzzle generator… #308

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Word-Search-Puzzle-Generator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Word Search Puzzle Generator
## by KenesuEXE

A word search puzzle generator in Python

## Usage
Run the `word_search_puzzle.py` script to generate a random word search puzzle. Input anything to reveal the answer.

## Note
This is a shorter version with less than 100 lines of code. For a more customizable puzzle and word choice, check the original repo [here](https://github.com/KenesuEXE/word-search-puzzle).

## Sample Output
```
B N V S U P E R G E N E R O U S W I A D
E S T I M A T E D R E N E T R A E H D M
T Y A Q G T R L I T Q Y U G O T Q K N S
D S U P E R B E N E V O L E N T L Y T S
E F J C I U U P U M I C A T I N G Z R U
C L U X H A E X F P R O C E D E S P E B
A O L E A C H E R S F S Q P C Z G A D F
S L D I M E R C U R Y K R G R C P R L I
Y E K K U D I W L X D H Z E P A K C U L
L A B R E V I R O S T R A L T S X H O E
L G U S T Z P G Q M C V Z R U I U A H Y
A I N R Z Q V T T X G M X O G B B I S L
B N T E G E C W M E X A C W L I S S E T
O O A P L W H R O G O U K D A I R M R S
N U M P W S Q T X G T Q T M K T X S O Q
D S E O X G Z S A S Y U P P Q T F B F O
X N L H I L J F E U Q A F U F N C D X G
F E Y G E L J F O G K H Q Z O N L X S A
G S S P U M I E R W Q O U S B A N J B C
T S H X V W L J W S K G I D I N L R L I

Your words are:
BREVIROSTRAL
MAUQUAHOG
BITERS
PUMICATING
DIMERCURY
OLEAGINOUSNESS
FORESHOULDER
SPUMIER
SUPERBENEVOLENTLY
LEACHERS
ARCHAISMS
HEARTENER
HOPPERS
DECASYLLABON
SUBFILE
FESTUCOUS
SUPERGENEROUS
ESTIMATED
PROCEDES
UNTAMELY

Press ENTER to reveal answers

_ _ _ S U P E R G E N E R O U S _ _ _ _
E S T I M A T E D R E N E T R A E H _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
D S U P E R B E N E V O L E N T L Y _ S
E _ _ _ _ _ _ P U M I C A T I N G _ R U
C _ _ _ _ _ _ _ _ P R O C E D E S _ E B
A O L E A C H E R S _ S _ _ _ _ _ A D F
S L D I M E R C U R Y _ R _ _ _ _ R L I
Y E _ _ _ _ _ _ _ _ _ _ _ E _ _ _ C U L
L A B R E V I R O S T R A L T S _ H O E
L G U S _ _ _ _ _ _ _ _ _ _ U I _ A H _
A I N R _ _ _ _ _ _ _ M _ O _ _ B I S _
B N T E _ _ _ _ _ _ _ A C _ _ _ _ S E _
O O A P _ _ _ _ _ _ _ U _ _ _ _ _ M R _
N U M P _ _ _ _ _ _ T Q _ _ _ _ _ S O _
_ S E O _ _ _ _ _ S _ U _ _ _ _ _ _ F _
_ N L H _ _ _ _ E _ _ A _ _ _ _ _ _ _ _
_ E Y _ _ _ _ F _ _ _ H _ _ _ _ _ _ _ _
_ S S P U M I E R _ _ O _ _ _ _ _ _ _ _
_ S _ _ _ _ _ _ _ _ _ G _ _ _ _ _ _ _ _
```
93 changes: 93 additions & 0 deletions Word-Search-Puzzle-Generator/word-search-puzzle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import random
import time
import copy

grid = [ [ '_' for _ in range(20) ] for _ in range(20) ] # 20x20 placeholder grid
orientations = ["horizontal", "vertical", "diagonal_up", "diagonal_down"]

all_words = open('words_alpha.txt', 'r').readlines()
word_list = []
while len(word_list) < 20: # Choose random word and check if below length limit
picked_word = random.choice(all_words)
if len(picked_word) <= 20:
word_list.append(picked_word.strip().upper())

for word in word_list:
timeout = time.time()+10 # Store timeout to end program when taking too long
is_placed = False

while not is_placed:
if time.time() > timeout: # Check if loop is taking too long to avoid program running indefinitely
raise Exception("Program took too long to generate grid. Please try again.")

orientation = random.choice(orientations)
is_reversed = True if random.random() > 0.5 else False

if orientation == "horizontal":
step_x = 1
step_y = 0
if orientation == "vertical":
step_x = 0
step_y = 1
if orientation == "diagonal_up":
step_x = 1
step_y = -1
if orientation == "diagonal_down":
step_x = 1
step_y = 1
if is_reversed:
step_x *= -1
step_y *= -1

start_x = random.randrange(20)
start_y = random.randrange(20)
end_x = start_x + len(word)*step_x
end_y = start_y + len(word)*step_y

# Check if word is outside of grid; if outside, try new orientation
if end_x < 0 or end_x > 20: continue
if end_y < 0 or end_y > 20: continue

can_fit = True # Check if letters don't overlap other letters
for i in range(len(word)):
letter = word[i]

position_x = start_x + i*step_x
position_y = start_y + i*step_y

letter_at_position = grid[position_x][position_y]

if letter_at_position == '_' or letter_at_position == letter:
continue # Check next letter
else:
can_fit = False
break # Cannot place word

if not can_fit: continue # Restart and choose another orientation
else: # Word can be placed, actually place word on grid
for i in range(len(word)):
letter = word[i]
position_x = start_x + i*step_x
position_y = start_y + i*step_y
grid[position_x][position_y] = letter

is_placed = True # Current word done. Go for next word

revealed_grid = copy.deepcopy(grid) # Save unfilled grid for answer reveal

for x in range(20): # Fill grid blanks with random letters
for y in range(20):
if grid[x][y] == '_':
grid[x][y] = random.choice("ABCDEFGHIJKLMNOPQRSTUVWXYZ")

for row in grid:
print(' '.join(row))

print("\nYour words are:")
for word in word_list:
print(word)

input("\nPress ENTER to reveal answers \n")
for row in revealed_grid:
print(' '.join(row))
print("\n")
Loading