Wordle Clone in Python (With Customizable Words)



"Hello" is now a primitive greeting. The latest greeting is "Have you finished today's Wordle ?" Although simple to understand and play, this game has taken the world by storm. If you are a Wordle fan and want to recreate the basics in Python, here's how you can proceed.

Of course, the first step is to get a set of words. You can create a list of your own words or find many words on the internet. If you have too many words, consider placing them in an external file and reading from there. If you want to know the basics of file handling with Python, check this out: File Handling in Python.

An idea would be to use the random module here to keep the game fun and not have all the words coming in the same order you entered them in the file/list.

Now, Wordle uses colors to show the letter status. However, since colors in Python is a bit of a headache, let's play it simple and create three lists - grey_letters, yellow_letters, and green_letters, which store the corresponding letters.

Here, we can also initialize another critical feature, the word check. Wordle allows only valid words, so we need to develop a method to check the entered word's validity. Here, we can use the enchant module. Enchant offers a function that initializes the English dictionary. We can use the check function to check the validity of the word.

Add a moves counter, and we are done with the basic initializations—time to move on to the main stuff.

The main functions are simple: Initialize a word (the word to find, say W1); Get a word from the user (say W2); Check W2's validity; Split W2 into letters and compare W2 and W1; Add letters to corresponding letter list. Print the lists—increment move counter.

How would we differentiate green_letters and yellow_letters, though? A simple method to do this is first to find the greens, then the yellows. To find greens, compare W1 and W2 by checking letters index-wise, i.e., 1st letter in W1 to 1st letter in W2, 2nd letter in W1 to 2nd letter in W2, and so on. Suppose a letter doesn't apply for green. In that case, we can use just a conditional statement to check whether the letter is present anywhere in the word, if yes: yellow; if no: grey.

Now throw this entire process into a loop and break the loop if you get the word or play all six moves.

Now for the code: 

#Initialization

all_words = ["LIGHT","POWER","PIOUS","ALERT","SHOUT","WORDS"]

removed_letters = []

yellow_letters = []

green_letters = []

import enchant

dic = enchant.Dict('en_us')

def word_check(word):
    return dic.check(word)

def list_print(word,the_list):
    print(word, ' letters :',end='')
    for letter in the_list:
        print(letter,end=' ')
    print()

import random

word = random.choice(all_words)

moves = 1

f=0

#Main Logic

print("Welcome to Wordle: \n")

while True:
    
    word_input = input("Enter word: ")
    
    if(len(word_input)) != 5:
        print("Retry ! Only 5 letter words...\n")
        
    elif word_check(word_input) != True:
        print("Word doesn't exist...\n")
        
    else:
        word_input = word_input.upper()
        word_split = [chr for chr in word]
        
        for i in range(0,5):
            if(word_input == word):
                print("You got it...")
                f=1
                break
            
            if(word_input[i] == word_split[i]):
                print("G ",end='')
                if(word_input[i] not in green_letters):
                    green_letters.append(word_input[i])
                    
            elif(word_input[i] in word_split):
                print("Y ",end='')
                if(word_input[i] not in yellow_letters):
                    yellow_letters.append(word_input[i])
            else:
                print("g ",end='')
                if(word_input[i] not in removed_letters):
                    removed_letters.append(word_input[i])

                    #G is green; Y is yellow; g is grey
                    
        print("\n")
        
        if(f==1):
            break
        
        list_print("Removed", removed_letters)
        list_print("Yellow", yellow_letters)
        list_print("Green", green_letters)
        print("\n")
        
        if(moves==6):
            print("You are out of moves... Word was ", word)
            break
        
        else:
            moves+=1

That's it... You have a basic working Wordle game on your PC. You can leave it as it is or add your own features. Of course, don't forget to have fun...

If you found this post interesting and valuable, don't forget to share it with others you think might like it. The comment section is open to any queries you have, so feel free to share your thoughts.

If you want to check out some other fun but simple games with Python, check these out: 

Battleships in Python

Blackjack in Python

Comments