Put Wordle Logic Plan into Action!

Published June 29, 2022, 1:13 p.m.

Here is our code so far:

import sys, os
import colorama
from colorama import Fore, Back, Style
from random import choice

colorama.init()

def printback(statement, color):
    print(color + Style.BRIGHT + f"{statement}" + Style.RESET_ALL, end="")
    
green = Back.GREEN
yellow = Back.YELLOW
grey = Back.LIGHTBLACK_EX

fname = "five_letter_words_list.txt"
f = open(fname, 'r')
lines = f.readlines()
f.close()

wordlist = []
for line in lines:
    words = [w.strip() for w in line.split(',') if w not in [' ', ' \n']]
    wordlist += words

# wordlist = list(set(wordlist))
# wordlist.remove('')

TEST_WORD_LIST = [
'error',
'apple',
'cruel',
'ionic',
'pizza'
]

word = choice(TEST_WORD_LIST)

word ="error"
print(word)
"""
true word == error
Method: remove correctly placed letters and loop through twice
1st guess: arrow

arrow --> a___w |  e___r  firstpass
break --> b_eak |  e_ror  firstpass --> e should print yellow
rrrrr --> r__r_ |  e__o_  firstpass --> both rs should be grey
rrrrx --> r__rx |  e__or  firstpass --> one yellow r one grey r

"""

listword = list(word)
print("Guess a 5 letter word! ")
guess = ""
num_guesses = 0
counter = 1
while num_guesses != 6:
    guess = input(f"Guess {counter}: ")
    listguess = list(guess)
    for guess_letter, word_letter in zip(listguess, listword):
        if guess_letter == word_letter:
             printback(guess_letter, green)
        else:
            printback(guess_letter, grey)

The 'for' loop at the end of our code is the loop that we will do twice.  In the first iteration of the loop let us remove the 'printback' statement and instead let us construct our 'word without correctly placed letters' or 'word_without_cpl':

...
while num_guesses != 6:
    guess = input(f"Guess {counter}: ")
    listguess = list(guess)
    word_without_cpl = ""
    for guess_letter, word_letter in zip(listguess, listword):
        if guess_letter == word_letter:
            # printback(guess_letter, green)
            word_without_cpl += " "
        else:
            word_without_cpl += word_letter
            # printback(guess_letter, grey)

Notice that if the guess letter matches the word letter we add a space character, otherwise, we add the letter from the actual word.

Now we will duplicate the for-loop and add back the "printback" logic to it!

...
while num_guesses != 6:
    guess = input(f"Guess {counter}: ")
    listguess = list(guess)
    word_without_cpl = ""
    for guess_letter, word_letter in zip(listguess, listword):
        if guess_letter == word_letter:
            # printback(guess_letter, green)
            word_without_cpl += " "
        else:
            word_without_cpl += word_letter
            # printback(guess_letter, grey)
    for guess_letter, word_letter in zip(listguess, listword):
        if guess_letter == word_letter:
            printback(guess_letter, green)
            
        else:
            printback(guess_letter, grey)

Next we need to add an "elif" statement in the middle of our second for-loop.  We want to check to see of the guess letter is in our 'word_without_cpl'  variable:

...
    ...
    for guess_letter, word_letter in zip(listguess, listword):
        if guess_letter == word_letter:
            printback(guess_letter, green)
        elif guess_letter in word_without_cpl:
            printback(guess_letter, yellow))
            
        else:
            printback(guess_letter, grey)

Now, that is not going to be the whole story, however, it is a good place to test our code and see if we get the expected behaviors!

Guess a 5 letter word!
Guess 1: arrow
arrow

Guess 2: break
break

Guess 3: rrrrr
rrrrr

Guess 4: rrrrx
rrrrx

With the code we have above, guesses 1, 2, and 3 work the way that we expected them to work.  However,  guess 4 gave us a problem.  In guess four, both wrongly placed 'r's printed yellow.  That is not the correct behavior!  What to do?  My approch will be to convert our word to a list, remove the first instance of the letter from the list, then convert what remains back to a word. 

...
    ...
    for guess_letter, word_letter in zip(listguess, listword):
        if guess_letter == word_letter:
            printback(guess_letter, green)
        elif guess_letter in word_without_cpl:
            printback(guess_letter, yellow))
            word_without_cpl = list(word_without_cpl)
            word_without_cpl.remove(guess_letter)
            word_without_cpl = "".join(word_without_cpl)
            
        else:
            printback(guess_letter, grey)

The reason for doing it this way is that the function that removes an item from a list only removes the first instance of that item.  Using a text attribute like ".replace" would replace all instances of the letter.  That would work sometimes and not work other times.  If we run it again and use our 4 test words we should see that the code works as intended now!

In the next tutorial we will start to implement the conditions that let the game know if you won or lost!  See you then!

 

 

skip_nextDeclaring Victory
  • Introduction to PyWordle

  • Get the latest version of Python (3.10.4)

  • Figure out all the 5 letter words (part 1)

  • Figure out all the 5 letter words (part 2)

  • Color letters with colorama!

  • Get words from our word list!

  • Select a Random Wordle

  • Use a 'WHILE' loop for guessing

  • Start Comparing Letters in Words

  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!
    (currently viewing)
  • Declaring Victory