Build a Wordle Clone using Python and Rich
Introduction
Wordle is a popular game where we are supposed to guess a five-letter word in six tries. After each guess, the color of the tiles will change to show how close the guess was to the word. It is similar to the Hangman game which we have already built using Python.
In this blog, we'll build our own terminal version of the popular game Wordle using Python and Rich, a library for rich-text formatting. If you're new to Rich, check out this tutorial to get started with Rich. Make sure you have installed Rich before you get started.
Demo
Let us see what our final game will look like:
Let's Code It!
As with the real Wordle game, a random five-letter word is chosen daily, we will also select a random word from a list of five-letter words. So, first, let's define the list of words called word_list
in a separate words.py
file:
word_list = ['ABOUT', 'ABOVE', 'ABUSE', 'ACTOR', 'ACUTE', 'ADMIT', 'ADOPT', 'ADULT', 'AFTER', 'AGAIN', 'AGENT', 'AGREE', 'AHEAD', 'ALARM', 'ALBUM', 'ALERT', 'ALIKE', 'ALIVE', 'ALLOW', 'ALONE', 'ALONG', 'ALTER', 'AMONG', 'ANGER', 'ANGLE', 'ANGRY', 'APART', 'APPLE', 'APPLY', 'ARENA', 'ARGUE', 'ARISE', 'ARRAY', 'ASIDE', 'ASSET', 'AUDIO', 'AUDIT', 'AVOID', 'AWARD', 'AWARE', 'BADLY', 'BAKER', 'BASES', 'BASIC', 'BASIS', 'BEACH', 'BEGAN', 'BEGIN', 'BEGUN', 'BEING', 'BELOW', 'BENCH', 'BILLY', 'BIRTH', 'BLACK', 'BLAME', 'BLIND', 'BLOCK', 'BLOOD', 'BOARD', 'BOOST', 'BOOTH', 'BOUND', 'BRAIN', 'BRAND', 'BREAD', 'BREAK', 'BREED', 'BRIEF', 'BRING', 'BROAD', 'BROKE', 'BROWN', 'BUILD', 'BUILT', 'BUYER', 'CABLE', 'CALIF', 'CARRY', 'CATCH', 'CAUSE', 'CHAIN', 'CHAIR', 'CHART', 'CHASE', 'CHEAP', 'CHECK', 'CHEST', 'CHIEF', 'CHILD', 'CHINA', 'CHOSE', 'CIVIL', 'CLAIM', 'CLASS', 'CLEAN', 'CLEAR', 'CLICK', 'CLOCK', 'CLOSE', 'COACH', 'COAST', 'COULD', 'COUNT', 'COURT', 'COVER', 'CRAFT', 'CRASH', 'CREAM', 'CRIME', 'CROSS', 'CROWD', 'CROWN', 'CURVE', 'CYCLE', 'DAILY', 'DANCE', 'DATED', 'DEALT', 'DEATH', 'DEBUT', 'DELAY', 'DEPTH', 'DOING', 'DOUBT', 'DOZEN', 'DRAFT', 'DRAMA', 'DRAWN', 'DREAM', 'DRESS', 'DRILL', 'DRINK', 'DRIVE', 'DROVE', 'DYING', 'EAGER', 'EARLY', 'EARTH', 'EIGHT', 'ELITE', 'EMPTY', 'ENEMY', 'ENJOY', 'ENTER', 'ENTRY', 'EQUAL', 'ERROR', 'EVENT', 'EVERY', 'EXACT', 'EXIST', 'EXTRA', 'FAITH', 'FALSE', 'FAULT', 'FIBER', 'FIELD', 'FIFTH', 'FIFTY', 'FIGHT', 'FINAL', 'FIRST', 'FIXED', 'FLASH', 'FLEET', 'FLOOR', 'FLUID', 'FOCUS', 'FORCE', 'FORTH', 'FORTY', 'FORUM', 'FOUND', 'FRAME', 'FRANK', 'FRAUD', 'FRESH', 'FRONT', 'FRUIT', 'FULLY', 'FUNNY', 'GIANT', 'GIVEN', 'GLASS', 'GLOBE', 'GOING', 'GRACE', 'GRADE', 'GRAND', 'GRANT', 'GRASS', 'GREAT', 'GREEN', 'GROSS', 'GROUP', 'GROWN', 'GUARD', 'GUESS', 'GUEST', 'GUIDE', 'HAPPY', 'HARRY', 'HEART', 'HEAVY', 'HENCE', 'HENRY', 'HORSE', 'HOTEL', 'HOUSE', 'HUMAN', 'IDEAL', 'IMAGE', 'INDEX', 'INNER', 'INPUT', 'ISSUE', 'JAPAN', 'JIMMY', 'JOINT', 'JONES', 'JUDGE', 'KNOWN', 'LABEL', 'LARGE', 'LASER', 'LATER', 'LAUGH', 'LAYER', 'LEARN', 'LEASE', 'LEAST', 'LEAVE', 'LEGAL', 'LEVEL', 'LEWIS', 'LIGHT', 'LIMIT', 'LINKS', 'LIVES', 'LOCAL', 'LOGIC', 'LOOSE', 'LOWER', 'LUCKY', 'LUNCH', 'LYING', 'MAGIC', 'MAJOR', 'MAKER', 'MARCH', 'MARIA', 'MATCH', 'MAYBE', 'MAYOR', 'MEANT', 'MEDIA', 'METAL', 'MIGHT', 'MINOR', 'MINUS', 'MIXED', 'MODEL', 'MONEY', 'MONTH', 'MORAL', 'MOTOR', 'MOUNT', 'MOUSE', 'MOUTH', 'MOVIE', 'MUSIC', 'NEEDS', 'NEVER', 'NEWLY', 'NIGHT', 'NOISE', 'NORTH', 'NOTED', 'NOVEL', 'NURSE', 'OCCUR', 'OCEAN', 'OFFER', 'OFTEN', 'ORDER', 'OTHER', 'OUGHT', 'PAINT', 'PANEL', 'PAPER', 'PARTY', 'PEACE', 'PETER', 'PHASE', 'PHONE', 'PHOTO', 'PIECE', 'PILOT', 'PITCH', 'PLACE', 'PLAIN', 'PLANE', 'PLANT', 'PLATE', 'POINT', 'POUND', 'POWER', 'PRESS', 'PRICE', 'PRIDE', 'PRIME', 'PRINT', 'PRIOR', 'PRIZE', 'PROOF', 'PROUD', 'PROVE', 'QUEEN', 'QUICK', 'QUIET', 'QUITE', 'RADIO', 'RAISE', 'RANGE', 'RAPID', 'RATIO', 'REACH', 'READY', 'REFER', 'RIGHT', 'RIVAL', 'RIVER', 'ROBIN', 'ROGER', 'ROMAN', 'ROUGH', 'ROUND', 'ROUTE', 'ROYAL', 'RURAL', 'SCALE', 'SCENE', 'SCOPE', 'SCORE', 'SENSE', 'SERVE', 'SEVEN', 'SHALL', 'SHAPE', 'SHARE', 'SHARP', 'SHEET', 'SHELF', 'SHELL', 'SHIFT', 'SHIRT', 'SHOCK', 'SHOOT', 'SHORT', 'SHOWN', 'SIGHT', 'SINCE', 'SIXTH', 'SIXTY', 'SIZED', 'SKILL', 'SLEEP', 'SLIDE', 'SMALL', 'SMART', 'SMILE', 'SMITH', 'SMOKE', 'SOLID', 'SOLVE', 'SORRY', 'SOUND', 'SOUTH', 'SPACE', 'SPARE', 'SPEAK', 'SPEED', 'SPEND', 'SPENT', 'SPLIT', 'SPOKE', 'SPORT', 'STAFF', 'STAGE', 'STAKE', 'STAND', 'START', 'STATE', 'STEAM', 'STEEL', 'STICK', 'STILL', 'STOCK', 'STONE', 'STOOD', 'STORE', 'STORM', 'STORY', 'STRIP', 'STUCK', 'STUDY', 'STUFF', 'STYLE', 'SUGAR', 'SUITE', 'SUPER', 'SWEET', 'TABLE', 'TAKEN', 'TASTE', 'TAXES', 'TEACH', 'TEETH', 'TERRY', 'TEXAS', 'THANK', 'THEFT', 'THEIR', 'THEME', 'THERE', 'THESE', 'THICK', 'THING', 'THINK', 'THIRD', 'THOSE', 'THREE', 'THREW', 'THROW', 'TIGHT', 'TIMES', 'TIRED', 'TITLE', 'TODAY', 'TOPIC', 'TOTAL', 'TOUCH', 'TOUGH', 'TOWER', 'TRACK', 'TRADE', 'TRAIN', 'TREAT', 'TREND', 'TRIAL', 'TRIED', 'TRIES', 'TRUCK', 'TRULY', 'TRUST', 'TRUTH', 'TWICE', 'UNDER', 'UNDUE', 'UNION', 'UNITY', 'UNTIL', 'UPPER', 'UPSET', 'URBAN', 'USAGE', 'USUAL', 'VALID', 'VALUE', 'VIDEO', 'VIRUS', 'VISIT', 'VITAL', 'VOICE', 'WASTE', 'WATCH', 'WATER', 'WHEEL', 'WHERE', 'WHICH', 'WHILE', 'WHITE', 'WHOLE', 'WHOSE', 'WOMAN', 'WOMEN', 'WORLD', 'WORRY', 'WORSE', 'WORST', 'WORTH', 'WOULD', 'WOUND', 'WRITE', 'WRONG', 'WROTE', 'YIELD', 'YOUNG', 'YOUTH']
Utility Functions
We'll define a few utility functions to help us print colored text on the console.We'll use similar settings to the Wordle defaults:
- Green = correct letter in the correct position
- Yellow = correct letter, but in the incorrect position
- Gray = incorrect letter
For this, we're using Rich and it's quite easy to print colored text using it.
from rich.console import Console
from random import choice
from words import word_list
WELCOME_MESSAGE = f'\n[white on blue] WELCOME TO WORDLE [/]\n'
PLAYER_INSTRUCTIONS = "You may start guessing\n"
ALLOWED_GUESSES = 6
def correct_place(letter):
return f'[black on green]{letter}[/]'
def correct_letter(letter):
return f'[black on yellow]{letter}[/]'
def incorrect_letter(letter):
return f'[black on white]{letter}[/]'
if __name__ == '__main__':
console = Console()
chosen_word = choice(word_list)
console.print(WELCOME_MESSAGE)
console.print(PLAYER_INSTRUCTIONS)
Thus we have defined three functions to print colored text using Rich. Further, we have a welcome message and player instructions too, that will be used in the main function.The allowed guesses is set to 6. In the main function, we first create an instance of the Console
class from rich.console
. We then choose a random word from the word_list
defined in words.py
. In the last line, we just print the Welcome message and Player Instructions using console.print
.
Game Loop
We will run a while loop until all the guesses have been used. Let's see the code and then explain it.
GUESS_STATEMENT = "\nEnter your guess"
SQUARES = {
'correct_place': '🟩',
'correct_letter': '🟨',
'incorrect_letter': '⬛'
}
def check_guess(guess, answer):
guessed = []
wordle_pattern = []
for i, letter in enumerate(guess):
if answer[i] == guess[i]:
guessed += correct_place(letter)
wordle_pattern.append(SQUARES['correct_place'])
elif letter in answer:
guessed += correct_letter(letter)
wordle_pattern.append(SQUARES['correct_letter'])
else:
guessed += incorrect_letter(letter)
wordle_pattern.append(SQUARES['incorrect_letter'])
return ''.join(guessed), ''.join(wordle_pattern)
def game(console, chosen_word):
end_of_game = False
already_guessed = []
full_wordle_pattern = []
all_words_guessed = []
while not end_of_game:
guess = Prompt.ask(GUESS_STATEMENT).upper()
while len(guess) != 5 or guess in already_guessed:
if guess in already_guessed:
console.print("[red]You've already guessed this word!!\n[/]")
else:
console.print('[red]Please enter a 5-letter word!!\n[/]')
guess = Prompt.ask(GUESS_STATEMENT).upper()
already_guessed.append(guess)
guessed, pattern = check_guess(guess, chosen_word)
all_words_guessed.append(guessed)
full_wordle_pattern.append(pattern)
console.print(*all_words_guessed, sep="\n")
if guess == chosen_word or len(already_guessed) == ALLOWED_GUESSES:
end_of_game = True
if len(already_guessed) == ALLOWED_GUESSES and guess != chosen_word:
console.print(f"\n[red]WORDLE X/{ALLOWED_GUESSES}[/]")
console.print(f'\n[green]Correct Word: {chosen_word}[/]')
else:
console.print(f"\n[green]WORDLE {len(already_guessed)}/{ALLOWED_GUESSES}[/]\n")
console.print(*full_wordle_pattern, sep="\n")
The game()
function accepts two arguments - console
and chosen_word
(correct answer). We have a boolean variable end_of_game
that basically controls the while loop. The while loop will run until end_of_game
becomes True. already_guessed
is a list that will contain words already guessed by the user. full_wordle_pattern
list will contain the Wordle pattern(made of colored squares). all_words_guessed
list will contain the words with their colors.
After that, we run a while loop until end_of_game
and prompt the user to guess the word. We also continuously keep checking whether the word guessed by the user is of five letters or not as well as if the user has already guessed that word. In either of the cases, we print an error and prompt the user to guess again. If the user has guessed a word, we put the word in the already_guessed
list. Then we use a function called check_guess()
to check if the user has guessed the word correctly or not.
check_guess()
function
This function accepts two arguments - the word guessed by the user and the correct answer. It compares them letter by letter and then uses the helper functions we defined earlier to create the Rich formatting string for each letter, and then joins them all together into a single string. This function returns two strings - Rich formatted guessed word and colored squares for that guessed word.
The returned strings are stored in two variables, guessed
and pattern
, which are then appended in their respective lists, all_words_guessed
and full_wordle_pattern
. In each loop, we'll print the elements in all_words_guessed
list separated by a newline character(\n
). If the user has guessed the word correctly or the allowed number of guesses has been exhausted, we will set end_of_game
to True
and hence the loop will terminate. Further outside the loop, we will check if the allowed guesses have been exhausted and the user wasn't able to guess the word correctly, we will print the correct word and WORDLE X/6. However, in the other case, we'll print WORDLE n
/6 where n is the number of guesses user took to guess the word correctly. At the very end, we'll print the wordle pattern list separated by a newline character(\n
).
In the main function, we'll call the game()
function with console
and chosen_word
as the arguments.
Full Code
from rich.prompt import Prompt
from rich.console import Console
from random import choice
from words import word_list
SQUARES = {
'correct_place': '🟩',
'correct_letter': '🟨',
'incorrect_letter': '⬛'
}
WELCOME_MESSAGE = f'\n[white on blue] WELCOME TO WORDLE [/]\n'
PLAYER_INSTRUCTIONS = "You may start guessing\n"
GUESS_STATEMENT = "\nEnter your guess"
ALLOWED_GUESSES = 6
def correct_place(letter):
return f'[black on green]{letter}[/]'
def correct_letter(letter):
return f'[black on yellow]{letter}[/]'
def incorrect_letter(letter):
return f'[black on white]{letter}[/]'
def check_guess(guess, answer):
guessed = []
wordle_pattern = []
for i, letter in enumerate(guess):
if answer[i] == guess[i]:
guessed += correct_place(letter)
wordle_pattern.append(SQUARES['correct_place'])
elif letter in answer:
guessed += correct_letter(letter)
wordle_pattern.append(SQUARES['correct_letter'])
else:
guessed += incorrect_letter(letter)
wordle_pattern.append(SQUARES['incorrect_letter'])
return ''.join(guessed), ''.join(wordle_pattern)
def game(console, chosen_word):
end_of_game = False
already_guessed = []
full_wordle_pattern = []
all_words_guessed = []
while not end_of_game:
guess = Prompt.ask(GUESS_STATEMENT).upper()
while len(guess) != 5 or guess in already_guessed:
if guess in already_guessed:
console.print("[red]You've already guessed this word!!\n[/]")
else:
console.print('[red]Please enter a 5-letter word!!\n[/]')
guess = Prompt.ask(GUESS_STATEMENT).upper()
already_guessed.append(guess)
guessed, pattern = check_guess(guess, chosen_word)
all_words_guessed.append(guessed)
full_wordle_pattern.append(pattern)
console.print(*all_words_guessed, sep="\n")
if guess == chosen_word or len(already_guessed) == ALLOWED_GUESSES:
end_of_game = True
if len(already_guessed) == ALLOWED_GUESSES and guess != chosen_word:
console.print(f"\n[red]WORDLE X/{ALLOWED_GUESSES}[/]")
console.print(f'\n[green]Correct Word: {chosen_word}[/]')
else:
console.print(f"\n[green]WORDLE {len(already_guessed)}/{ALLOWED_GUESSES}[/]\n")
console.print(*full_wordle_pattern, sep="\n")
if __name__ == '__main__':
console = Console()
chosen_word = choice(word_list)
console.print(WELCOME_MESSAGE)
console.print(PLAYER_INSTRUCTIONS)
game(console, chosen_word)
Wrapping Up
In this blog, we have created our very own terminal version of Wordle. However, there is still a lot you could build from here. Similar to the real Wordle, you can create a web version of this project.
Let me know if you have any questions! Share with your friends.
Code Repository: https://github.com/ashutoshkrris/Terminal-Wordle