# Python Wordsearch Generator

For this challenge we will write a Python program to randomly generate a 12 by 12 wordsearch where computing words will be randomly positioned on the grid and will appear either horizontally, vertically or diagonally.

We have started the code for you but one of our subroutine is incomplete. This is the addWord() subroutine used to position a word on the wordsearch. Your task is to complete the code to:

• Decide where the word will start (random row and column)
• Decide if the word will be added horizontally, vertically or diagonally
• Check that the word will fit in (within the 12 by 12 grid)
• Check that the word will not overlap with existing letters/words in the wordsearch

#### Extension Task

Add two constants called ROWS and COLS both sets to 12. Make sure you declare and initialise these constants at the very beginning of your code.

Ensure that your code is using these constants throughout so that you can easily change the final size of the wordsearch by changing these two values only once at the beginning of your code.
[pms-restrict display_to=”not_logged_in” message=” “]

#### Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
[/pms-restrict] [pms-restrict subscription_plans=”14217″ message=” “]

#### Solution...

You are viewing this solution as part of your full membership subscription!

Python Code

#### Python Code

```#Python Wordsearch Generator
import random

ROWS = 12
COLS = 16
words = ["PYTHON","ALGORITHM","CODING","PROGRAM","VARIABLE","INTEGER","STRING"]

#A subroutine to replace all "-" (empty characters) with a random letter
def randomFill(wordsearch):
LETTERS="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for row in range(0,ROWS):
for col in range(0,COLS):
if wordsearch[row][col]=="-":
randomLetter = random.choice(LETTERS)
wordsearch[row][col]=randomLetter

#A subroutine to output the wordsearch on screen
def displayWordsearch(wordsearch):
print(" " + ("_"*COLS*2) + "_ ")
print("|" + (" "*COLS*2) + " |")
for row in range(0,ROWS):
line="| "
for col in range(0,COLS):
line = line + wordsearch[row][col] + " "
line = line + "|"
print(line)
print("|" + ("_"*COLS*2) + "_|")

#A subroutine to add a word to the wordsearch at a random position
def addWord(word,wordsearch):
placed=False
attempts = 0
while not placed and attempts<100: #Avoid infinite loops if the program can't find a place for a word.
attempts +=1
direction = random.randint(0,5)
#Decide starting row and col position for the first letter of the wor
#Decide horizontal step (hs) and vertical step (vs)

if len(word)>ROWS or len(word)>COLS:
print("Some of your words are too long for this grid. Remove long words or resize your grid.")
exit()

if direction==0: #Horizontal Left to Right
row=random.randint(0,ROWS - 1)
col=random.randint(0,COLS - len(word))
hs=1
vs=0
elif direction==1: #Vertical Top To Bottom
row=random.randint(0,ROWS - len(word))
col=random.randint(0,COLS - 1)
hs=0
vs=1
elif direction==2: #Diagonal Top Left - To Bottom Right
row=random.randint(0,ROWS - len(word))
col=random.randint(0,COLS - len(word))
hs=1
vs=1
if direction==3: #Horizontal Right to Left
row=random.randint(0,ROWS - 1)
col=random.randint(len(word)-1,COLS - 1)
hs=-1
vs=0
elif direction==4: #Vertical Bottom To Top
row=random.randint(len(word)-1,ROWS - 1)
col=random.randint(0,COLS - 1)
hs=0
vs=-1
elif direction==5: #Diagonal Bottom Right To Top Left
row=random.randint(len(word)-1,ROWS - 1)
col=random.randint(len(word)-1,COLS - 1)
hs=-1
vs=-1

#Check if word fits without colliding with other letters
collision=False
for i in range(0,len(word)):
if (wordsearch[row+vs*i][col+hs*i]!="-" and wordsearch[row+vs*i][col+hs*i]!=word[i]):
collision=True
# No Collision means we can add the word to the gird
if not collision:
for i in range(0,len(word)):
wordsearch[row+vs*i][col+hs*i]=word[i]
placed=True

if not placed:
print("Program aborted. Try again, remove words from your list or increase the size of the grid.")
exit()

#Create an empty wordsearch (list of lists)
wordsearch = []
for row in range(0,ROWS):
wordsearch.append([])
for col in range(0,COLS):
wordsearch[row].append("-")

#Adding words to our wordsearch
for word in words:
if word != "":
addWord(word,wordsearch)

#All unused spaces in the wordsearch will be replaced with a random letter
randomFill(wordsearch)

#Display the fully competed wordseach on screen
displayWordsearch(wordsearch)

print("\n--- LIST OF WORDS ---\n")
for word in words:
print("  - " + word)
```
[/pms-restrict]

Did you like this challenge?

Click on a star to rate it!

Average rating 4.8 / 5. Vote count: 8

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!