Word game
Code your own Wordle-like game
PYTHON
Keeping his grey matter well tested, Matt Holder creates a Wordle-style game and hopes the lawyers don’t ask too many questions.
OUR EXPERT Matt Holder is an IT professional of 15 years, Linux user for over 20 years, home-automation fan and self-professed geek.
QUICK TIP
The complete source code can be downloaded from https://github.com/mattmole/LXF-Wordle.
During late 2021, a game called Wordle was released to the world, and it became very popular incredibly quickly. The appeal of the game is its simplicity, the relatively short amount of time it takes to play and the innovative way in which your daily score can be shared with friends.
In this article, we are going to create a clone of the famous game, using around 100 lines of Python code. Before we start coding, though, let’s introduce the game a bit more thoroughly.
Wordle (now owned by the New York Times) is centrally hosted and there is only one game to play each day. This means that the game doesn’t take too much time and enables friends to compare results by everybody having the same goal to work towards.
For each guess, the result is colour-coded in the following way: a green letter means that the letter in that position is correct; an orange letter means that the chosen letter is present in the word, but is currently in the wrong place; while a grey letter indicates that the letter isn’t in the solution. Sharing your results with friends is clever because it doesn’t give away the answer. A grid of coloured squares is generated, which can be shared. The colours represent the same as already described and shows the progress throughout the guesses.
Setting up for this project is easy. First of all, create a directory in which to store your code. Open your favourite IDE, install any Python add-ons that may be required, and create a new file, called Wordle.py. Make sure you have the rich library installed, which we will use to colour-code the output on the screen. You can install the library by opening a terminal and entering the following command:
$ pip install rich
The code will be structured by generating a class, which will contain the game logic, and then some code outside of the class, which allows the game to be interacted with. With that said, let’s take a look at the first code sample:
from rich import console
from rich.prompt import Prompt import random
class Wordle: def __init__(self,wordFile=”words_alpha.txt”):
self.wordList = []
self.loadFile(wordFile)s
Self._randomWord = self.pickRandomWord()
self.guessResults = None
self.guessStatus = None
Reactle is an open source clone of the Wordle game.