Last week, u/tberghuis posted a Wordle clone using Kotlin Compose (link to their post), and I thought, what a great idea!
Wordle is a fun project you can learn quite a bit from implementing, and it’s simple enough you could actually do a very basic loop in a text console in just a few hours (if you don’t worry about the UI). Just ask the user for their next word guess (using readln
), compare it against the target word, rinse and repeat.
I have a terminal UI library that I maintain, though, so I took it a step further, adding colors and additional features like contrast mode and hard mode:
You can find the project for the code here. As it’s just an example project and not some final production codebase, there’s only a single source file in it, main.kt. It clocks in at 462 lines of code to accomplish everything you see above, and it took about a day.
Code Highlights
The Wordle Heartbeat
If I could point to any part of the code that is the heart of Wordle, it would be these lines that check your guess against a target word. Honestly, there’s not too much too it — the one trick you have to realize is that you have to consume all exact matches first before checking for near match tiles. Why this is so I leave as an exercise to the reader π.
Anyway, it takes two sweeps. NYTimes can I have my seven figures now?
Hard Mode
Perhaps the coolest part is how elegant hard mode is to implement. If you’re not aware, hard mode adds an additional restriction — any new word you add must not contradict your previous guesses. Sounds tricky, but you can find my algorithm for that here, which is actually just a one liner! (Followed by an additional 20 lines of converting the failure into readable text to present to the user… UI is hard…. π ).
The insight to hard mode is to realize that you have already written a solver, of course — “if I play XXXXX, does it match YYYYY?”, but then when the user types a new word, you just check all previous guesses against the new guess as if it were the target word itself. It’s kind of hard to explain why this is within a reddit post that’s already getting too long, but if you go through a few concrete examples at your desk, you’ll have a π€― moment.
Wordle dictionaries
For you coroutine lovers out there, here’s a bit where I download Wordle dictionaries before starting up.
The all-words
set was taken by inspecting Wordle itself, and the common-words
set was created by finding a list of words online sorted by how common they are and then filtering out ~2000 of them using the all-words
dictionary. I’ve uploaded both dictionaries to GitHub in case anyone wanted to use them as well in their own project.
Trying Wordle Demake Yourself
My Wordle implementation exists less to be shared for easy play at this point and more to act as a demonstration of what my TUI library, Kotter, can do, and what code written using it looks like.
But, if you just wanted to give this a quick try and play some Wordle like it’s 1999, the easiest way is to clone and compile the project in a terminal. It shouldn’t take long:
git clone https://github.com/varabyte/kotter.git && cd kotter ./gradlew :examples:wordle:installDist ./examples/wordle/build/install/wordle/bin/wordle
(And if you find any bugs πππ or BS words in the official Wordle dictionary π please report them here, thanks!)
submitted by /u/bitspittle
[link] [comments]