Hacker News new | past | comments | ask | show | jobs | submit login
A Wordle clone in 50 lines of Bash (gist.github.com)
271 points by huydotnet on Feb 2, 2022 | hide | past | favorite | 75 comments



This doesn't handle Yellow result correctly. Specifically, if there is a repeated letter in the guess but that letter appears once in the actual word, then the second occurrence will get a Black instead of a Yellow.

In other words, a Yellow result doesn't just mean that letter occurs somewhere. A Black also doesn't mean the letter occurs nowhere.


There seems to be a lot of confusion and different opinions around how repeated letters should be treated; we really need FIDW (Fédération Internationale de Wordle) to make a definitive ruling.


Why not Ffederasiwn Rhyngwladol Wordle given the original programmer is Welsh?


FIWA (Fédération Internationale de Wordle Association)


Association Wordle? What's next, Aussie rules?


But seriously, shouldn't the original Wordle be treated as the reference implementation?


It depends on how you look at it. Wordle is just an online implementation of the game "Lingo", after all. You could argue that Lingo is a letter implementation of Mastermind.

When building a new word grid game, would you take Wordfeud's rules over Scravble's? I don't think I would.


I see your point. But calling it a "Wordle clone" implies it follows the same rules. If you want to change the rules, call it "inspired by Wordle" or something similar.


I used to have exactly this bug in my revolt Wordle chatbot. It's especially annoying when one of the duplicates is in the correct position but get's marked as BLACK since a previous repeat has been marked YELLOW. (I don't think this can be classed as correct behavior)

( My revolt Wordle server: https://app.revolt.chat/invite/2q3NYtM9 )


Yes I just noticed that issue too, still trying to figure out how to fix it :P


I think I just fixed the problem now. And it's still 50 lines :D


If you don't mind a shameless plug, I made this same mistake before and here's how I fixed it: https://github.com/kccqzy/Wordle-Assistant/blob/27d208248b7a...


Here is one way to fix it (but it will push you over 50 lines!)

    remaining="" output=""
    for ((i = 0; i < ${#actual}; i++)); do
        if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
            remaining+=${actual:$i:1}
        fi
    done
    for ((i = 0; i < ${#actual}; i++)); do
        if [[ "${actual:$i:1}" != "${guess:$i:1}" ]]; then
            if [[ "$remaining" == *"${guess:$i:1}"* ]]; then
                output+="\033[30;103m ${guess:$i:1} \033[0m"
                remaining=${remaining/"${guess:$i:1}"/}
            else
                output+="\033[30;107m ${guess:$i:1} \033[0m"
            fi
        else
            output+="\033[30;102m ${guess:$i:1} \033[0m"
        fi
    done
    printf "$output\n"
Explanation:

The variable `remaining` tracks the letters of `actual` that can contribute toward yellows.


I just used this fix, and it worked!!!! Thank you so much!!!


I can feel a round of golf coming on :)


Here's my straight javascript version https://github.com/rrmm/mastermind-word . But it isn't particularly source length efficient.


Please let us know when that is fixed and I’ll play this for sure!


That isn't a bug. In the original if a guess contains a repeated letter and there is only one occurrence of that letter in the target work then the first (on the left) letter will be yellow and the second black.

You can see this with today's word by guessing MOTTO. The leftmost T will be yellow and the rightmost will be black.

Similarly, if you guess TENET the initial T is black but the final T is green.

Today's word is ZBVFG (rot13).


I believe op was saying that the linked reimplementation incorrectly misses the behavior that you are describing.


Bummer. He should have used microservices.

Then the problem at least would have been isolated to that part and the rest of the system in 100% good shape.


Slightly off-topic - I've seen wordle become very popular out of nowhere, and haven't been following it. I'm trying to understand how it would be played. Is this basically Lingo (a gameshow on Dutch TV) but for individuals and as an app?


Why not try it and see for yourself? It's not an app, it's a website:

https://www.powerlanguage.co.uk/wordle/


Fellow Dutchman here, who initially wondered the same. Yes, it's Lingo, but:

- One word every day, same word for the whole world.

- You don't get a start letter

- No ballenbak :-( :-(

- No purpose

Note that as a fellow comment suggested, Lingo itself isn't an original show either. I don't know where the idea originated but certainly not on Dutch TV, so Wordle is simply a new iteration of an old idea. The innovation is in the accessibility and the low-key virality, not the game dynamics.


Not that it matters, but the Dutch Lingo is a local version of an originally US/Canadian game show, filmed in Vancouver but aired in the US, hosted by Ronald Reagan's adopted son. I watched far too many game shows as a child.


I did not know this. I had a look, and I'm surprised that the Dutch version is still running where the British version was only just revived, and the rest aren't running at all.


At this point, I'm actively avoiding finding out what it is to see how long I can remain deliberately out of the loop. For example, I didn't know it was a word game until I read your comment.


That could be a game in and of itself.


Yes, pretty much!


This is great, thank you for creating it! I just added it to my $PATH.


Doesn't work for me on linux using 5.1.8(1)-release (x86_64-pc-linux-gnu). Output with `set -vx` at top (I guessed "fooba"):

    ++ [[ false != true ]]
    ++ guess_count=1
    ++ [[ 1 -le 6 ]]
    ++ echo 'Enter your guess (1 / 6):'
    Enter your guess (1 / 6):
    ++ read guess
    fooba
    +++ echo fooba
    +++ tr '[a-z]' '[A-Z]'
    ++ guess=FOOBA

    [... ÉTUDE  =~  FOOBA  ]]
    ++ echo 'Please enter a valid word with 5 letters!'
    Please enter a valid word with 5 letters!
    ++ guess_count=0
    ++ [[ false != true ]]
    ++ guess_count=1
    ++ [[ 1 -le 6 ]]
    ++ echo 'Enter your guess (1 / 6):'
    Enter your guess (1 / 6):
    ++ read guess


I think it's something with the `/usr/share/dict/words` file, I made a comment in the gist to update the code to the actual english dictionary words, could you try that?


I've replied to your comment on GH, The problem is likely due to how your symlinks are set-up. Which is an artifact of your systems' locales.

Easiest is to check /usr/share/dict with `ls -hal`. Do read the README there, before manually flipping symlinks around, though.


Perhaps allowing the user to specify a word list, and defaulting to some value would be better than suggesting symlinks.

On Tumbleweed, I have no /usr/share/dict/words, but /use/share/dict/american. Allowing the user to set would also be useful if multiple languages are installed, and doesn't change the number of lines of code.

I changed the first line to:

  words=$(grep -Ex '\w{5}' "/usr/share/dict/${wordlist:=words}" | tr '[a-z]' '[A-Z]')
so I can invoke it

  wordlist=american wordle.sh
You can also replace it with $1 (or any other positional parameter):

  words=$(grep -Ex '\w{5}' "/usr/share/dict/${1:=words}" | tr '[a-z]' '[A-Z]')
And invoke it with

  wordle.sh american
In either case, it defaults to /usr/share/dict/words if no list is given. Personally I'd go with the first as you use $1 for guesses, and higher numbered parameters get messy.


In more UNIXy tradition, I'd say either pipe in a file, or pick the default.

That way people can use downloaded files too.


unfortunately using a simple pipe is not going to work, because you're also looking for user input, and you can't do both easily with stdin.


It’s possible to construct parallel (ish) pipeline processing in bash by using file descriptors above 2. See https://mywiki.wooledge.org/FileDescriptor . It’s part of why I always liked Ruby’s .tap() : https://medium.com/aviabird/ruby-tap-that-method-90c8a801fd6...


Really neat!

I wish bash would have a nicer way to address a character in a string though. How hard would it be to use the same syntax as for arrays for it?

By the way, this can be expressed simpler:

    guess_count=$(( $guess_count - 1 ))
As

    ((guess_count--))


I wish bash would have a nicer way to address a character in a string though. How hard would it be to use the same syntax as for arrays for it?

Yes it would be nicer if ${s[i]} worked, instead of needing ${s:i:1}. But there's a pretty fundamental reason for that: bash doesn't have dynamic types of strings and arrays like Python or JavaScript.

Instead everything is basically one kind of string-or-array value. Roughly speaking, strings are values with one element, and arrays are values with more than one.

And ${array} is equivalent to ${array[0]}, which is very confusing. Conversely, you can do ${s[0]} on anything you declared to be a string, and you'll get the whole value back.

Rather than having types attached to values, bash has weird "flags" on the LOCATION for a value. And even that's not the whole story! Evaluation of a variable in bash depends on three things:

    1. the contents of the "cell" / location
    2. the flag on the cell (-a -A)
    3. the operation, e.g. echo $x or echo foo > $x
I might make a blog post about this.

Oil does it more like Python or JavaScript, which is noted here, but this section could use a lot of elaboration:

https://www.oilshell.org/release/0.9.7/doc/known-differences...


Interesting, thanks! Is there a reason for such awkward design of handling variables and types or it's simply some historic choice that's stuck?


Yeah it's definitely a historical thing. Bourne shell / POSIX shell doesn't have arrays, and ksh added arrays and bash adopted the same semantics.

ksh did it in the most "backward compatible" way, with the least amount of work, but which leads to very confusing semantics.

They just "extended" every string so it can have an array part, and then changed a FEW parts of the evaluator, not ALL parts of it. So arrays "decay" to strings in most contexts. They didn't have to touch that code.

It's hard to explain but if you write an interpreter you'll get a feel for this O(M x N) explosion between data types and operations. The amount of work explodes as you add more of either, and shell already has a ton of operations. As mentioned, the evaluation rules for the word $x in 'echo $x' and 'echo foo > $x' are completely different.

There are ways to avoid this, like Python has PyObject and a bunch of protocols. Similarly, Clojure has more protocols than Racket, which the Racket authors have noted -- it works on abstractions and not "concretions". As far as I remember, these come directly from Java interfaces. So yes Java actually adds something to Lisp :)


You need to be careful with that.

If guess_count is zero before the post-decrement the statement will have an exit code of 1. If you have set -e in effect this will kill your script. Depending on the circumstances it might be better to use pre-decrement.

    (( --guess_count ))


Thanks for the hint!


This is awesome. My word was cleam though. That was hard.


You know, like "cleam the bathroon"


Always cleam your inputs!


This article made me check out my dict out of curiosity (american). It has words like "zorch". No. Clue.


And here’s a quick one-liner to solve Wordle puzzles:

https://twitter.com/AlexWeichart/status/1488223629867462664


I do use some grep-fu and sed, awk, and sort to assist though.

It may be cheating (I think it is) but its a great moment to hone some of your gnutools skills.

Some tricks (shameless self-promotion, sorry) https://twitter.com/berkes/status/1481222011980550146 and https://twitter.com/berkes/status/1486296116899504131


Gotta be something wrong here no?

https://ibb.co/2dFhwN4


shuck


Looks like you’re right, but that’s not how Wordle yellow letters are supposed to work.. OP this needs another fix I believe


Are you sure about that? I thought yellow letters were matches but in the wrong placement. Which is what’s happening in your screenshot


The repeated letters handling is subtle. If your guess contains repeats of a letter it will highlight as many of them as there are repeats in the actual word. Any that can be green will be green and other repeats will be marked yellow to make up the numbers (going from left to right). Repeats left over after that are black.

So for example if the target word is QUEEN and you guess EELER (not an accepted guess but let's pretend it is), it would be yellow, black, black, green, black.


I found this explanation helpful when I was developing a clone [1]

[1] https://sonorouschocolate.com/notes/index.php?title=The_best...


Ahhh that makes sense. Thanks for the explanation.


I don’t believe it should show yellow like that when you already have the other letters placed/guessed correctly. Should show black again (or white in this version’s case)..


Thank you so much everyone! The issue with the yellow letters is fixed. Please give it a try again!


Pro tip: if you're using Solarized, change to another color scheme before running it.


Nice work!

With enough determination, anything can be a one liner in bash. /joke


Sweet, I even got it to work on my iPhone with iSH terminal!


finally the words file has a purpose again


Million dollah 50 lines


Really cool!


very cool!


Someone needs to make the obligatory "Wordle clone in 50,000 lines of react/java/nosql/docker/terraform/documentation/7GB of unused dependencies" :)


Web 3.0 Wordle clone, complete with a decentralized score ledger, cryptographicly-signed guess registration, highly anticipated WordCoin ITO, exclusive achievement NFTs, and rug-pull. Bonus if its actually all powered by centralized word database running on Digital Ocean.


with 3306 open to remote root :)


Ha yes! Worldle enterprise edition.

Needs to be microservice-based, obv. A word finder service, a word checker service and a few others for good measure. Reactive architecture, probably Kafka based, for resilience. Also GDPR compliant, CI/CD pipeline, distributed tracing, all major languages covered, dev, staging and production environments.

Bonus points for Java spring, React and Redux.


It may sound hilarious at first but also serve as a good (and extremely simple) learning model.


It may sound logical, but learning many things without going in deep is dangerous. One reason I can think on top of my head is people will abuse and try to use same techniques in every upcoming projects. One of my friend start Kubernetes in digital ocean for simple website is one anecdote i can remember atm.

A little knowledge is a dangerous thing.


Why would they do that when they could Rewrite it in Rust®? Given that it’s ~50× as complex as hello world¹, it should only take about 54,800 dependencies (including 30,000 C binding crates), 1.65 terabytes of disk space for the target folder or even just 190 GB after a clean build, and maybe raise a quad-core CPU to 1567.6°C or a 200 core one to 95.

¹ Written in Rust here: https://github.com/mTvare6/hello-world.rs


Wordle Desktop, a small modern handcrafted Wordle clone built on Electron that uses 3GB of RAM.


A bespoke Wordle clone where each pixel is an image!


This is a 150 lines of Java, compiled to native binaries: https://github.com/ludovicianul/word-guess ;)


Here's my straight javascript version: https://github.com/rrmm/mastermind-word




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: