source: https://www.nytimes.com/games/wordle/index.html
source: https://www.nytimes.com/games/wordle/index.html
Wordle
Wordle is the very popular and successful, word game where players have six attempts to guess a five-letter word. Each guess provides feedback on the letters:
1) are the letters in the word?
2) are the letters in the correct place?
I created a program using python to play wordle.*
The program functions like this:
Start
Initialize game and select a random word from a list.
Ask player for a guess.
Check guess and provide feedback.
Is the guess correct? And have all the attempts been used? If yes, go to end.
End the game with a message.
Preparation
Before diving into the creation of the Wordle game, I first had to prepare the data, ie the list of possible words. I found a list of words online from this site: (https://www.wordunscrambler.net/word-list/wordle-word-list). This list was a long string of words separated by spaces, essentially one giant string, which wasnt quite ideal for the game's design since each word needs to be selected randomly.
Writing a Python Script for Formatting:
To address this, the first task was to write a Python script to reformat the word list to be one word on each line.
Once the code was run, it took about one second to reformat that list (whereas it would have taken me considerably longer doing it manually).
Game
The challenge in recreating Wordle was figuring out how to provide feedback similar to wordle, which tells you if the guessed letter is in the correct place, and also which letters are in the word but not in their correct locations.
At first, I used the indexes to provide feedback on the placement of characters.
Enter a word: scope
1 letter: right letter, wrong place.
3 letter: right letter, wrong place.
5 letter: right letter, wrong place.
It made it difficult to keep track of the letters. Drawing inspiration from the game Hangman, I decided to try using underscores to represent each character in the word.
def provide_feedback(guess, wordle):
correct_place = ["_"] * 5
correct_but_wrong_place = []
The program starts by creating 2 lists. It creates a correct_place list with five “_” (underscores). These underscores are placeholders for each character. The second list is ‘correct_but_wrong_place’ list, which keeps track of letters that are in the word but not correctly placed.
for i, char in enumerate(guess):
if char == wordle[i]:
correct_place[i] = char
elif char in wordle:
correct_but_wrong_place.append(char)
The function checks each letter and its index. If a letter is in the word AND in the correct location, it updates the ‘correct_place’ list at that index. Which results in feedback like the following:
For letters not in the correct place, the ‘provide_feedback’ function constructs a feedback string combining the guesses to provide the correct feedback.
feedback = ''.join(correct_place)
if correct_but_wrong_place:
feedback += ". Letters " + ", ".join(correct_but_wrong_place) + " are correct but in the wrong place."
return feedback
The system also has checks to make sure what’s entered meets the input requirements and to cap the number of guesses at 6.
ready = input(f"Are you ready, {username}? Y/N: ").upper()
while ready not in ["Y", "N"]:
print("Invalid input. Please enter 'Y' for Yes or 'N' for No.")
ready = input(f"Are you ready, {username}? Y/N: ").upper()
if attempt == 5 and guess != wordle:
print("You have used up your guesses. The Wordle was " + wordle + ".")
print("Better luck next time.")
Because this project was for a class assignment, I wont post it in its entirety. But feel free to email me for the full program (as long as you're not enrolled in Harvard's Introduction to Computer Science with Python).
------
*This program was for a python project, a class I took while enrolled in the masters program for an Information Systems Management degree at Harvard.