Plan out the Wordle Logic

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

Welcome to the 7th tutorial in this series.  Let's fix a minor problem and then use the rest of the time to map out the logic we want.

Last time our program was asking us for our new guesses on the same line that it printed our former guess. If you don't want that behavior then just add an empty print statment after the loop:

print()

Well that solves that!

Now at the top of our code let us define the word to be "error" instead of randomly getting a word from our test word list.

The word "error" makes a good test case because it has duplicate letters so that we can test the limits of our logic.

word = choice(TEST_WORD_LIST)

word ="error"

This just overwrites our random choice with the word "error".  Then, later on we can just delete the forced word choice.

The method that I am going to use to determine when a letter is in the word but not in the correct place is as follows:

I will plan to loop through the letters of the word 2 times.  The first time I will compare the word with my guess in a letter-for-letter fashion and then *remove* any correctly placed letters from the guess (assigned as a new variable).  Then in the second pass through I will compare my new variable (with the missing letters) against the word to see if any letters from my guess are in the word but in the wrong place.  Sound confusing?  Good.  Lets plow ahead!

Let us first just make a large comment that maps out the behavior that we want and expect.  That way we can see if our code actually does what we want it to do.

I will assume 4 different guesses:  arrow, break, rrrrr, and rrrrx.  (We haven't yet forced our guesses to be real words or only 5-letter long-- that will come later.)  The nonsense word guesses are there to demonstrate the expected behavior.

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

"""

If we look at the comment above, we see the guess with correct letters removed and we see the word with correct letters removed.  Notice what will happen in a second pass.  We now have a guess with correctly placed letters removed (at our disposal).  As we loop through again we still compare the full guess to the full word.  But when a guess letter does not match a word letter, we can start to compare the actual word letter with all the "leftover" letters to see if the letter is in our guess but in the wrong place.

In our first guess, "arrow"  the 'r','r', and 'o' are all correctly placed.  Neither 'a' nor 'w' shows up in the remainder of the word so we don't expect any yellow printed letters.

In our second guess 'break', the first 'r' is correctly placed (should be green). When we get to the 'e' in 'break' we should be able to see that there is an 'e' in 'e_ror' and now we should color that 'e' yellow.

In our third guess (rrrrr), we should see that all three 'r's are correctly placed.  Since there are only three total 'r's in the word the other two 'r's should be grey.

In our fourth guess (rrrrx), we have two correctly placed 'r's but one 'r' not correctly placed.  As we compare our guess with our true-word (minus the correctly placec letters) we see that the first 'r' in our guess should be colored yellow,  the second two 'r's should be green and the fourth 'r' should be grey.

This contrived example hopefully demonstrates some of the most exotic behavior that we can expect with the game.  Therefore  If our logic can handle this, it can handle anything!

In the next tutorial, we will construct the logic with the 2-passes through the letters to make the game!  See you there!

skip_nextPut Wordle Logic Plan into Action!
  • 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
    (currently viewing)
  • Put Wordle Logic Plan into Action!

  • Declaring Victory