The Algorithm to Create a Crossword Game Using Python.

Categories: Tech | Pubby Cash Received:  0 | Click to Award

Do you know the algorithm to make a crossword puzzle using Python? If you do not know how, then you should read this article because it will teach you how. We will make two arrays one of them has the correct answers while the other one has the letter O to replace the words. Here is the code. added_words_count = 0# make a variable equal to zero def add_word(word,arr): #this function adds the horizontal words to the cross word     global added_words_count#global that variable that was made     row=random.choice(lst)#take a random row to put the word in     lst.remove(row)# remove it so it does not repeat     col=random.randint(0,15-len(word))# choose a random column to put the word in     addhword = True#set this variable to true for adding horizontal words     count = 0#set count to zero     rand_num = random.randint(0,1)# randomly choosing 1 or 0 to decide if the word goes from right to left or left to right.     if rand_num == 0:if the random number is zero         word1 = word[::-1]# make it backwards     else: # if the random number is not zero         word1 = word# keep it the same     for i in range(0,len(word1)):# print i         arr[row,col+i]=lett2num[word1[i]]     added_words_count += 1 #add one to the added_words_count variable     print('added horizontal')# print added horizontal to know how many words you added were horizontal def add_word1(word,arr):# this function is for adding vertical words     global added_words_count# global the added_words_count variable     addvword = True# set adding vertical words to true     rand_num = random.randint(0,1)# randomly choosing 1 or 0 to decide if the word goes from right to left or left to right.     if rand_num == 0:if the random number is zero         word1 = word[::-1]# make it backwards     else: # if the random number is not zero         word1 = word# keep it the same     def adding_v_word():# make another function for adding         completely_empty = True# set the completely empty variable to true         col=random.choice(lst)# lst.remove(col)         row=random.randint(0,15-len(word))# choose a random row to put the word in         ready = False# set the variable ready to false         for i in range(0,len(word1)):print i             if arr[row+i,col] != 0:# if it is not equal to zero                 completely_empty = False# the completely empty variable is equal to false                 addvword = False# also, adding a vertical word would be false                 # if the letter in this square is the same as the one we're adding                 if arr[row+i,col] == lett2num[word1[i]]:# if it is equal                     addvword = True# adding vertical word is true                     ready = True# also ready is true                 else:if that is not true                     addvword = False # both are false                     ready = False                     break # use break method         return row, col, ready, completely_empty# return the variables     while True:# while that is true         row, col, ready, completely_empty = adding_v_word()# those variables equal adding_v_word()         if ready or completely_empty:# if ready or completely_true are true             for i in range(0,len(word1)):# print i                 arr[row+i,col]=lett2num[word1[i]]             print('just added another vertical') # print this so you know when you added a vertical word             added_words_count += 1             break def display_arr(arr):# this function is for displaying the array     for row in range(0,15):# for the row in the range of 0 to 15         line=""# set line to an empty string         for col in range(0,15):# for the column in the range of 0 to 15             line = line + num2lett[(arr[row,col])] + " "# set line to line plus the num2lett plus an empty string         print(line)# print line     print()# print empty space so it is easier to see the two arrays     for row in range(0,15):# for the row in the range of 0 to 15         for col in range(0,15):# for the column in the range of 0 to 15             if arr[row, col] != lett2num['X']:# if it is not equal, use the letter X             arr[row, col] = lett2num['O'] # if it is equal, use the letter O     for row in range(0,15):# for the row in the range of 0 to 15         line1=""# line1 equals empty string         for col in range(0,15):# for the column in the range of 0 to 15             line1 = line1 + num2lett[(arr[row,col])] + " "         print(line1)# print line1     print('added words', added_words_count)# print all of the parts By now you should have a better understanding of the algorithm to create a crossword game using Python.


Published from: Pennsylvania US
Liked by: Andy Tang, H2O 

Daily Deals


MECHANICSBURG WEATHER