03 Feb Five-Letter Word Guessing Game App
This guessing game makes users suggest hidden letters for a five-letter word. The implementation of this guessing game is you have five empty boxes where you supply just one special letter and the position where you will place the letters. Then, it generates five-letter words based on your input.
In this article, we will guide you through the steps in creating a five-letter word guessing game app and how to play the game. Let’s jump right in.
The Game Design
A blank five-letter space appears to the player in the game, each space is for one letter. The player then guesses a letter and a position of which the player can only enter one letter into each space.
Based on the letter he enters and the position he places the letter in the five blank spaces, the game will generate five-letter words in the exact position. For instance, let’s say I have five empty spaces, and I am to fill in just a letter, I then enter the letter ‘S’ at position 1; this will generate 5 letter words like smile, Sahel, sager, and so on.
This game filters words for the user as well. We can also specify characters we don’t want to find in our five-letter words. Let’s say for all five-letter words that I enter the letter ‘S’ at position 1, I can specify characters as many as possible. We might not want to find ‘e’ and ‘a’ in each of the five different words generated. This game will handle that to print out all five-letter words that don’t contain ‘e’ and ‘a’. Quite interesting right?
Data Collection
Firstly, collect data to make the word generation easier. Then, clean the data to remove any dirty data and check for duplicates.
Game Implementation
Import Libraries
We will first import the libraries we want to use and then bring in our five-letter words generated and cleaned. The next step is to provide players with instructions to know which of the levels they are interested in playing and also obtain their input on that level(by picking either 1 , 2, 3, 4, 0)
import pandas as pd file='all_joined_fivelett_16plus.csv' def intro(): print("*******************************************") print('Hii, you are welcome to the guessing game') print('Instructions: This game has different level of difficulty, Level 1, Level2, Level3, Level4, Level0') print('Level1: You only supplied one character') print('Level2: You only supplied two character') print('Level3: You only supplied three character') print('Level4: You only supplied four character') print('Level0: it return negative words') print('You indicate which level you will be playing') print("*******************************************") def words(csv_file): data=pd.read_csv(csv_file) five_lw=data['Letters'].tolist() return five_lw def get_input_lev(): lev_input = input(' Enter level to play ') return lev_input
Game Initiation
We will define different functions for each level to play. For any level the player selects, we will obtain our input from the player to know the letters he wants to supply to the empty boxes and at what position he would like them to be placed.
def one_game_input(): #we designed and get input from the user here print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your letter to be placed') pos=int(input())-1 print('which letter would you like to enter') the_input=input() return pos, the_input def two_game_input(): print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your first letter to be placed') letter_posone=int(input())-1 print('which letter would you like to enter first') letter_inputone=input() print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your next letter to be placed') letter_postwo=int(input())-1 print('which second letter would you like to enter next') letter_inputtwo=input() return letter_posone, letter_inputone, letter_postwo, letter_inputtwo def three_game_input(): print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your first letter to be placed') letter_posone=int(input())-1 print('which letter would you like to enter first') letter_inputone=input() print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your next letter to be placed') letter_postwo=int(input())-1 print('which second letter would you like to enter next') letter_inputtwo=input() print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your next letter to be placed') letter_posthree=int(input())-1 print('which third letter would you like to enter next') letter_inputthree=input() return letter_posone, letter_inputone, letter_postwo, letter_inputtwo, letter_posthree, letter_inputthree def four_game_input(): print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your first letter to be placed') letter_posone=int(input())-1 print('which letter would you like to enter first') letter_inputone=input() print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your next letter to be placed') letter_postwo=int(input())-1 print('which second letter would you like to enter next') letter_inputtwo=input() print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your next letter to be placed') letter_posthree=int(input())-1 print('which third letter would you like to enter next') letter_inputthree=input() print("_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _") print('At what postion would you like your next letter to be placed') letter_posfour=int(input())-1 print('which fourth letter would you like to enter next') letter_inputfour=input() return letter_posone, letter_inputone, letter_postwo, letter_inputtwo, letter_posthree, letter_inputthree, letter_posfour, letter_inputfour
Words Generation
Here, we will define different functions for our game levels, for which we get our user input and the position the player wants the letter to be placed. Words will be generated by the player based on his input. The functions defined to get user input above will be called in this function. After these new words have been generated, we will save them in an empty list.
#the one game will be called based on user input on level to play def one_game(the_data): word_gen=[] letter_posone, letter_inputone = one_game_input() #we loop through all the five letter words we have in the list five_lw = words(the_data) for i in five_lw: #print(i) # #we loop through each of the five letter words using enumerate to get both position and each character for each words for pos, char in enumerate(i): #An if function is used to test the first case for the first user input if (char==letter_inputone and pos==letter_posone): word_gen.append(i) return word_gen #the two game will be called based on user input on level to play def two_game(the_data): word_gen=[] letter_posone, letter_inputone, letter_postwo, letter_inputtwo = two_game_input() five_lw= words(the_data) for i in five_lw: #print(i) # #we loop through each of the five letter words using enumerate to get both position and each character for each words for pos, char in enumerate(i): #An if function is used to test the first case for the first user input if (char==letter_inputone and pos==letter_posone): #After we tested, the result it gave was assigned to a variable letter_ex=i #we loop through each character in the variable we assigned earlier using the enumerate to get both position and each characters for s, j in enumerate(letter_ex): #we then pass in the second case of user's input to test if it exist if (j==letter_inputtwo and s==letter_postwo): pass #we then append this to an empty list word_gen.append(letter_ex) return word_gen #the three game will be called based on user input on level to play def three_game(the_data): word_gen=[] letter_posone, letter_inputone, letter_postwo, letter_inputtwo, letter_posthree, letter_inputthree = three_game_input() five_lw= words(the_data) for i in five_lw: #print(i) # #we loop through each of the five letter words using enumerate to get both position and each character for each words for pos, char in enumerate(i): #An if function is used to test the first case for the first user input if (char==letter_inputone and pos==letter_posone): #After we tested, the result it gave was assigned to a variable letter_ex=i #we loop through each character in the variable we assigned earlier using the enumerate to get both position and each characters for s, j in enumerate(letter_ex): #we then pass in the second case of user's input to test if it exist if (j==letter_inputtwo and s==letter_postwo): letter_exsec=letter_ex #we then pass in the third case of user's input to test if it exist for t, v in enumerate(letter_exsec): if (v==letter_inputthree and t==letter_posthree): pass word_gen.append(letter_exsec) return word_gen #the four game will be called based on user input on level to play def four_game(the_data): word_gen=[] letter_posone, letter_inputone, letter_postwo, letter_inputtwo, letter_posthree, letter_inputthree, letter_posfour, letter_inputfour = four_game_input() five_lw= words(the_data) for i in five_lw: #print(i) # #we loop through each of the five letter words using enumerate to get both position and each character for each words for pos, char in enumerate(i): #An if function is used to test the first case for the first user input if (char==letter_inputone and pos==letter_posone): #After we tested, the result it gave was assigned to a variable letter_ex=i #we loop through each character in the variable we assigned earlier using the enumerate to get both position and each characters for s, j in enumerate(letter_ex): #we then pass in the second case of user's input to test if it exist if (j==letter_inputtwo and s==letter_postwo): letter_exsec=letter_ex for t, v in enumerate(letter_exsec): if (v==letter_inputthree and t==letter_posthree): letter_exth=letter_exsec for h, l in enumerate(letter_exth): if (l==letter_inputfour and h==letter_posfour): word_gen.append(letter_exth) return word_gen
Removing Negative characters
The negative characters mean we specify characters we don’t want to see in our words. For instance, if you don’t want to see ‘b’, ‘a’ or more characters like this, we can handle this in our code. It will return words that have no ‘b’ and ‘a’ in them.
We can also add characters we want to see in those words just as we specified for negative characters(perhaps, it should contain ‘r’ and ‘s’).
def negative(): negative=[] the_file=words(file) for i in the_file: #we check if any of this character can not be found in each of the words if ('b' not in i) and ('l' not in i) and ('n' not in i) and ('d' not in i) and ('a' not in i): #we check if any of this character can be found in each of the words val =i if ('r' in val) and ('s' in val): #we print non-negative characters negative.append(i) return negative
Run Game
Here, we will write a function that calls the functions defined above to obtain the level user wants to play. Based on this response we will use functions designated for each of the different game levels; then run our game and provide game results.
def run_game(): intro() the_input=get_input_lev() if the_input == '1': for_one_game=one_game(file) all_non_neg=remove_non_negative(for_one_game) print(all_non_neg) elif the_input== '2': for_two_game=two_game(file) all_non_negt=remove_non_negative(for_two_game) print(all_non_negt) elif the_input== '3': for_three_game=three_game(file) all_non_negth=remove_non_negative(for_three_game) print(all_non_negth) elif the_input== '4': for_four_game=four_game(file) all_non_negfo=remove_non_negative(for_four_game) print(all_non_negfo) elif the_input == '0': the_neg_words= negative() print(the_neg_words) print(len(the_neg_words)) run_game()
If you have any questions concerning this article, feel free to drop them in the comments section below.
No Comments