Skip to content

Puzzle Object

Josh Duncan edited this page Jan 18, 2024 · 7 revisions

All word search puzzles are built from the base WordSearch class.

from word_search_generator import WordSearch
puzzle = WordSearch("dog pig cat")

Arguments:

  • words - Word to be included in the puzzle.
  • level - Difficulty level or potential directions for the puzzle words.
  • size - Size of the puzzle in characters.
  • secret_words - Secret words that will be in the puzzle but not included in the word list.
  • secret_level - Difficulty level or potential word directions for 'secret' words.

Words

A word search puzzle wouldn't be much without words... Puzzle words can be provided at time of creation or added after the fact. Words can be separated by spaces, commas, or new lines and Word-Search-Generator will sort them out for you.

puzzle = WordSearch("some random words to hide")  # separated by a space

Editing Current Puzzle Words

🤦‍♂️ Leave out a word?

puzzle.add_words("new, words, to, add")

❌ Need to remove a word?

puzzle.remove_words("words, to, delete")

♻️ Replace all of the words?

puzzle.replace_words("replace, current, words, with, new, words")

⚠️ When words are added, removed, or replaced, an entirely new puzzle will be generated!

Level

The level determines the difficulty level or potential word directions for a puzzle.

🍰 Too easy? Up the difficulty level.

>>> puzzle.level = 3
>>> puzzle.show()
-----------------------
      WORD SEARCH
-----------------------
W H Q Z M N M B P L F W
K Y K P D O L V A I N I
R X V E I C J C Z L M A
E Z H J O G F B J I G V
L F D Y E K N O D Y X F
E T I U Z O M T A C I E
D Q G M L B P E E H S X
I X W O H O R S E N C D
C M U R A K M Y C P I N
W P Y I C T C X O Y A K
Q D O G R N V E P F V C
Z F A C B E L T R U T A

Find these words: CAT, DOG, DONKEY, GOAT, HORSE, PIG, SHEEP, TURTLE
* Words can go N, SW, NW, SE, E, NE, W, and S.

Answer Key: CAT W @ (10, 6), DOG E @ (2, 11), DONKEY W @ (9, 5), GOAT SE @ (3, 7), HORSE E @ (5, 8), PIG SE @ (4, 2), SHEEP W @ (11, 7), TURTLE W @ (11, 12)

ℹ️ You'll find preset numeric levels listed in config.py, each with specific cardinal directions that words can travel.

You can also specify custom cardinal directions for your puzzle (e.g. N, NE, E, SE, S, SW, W, NW).

>>> puzzle.directions = "NW,SW"
>>> puzzle.show()
-----------------------
      WORD SEARCH
-----------------------
O I Y O T F J F M R L R
K W R D C W E N G S H V
N P S W M K I S U M D P
E F A L O D T Z R O H D
X H Q P G U B U G O O B
J R C E R A S L H N H P
Q Y I T V H Z Y K Q I C
R W L T E P T E S G X F
A E A E A Q Y A R A L O
J H P Y C O P W C Y B D
A V I E L R G X U J G A
W C X H D I N V K L W Y

Find these words: CAT, DOG, DONKEY, GOAT, HORSE, PIG, SHEEP, TURTLE
* Words can go SW, and NW.

Answer Key: CAT NW @ (9, 10), DOG SW @ (11, 3), DONKEY SW @ (12, 4), GOAT NW @ (7, 11), HORSE NW @ (11, 6), PIG SW @ (12, 6), SHEEP SW @ (7, 6), TURTLE SW @ (7, 4)

😱 Still too easy? Go expert mode.

> from word_search_generator import WordSearch
> from word_search_generator.utils import get_random_words
> puzzle = WordSearch(secret_words=get_random_words(10), level=3,)

Level

Word-Search-Generator offers preset difficulty levels. Some levels only allow for words to be placed left-to-right and others allow for right-to-left. You can find all of the preset numeric levels listed in config.py.

puzzle.level = 2

⚠️ Preset levels were introduced in the first implementation of Word-Search-Generator but have since been quasi-rolled into the directions property. They will continue to be supported for backward compatibility.

Size

By default, the puzzle size (in characters) is determined by the number of words provided and the difficulty level. Need a puzzle an exact size, override the default by setting puzzle.size. All puzzles are square so size will be the width and height. If you would like your puzzle to be a rectangle or a custom shape look at puzzle masking.

>>> puzzle.size = 55
ValueError: Puzzle size must be >= 5 and <= 50
>>> puzzle.size = 20

ℹ️ Puzzles are limited to 50 characters wide so that everything fits nicely on a US Letter or A4 sized sheet of paper.

⚠️ All provided words may not fit a specified puzzle size!

Secret Words

Word-Search-Generator accepts "secret" bonus words that can up the difficulty of your puzzle. Secret words are not included in the word list and can have their own level or directions.

>>> puzzle = WordSearch("dog, cat, pig", level=1, secret_words="secret, boo")
puzzle.show()
-----------------------
      WORD SEARCH
-----------------------
A U C Q X L H G C L K P
V K J A M D Y I N V S A
U M U Y J B O O M J N X
H D O Z F H Y Z B G S G
R U M A P D O G N U E P
B O P H B I C F J T C I
T J G K R V Y N X Q R G
S F U J P U K L I B E B
P H S K B F Y J Q C T X
G T L E P C N G U A K F
S H K H L D J I S T E O
P F P N Q C T D Q A K R

Find these words: CAT, DOG, PIG
* Words can go E, and S.

Answer Key: *BOO E @ (6, 3), CAT S @ (10, 9), DOG E @ (6, 5), PIG S @ (12, 5), *SECRET S @ (11, 4)

ℹ️ Secret words ARE included in the puzzle key (denoted by a '*').

Secret words can also have their own level or set of directions.

>>> puzzle.secret_directions="N,W"
-----------------------
      WORD SEARCH
-----------------------
X J G P N C R Q O J C O
G S F M G A N P M V Z Y
N H P D F T E T B N C Q
O C G T K Y D F U Y P Y
O K U E B T L C R S M T
B J X R D A R D O G Q J
Y N T C T K Z S R W Z E
B C D E P I P I G M U C
M W F S H C Z J D W N G
J Q X Y U N F V I V H V
S T N P Z C A O N B I Y
W A D H E W S B K A R K

Find these words: CAT, DOG, PIG
* Words can go E, and S.

Answer Key: *BOO N @ (1, 6), CAT S @ (6, 1), DOG E @ (8, 6), PIG E @ (7, 8), *SECRET N @ (4, 9)

⚠️ You may have noticed above that the secret directions are not included in the puzzle directions unless they overlap with the valid directions for the regular puzzle words. I told you it ups the difficulty level 📈.

Saving Puzzles

Word-Search-Generator can save puzzles as PDF, CSV, and JSON files. The .save() method defaults to a PDF is a format isn't specified.

💾 Save as a PDF.

puzzle.save(path="~/Desktop/puzzle.pdf")
"~/Desktop/puzzle.pdf"

💾 Save as a CSV.

puzzle.save(path="puzzle.csv", format="csv")
"./puzzle.csv"

💾 Save as a JSON.

puzzle.save(path="puzzle.json", format="json")
"./puzzle.json"

ℹ️ Using the Word-Search-Generator CLI Integration and redirections in your terminal you can also save the puzzle to a text file.

$ word-search dog, cat, pig, horse > puzzle.txt
Clone this wiki locally