How to Make Letters Horizontal, Vertical, Backwards, and Intersect when Coding a Word Search Game in Python
Categories: Tech | Pubby Cash Received:
Do you want to learn how to make letters horizontal, vertical, backwards, and intersect when coding a word search game in Python? If you said yes, than you should read this article, because it will teach you how. First, how to make words horizontal and how to make those horizontal words backwards. To do this, you first need to make a function. You then need to put things inside the function. Here is an example.
def add_word(word,arr):# make a function
col=random.choice(lst)#chooses a random location in the columns for the word to be placed
lst.remove(col)# removes that place so another word does not get placed there
row=random.randint(0,15-len(word))# chooses a random row for the word to be placed, however the length of the word must fit into the row and cannot go over the limit
rand_num = random.randint(0,1)# 0 means that the word is backwards, 1 means that is forward
if rand_num == 0: # if the random number is 0, then you make the word backwards
word1 = word[::-1]# [::-1] makes words reversed
else: # if it is not 0, then keep it the same
word1 = word
for i in range(0,len(word1)): # printing the i
arr[col,row+i]=lett2num[word1[i]]
Next I will tell you how to add the vertical words, make it backwards, and make them cross with the horizontal words. Adding a vertical word is almost the same as adding a horizontal one. The difference is that vertical ones are basically the opposite of horizontal ones. Here is an example.
def add_word1(word,arr): # make a function
row=random.choice(lst)#chooses a random location in the rows for the word to be placed
lst.remove(row)# removes that place so another word does not get placed there
col=random.randint(0,15-len(word))# chooses a random column for the word to be placed, however the length of the word must fit into the row and cannot go over the limit
addvword = True # make a new variable for adding vertical word. This is so vertical words and horizontal ones can cross
count = 0 # set count to 0
rand_num = random.randint(0,1)# 0 means that the word is backwards, 1 means that is forward
if rand_num == 0: if the random number is 0, then you make the word backwards
word1 = word[::-1]# [::-1] makes words reversed
else:# if it is not 0, then keep it the same
word1 = word
for i in range(0,len(word1)):# printing the i
arr[col+i,row]=lett2num[word1[i]]
for i in range(0,len(word1)):
if arr[col+i,row]!=0:# if the adding vertical word space is taken...
addvword = False #you cannot add that vertical word there
if arr[col+i,row] == lett2num[word1[i]]:# if the array is equal to the word1 letter...
count +=1 # add one to the count
else: # if that is not true...
count = 0# keep count at 0
if count != 0: # if the count is not zero...
for i in range(0,len(word1)):
arr[col+i,row]=lett2num[word1[i]] # the array is equal to the word1 letter
elif addvword: # if that is not true, addvword is equal to true
for i in range(0,len(word1)):
arr[col+i,row]=lett2num[word1[i]]# you can cross the word with another word if the crossing letter is the same
By now, you should have a better understanding of how to make letters horizontal, vertical, backwards, and intersect when coding a word search game in Python.
Published from: Pennsylvania US
Liked by: H2O, Andy Tang