Skip to content

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

PYTHON WORDLE

Description:

My project implements the famous game that we all know and love, wordle, fully using python.
It's a game where you are required to guess a 5-lettered word in 6 tries.
After each try, colored blocks are shown to represent which letters are in the correct place, which are not, and which are not even in the word, using green, yellow and grey colored blocks respectively.

Files Included:

  • project.py: Contains the main game loop and all core logic functions.
  • test_project.py: Contains pytest functions to verify the accuracy of the game's logic, specifically testing edge cases for letter matching.
  • wordlist.txt: A text file containing a dictionary of valid 5-letter words used to validate user input.
  • requirements.txt: Lists the external libraries required to run this program.

How It Works:

Functions:

  1. get_word(): Prompts the user to input their 5-letter guess and converts it to lowercase for standardized checking.

  2. validate_word(guess, wordlist): Ensures the user's input is valid. It checks that the guess is a valid word from the english dictionary (wordle dictionary)

  3. validate_word_length(guess): Ensures the user's input is exactly 5 letters, not more, not less.

  4. guess_results(guess, secret): This is the core engine of the game. It compares the valid guess against the secret word and generates the visual feedback using emojis (🟩, 🟨, ⬜).

  5. main(): The main function is responsible for running the whole game from start to finish, by calling the earlier functions we talked about. It starts by showing the secret word chosen by wordle for 3 seconds and proceeds to hide it so the user can start "guessing" the secret word. After the guess is obtained, it goes through the checking process of it being the correct length and being in the wordle dictionary. If the guess is invalid, the program will reprompt the user again for a valid guess. If the guess is valid, the program proceeds to check how accurate the guess was to the secret word by showing different colored emojis corresponding to the guess' accurateness. The user is then reprompted for his next try until the user guesses the secret word right or their number of tries exceeds 6.

Implementation decisions:

  1. guess_results(guess, secret) logic: The most complicated and time consuming implementation decision was done in the guess_results(guess, secret) function. It was making sure for a secret word like "apple" and a guess like "puppy", the result would look like this "🟨⬜🟩⬜⬜" and not like this "🟨⬜🟩🟨⬜". This shows that only one more 'p' is left to be in the right spot and not two. This was done by going through the guess twice. First time to lock in the results for letters in their correct spot. Second time was to check if a letter existed in both the guess and secret words but not in the correct positions. This was done by changing each letter in the secret word that also exists in the guess (but not in the correct position) to an underscore, to act as an indicator in the next loop to not result in a yellow emoji for a letter in the guess that has been replaced by an underscore in the secret word.

  2. checking word validity and length constraint: This implementation idea was also done after long sessions of trial and error. At first I had this implemented as one function, where it checks the word validity according to wordle's dictionary, and also checks the validity of its length. This did not end well when in the testing phase I tried to enter an invalid (not in the wordle dictionary) word, then being reprompted and entering a word of wrong length, the program would not respond as expected. Printing wrong error messages for wrong occasions. This was fixed after splitting those validity checks into 2 separate functions, calling both in main, and handling the reprompting and the error message display accordingly.

  3. Strings or Lists?: At first i thought strings and lists are immutable, making it somewhat impossible to implement the guess_results(guess, secret) function since I would have to update the secret word every loop and make sure that the guess is being compared to the updated secret word. This was fixed when I discovered that lists are mutable, my life became so much easier and decided to implement it using 2 loops as discussed in the first implementation decision

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages