You are currently viewing the United Kingdom version of the site.
Would you like to switch to your local site?
14 MIN READ TIME

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.
Unlock this article and much more with
You can enjoy:
Enjoy this edition in full
Instant access to 600+ titles
Thousands of back issues
No contract or commitment
Try for 99p
SUBSCRIBE NOW
30 day trial, then just £9.99 / month. Cancel anytime. New subscribers only.


Learn more
Pocketmags Plus
Pocketmags Plus

This article is from...


View Issues
Linux Format
September 2024
VIEW IN STORE

Other Articles in this Issue


LINUX FORMAT
LINUX FORMAT
The #1 open source mag Future Publishing Limited,
WELCOME
MEET THE TEAM
This issue, we’re introducing readers to Linux Mint 22 and its Cinnamon desktop; in the name of open source choice, what other desktop should people try and why?
Lightning strikes
Linux isn’t a magic bullet. It still has
REGULARS AT A GLANCE
CrowdStrike issues hit Microsoft and Linux
Microsoft wasn’t the only victim of faulty CrowdStrike updates. Weeks before the worldwide outage, Debian and RHEL were also hit.
Nvidia officially goes open source
The R560 driver marks Nvidia’s full transition to open source GPU kernel modules.
Google backs down on cookies
Chrome will allow users to make “informed choices”.
SUITE PRIVACY
Italo Vignoli is one of the founders of
EARLY ADOPTER
David Stokes is a technology evangelist at Percona.
Jellyfish devs say help others
Lead developer Joshua Boniface has asked users to support clients instead.
Switzerland is open source
New law requires all government apps to be open source.
Ext4 gets faster
The latest Linux 6.11 comes with performance optimisation for ext4.
Distro watch
What’s behind the free software sofa?
ATOMIC STEAM
Ludovico de Nittis is a software engineer at
FILTER TIPS
Jon Masters is a kernel hacker who’s been
Kernel Watch
Jon Masters summarises the latest happenings in the Linux kernel, so that you don’t have to.
Answers
Got a burning question about open source or the kernel? Whatever your level, email it to answers@linuxformat.com
Mailserver
WRITE TO US Do you have a burning
HotPicks
THE BEST NEW OPEN SOURCE SOFTWARE ON THE PLANET
THE LINUX SMART HOME
We aim to keep the lights on the smarter way with Home Assistant, Zigbee and low-cost IKEA kit.
REVIEWS
HighPoint Rocket 1608A
For when Shane Downing really needs 56GB/s of storage speed.
Q4OS 5.5
Nate Drake is on cue to explore the latest Q4OS and comes away amazed at its speed and simple setup.
Pardus 23.2
Nate Drake offers readers a chunk of Turkish Delight as he tours this Debian-based distro, coded with full government approval.
Nobara Project 40
Nate Drake delves into this modified Fedora distro. Is this the ultimate in Linux gaming or should you say “no Nobara”?
Mabox Linux 24.07
A spin of Manjaro, which is a spin of Arch, is leaving the normally centred Nate Drake in a right tizzy.
Men of War 2
Often found wondering what war is good for, Jake Tucker is reminded by The Management that it looks after his pay cheque and pension fund.
ROUNDUP
Remote desktops
Ever wish you had the desktop of another computer right in front of you? Michael Reed checks out five applications that can do this and more.
The verdict Remote desktops
We’ve ended up with five options that can
FRESH MINT 22!
Fresh Mint 22!
Linux Mint is in season and Jonni Bidwell has been sniffing it voraciously. Join him on a fragrant voyage of discovery.
Mint is for its users
Discover why Linux Mint is your new favourite Linux flavour.
How to install Mint
Get Mint booted, explore the live environment, then get this herbaceous powerhouse installed forthwith.
Getting to know Mint
Explore the freshest of fresh installs and tweak it to perfection.
X marks the apps
Perfect your install and discover the new apps on the block.
Pi USER
Pi Foundation releases AI teaching guidance
Worry no more about how to teach about ethical generative AI – the Pi Foundation has you covered.
Fritzing 1.0.3
Les Pounder spends most of his work hours looking at circuit diagrams, then carries on doing the same thing after work.
Sovol SV08
Big, super-fast and open source, it looks as though Denise Bertacchi has found her perfect printer!
Simulate your Pi Pico projects with Wokwi
Claiming he’s saving resources, Les Pounder simulates an electronics project because he is too lazy to get up and build it for real.
IN DEPTH
PIPEWIRE
Matt Holder delves into the depths of our audio stack to learn about PipeWire and what came before it.
TUTORIALS
Try a tiny text editor
Shashank Sharma lives by the try-it-before-you-dismiss-it code. That’s how he finds new tools. It’s also how he discovered a fondness for kimchi.
Navigate your way around the filesystem
LINUX BASICS
Geek out with NixOS
Mats Tage Axelsson shows you how easy it is to use NixOS and the challenges of installing new applications. All 100,000 packages.
Expand your storage with high-speed SSDs
RAID
Upgrade it: Solid-state drives
RAID
Take local control of multiple AI models
OLL AMA
Record your games on the Steam Deck
STEAM DECK
ADMINISTERIA
Get to grips with crontabs on Linux
Ever the busy boy, Stuart Burns needs things to run on time, so he’s here to show you how to set up schedules.
AngelVPN
Always a good boy, Sam Dawson thinks he’s rather smitten with this devilishly good service.
PureDome VPN
A VPN service that’s aimed at business types like Mike Williams, who prefers to do his private business in private.
WHATEVER HAPPENED TO SYMBIAN?
The Symbian operating system had a lot going for it, and even dabbled with open source, so why did it fail? Mike Bedford investigates.
CODING ACADEMY
How to build a 6502 assembler
6502
Chat
X
Pocketmags Support