Settings

Theme

Show HN: A simple Wordle clone in 60 lines, using Hyperscript

arhamjain.com

111 points by ajusa 4 years ago · 52 comments · 1 min read

Reader

Hello HN! I've been playing a lot with https://hyperscript.org/ recently (not to be confused with the other hyperhype hyperscript). I threw together a quick Wordle clone in an evening to see what it would look like using this language. The main functionality that is missing is checking for invalid words. The word dictionary is also very small, so it's very easy.

The goal here wasn't really to create a good version of Wordle, it was to build 80% of Wordle in a different language to see what it looks like. Turns out, it looks pretty good! Stuff like using CSS rule precedence to highlight the squares, CSS selectors to figure out which key to highlight, and using the DOM to keep state are all really natural in Hyperscript. I highly suggest going to the site and viewing the source!

jaidhyani 4 years ago

This is awesome, but there's a slight bug in this implementation: Wordle won't count additional instances of a letter as wrong-position, it'll just flag them as wrong. For example, if the word is TRACE, and I guess TRACT, the first T will be green, but the second T should be black, not yellow. In this implementation, at the moment, TRACT would return GGGGY, but in Wordle it would be GGGGB.

I actually made the same mistake in a Wordle solver I was writing recently - it's easy to miss, since Wordle doesn't make this explicit and you have to infer it yourself.

  • ajusaOP 4 years ago

    Oh wow, I never noticed that myself. I'll fix it once I'm at my computer, thanks for pointing it out! The fix will probably be something like

        if letter not in (.bg-warning in first .guess).innerText then add .bg-warning
    
    Haven't tested as I'm on my phone, but seems like a simple enough fix.
    • anonymoushn 4 years ago

      I don't think it's that simple. For HOUSE, guessing BEERS should get you WYWYW

    • Aethylia 4 years ago

      I have been trying to figure this out myself. The algorithm I'm using is to colour yellow only if the count of that letter before that occurance in the guess, is less than the count of that letter in the target word.

      • joeframbach 4 years ago

        Looks good, here are some test cases off the top of my head.

        guess: "SASSY", target: "BRASS", expect either "YYBGB" or "BYYGB". I believe your algorithm would return "YYYGB".

        guess: "BRASS", target: "SASSY", expect "BBYGY"

        guess: "ASSET", target: "BRASS", expect "YYYBB"

        guess: "BRASS", target: "ASSET", expect "BBYYY"

  • xmonkee 4 years ago

    Haha, I made the exact same mistake in my wordle solver. Well, not a mistake since that's what the worlde docs say.

aldebran 4 years ago

I found a bug. When there is a repeated character in the guess but not in the word, you’re given an incorrect color key.

E.g. Word was GROUP I guessed SPOON Output was green O at location 3 and yellow at 4. Happened for a couple more guesses making me think there is another O eventually to realize there are no more spots and it’s a bug.

  • ajusaOP 4 years ago

    Sorry for that (and thanks to all the folks that pointed out the word matching code was wrong). I've pushed an updated version, it's 63 lines now.

    Turns out, HN finds and reports bugs much faster than family and friends :)

  • joeframbach 4 years ago

    wordle-play.com fails the test. wordlegame.org passes. Implementations disagree, but I agree with how you are defining it should work.

huhtenberg 4 years ago

Not take away from the brevity of the code, but these 60 lines translate into 168KB of JavaScript.

  • dane-pgp 4 years ago

    Wait until you find out how big the browser that runs that code is!

    • ivanbakel 4 years ago

      This is a blithe statement - the browser isn't downloaded whenever a user navigates to the page. 1/6th of a MB for a page this simple is pretty substantial.

    • antihero 4 years ago

      Do you download the browser on every request?

      Of course 168kB is still nothing but your comparison is flawed.

  • irq-1 4 years ago

    _hyperscript_web.min.js is 84.1 KiB (86,093 bytes) and when zipped it's 22.1 KiB (22,641 bytes)

    • ajusaOP 4 years ago

      Looks like they doubled the size by accident, in the network debugger the first is a 302 that resolves to the actual file. It shows 84kb twice because of that.

d--b 4 years ago

Wow, first time I see hyperscript. It is so remote from what I am used to, I have no idea how to read the code in details, but what it does is completely self-explanatory.

aloknnikhil 4 years ago

On Safari on iOS, at least, typing fast will accidentally zoom into that part of the page instead of registering it as a character press.

But pretty neat implementation otherwise. Awesome work!

  • ajusaOP 4 years ago

    Yep, I have the wrong meta tag I believe:

            <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    
    That should fix it, I'll push a new version after fixing the word matching issue others have pointed out. I underestimated Wordle's complexity :)
  • recursivedoubts 4 years ago

    i see it slowing down on mobile safari as well

    will be a great chance to optimize the hyperscript runtime for mobile

martini333 4 years ago

Does not check for real words.

  • e67f70028a46fba 4 years ago

    “The goal here wasn't really to create a good version of Wordle, it was to build 80% of Wordle in a different language to see what it looks like.”

    • ajusaOP 4 years ago

      Yup, exactly. I've considered adding a bloom filter check for words but I've accomplished what I wanted to try out. Maybe if I'm ever interested in the future!

      It'd be very easy to check against just a list of words, but that'd require more storage and be slower than a bloom filter.

madhato 4 years ago

I made a simple Wordle clone in ~70 lines without using Hyperscript.

https://dougmcleod.ca/share/wordle.html

dane-pgp 4 years ago

This isn't just a beautiful demonstration of Hyperscript, it could also be a great introduction to what it can be like when writing for the web platform, or to coding in general. Well done!

fouc 4 years ago

I'm confused. Hyperscript is supposed to be an alternative way to writing JSX, good for skipping the babel/transpile step. Such as the React h() function. For example:

    h('header', { id: 'my-header', className: 'header' },
      h('div', { className: 'logo' },
        h('a', { href: '/' }, 'mtsknn.fi')
      )
    )
Hyperscript.org doesn't seem to be related to this at all?

https://mtsknn.fi/blog/hyperscript-hyperior-alternative-to-j... https://dev.to/fromaline/hyperscript-the-hidden-language-of-...

  • speg 4 years ago

    You are correct. Hyperscript.org is a companion library to htmx; they are totally different from the JSX Hyperscript.

rkimb 4 years ago

Nice work! I spent some time playing with this today and used it to build a solver algorithm. It's a greedy search through the possible combinations that prioritizes words based on letter frequency and uses the hints provided to whittle down the search space.

Though not enforced by the Hyperwordle clone, the script plays the game on "hard" mode by default where all letter hints must be included in subsequent guesses. Ironically, this constraint made the algorithm more efficient, easy mode tends to take longer and fail more frequently!

Anyone have a better algo yet? https://github.com/rgkimball/wordlebot

tardyp 4 years ago

Wordle will become the new todolist

e12e 4 years ago

Nice. Is there a link to the 60 (63) lines of source code? It's not obvious on mobile at least?

Ed, nevermind:

> I highly suggest going to the site and viewing the source!

For chrome on Android:

  view-source:https://arhamjain.com/hyperwordle/
olah_1 4 years ago

> build 80% of Wordle in a different language

I have been trying to do the same thing! I was looking for projects on github that I could adapt to a new language. Sadly, most of them require a server. It seemed to me that this game could pretty easily just run on the client alone.

I will dig into yours and see how it can be adapted to different languages too :)

  • mcintyre1994 4 years ago

    I think wordle does just run indefinitely on the client. It has a word list in its JS which has the list of winning words in order, so you can see the upcoming ones. And the current word is somewhere in localstorage as well. Obviously not much fun if you poke around there too much, but my guess is it just checks the current date and indexes the words array and sets all the state from there.

recursivedoubts 4 years ago

holy smokes, that's some seriously sweet hyperscript

_Bruno42 4 years ago

Doesn't work for me, getting a JS error when I'm entering a word:

hyperscript.org@0.9.4:1 'first <:empty/> in first .guess' is null

phkahler 4 years ago

Doesnt validate input as words,

jFriedensreich 4 years ago

every key press takes 4 seconds to update the ui, if this is supposed to be a showcase for hyperscript, its not doing too well.

  • jFriedensreich 4 years ago

    if it helps for reproducing: i pressed keys too fast wich was interpreted as a double tap in safari and triggered a zoom in. After zooming back to normal, the lag was even worse.

  • recursivedoubts 4 years ago

    what machine/browser are you on?

    the hyperscript runtime is defintely... pedestrian

    • jFriedensreich 4 years ago

      i tried on ios safari, which is admittedly not a great browser

      • ajusaOP 4 years ago

        Yeah, that must be a Safari thing. I pulled out my Nexus 5 (circa 2013) and everything feels instant. I don't have any working iOS devices lying around to test on unfortunately.

stefs 4 years ago

it's buggy and accepts bogus words

pygy_ 4 years ago

Hyperscript already has an established meaning in this space, as a pure JS-based syntax alternative to JSX.

See https://github.com/hyperhype/hyperscript and all its descendants, like Mithril, HyperApp or Sinuous.

Could you please think of another name ?

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection