Projects

Wored

Wordle replica implemented using vanilla JS and web components.

View / Source
Type

game

Active

2023

Status

Done

Tags

Wordle, TypeScript, Web Components

Projects
Woredgame2023View / Source

Please note, as of 2026 the dictionary API does not work for me.

My take on Wordle. I’ve played it a couple of times before so I figured it’d be a nice challenge implementing it from scratch.

Implementation

The game is divided into two main parts. The core function and a set of custom elements for the user interface. Game state is shared between components and functions using custom events.


The interesting thing about this game is the customizability:

Game Core

The main “loop”. Its main function is to listen for game events, validate user input, cache game state and re-apply cache on page reload.

For example, this is how input validation and game state update are handled at the end of every round.

document.addEventListener(EVT_ROW_SUBMIT_TO_CORE, (event) => {
  // Custom event sent from the game's UI Controller
  const { input } = (event as CustomEvent<{ input: string }>).detail
  // Each round is saved so it can be cached or referenced later
  const round: Round = {
    index: game.rounds.length,
    userGuess: input,
    letters: [],
  }

  // Iterate over each letter in the user input. We are checking if the letter
  // is present in the word (anywhere) or if it's the exact match (index of
  // the letter corresponds with the word)
  for (let i = 0; i < cfg.WORD_LENGTH; i++) {
    const letterUser = input.charAt(i)
    const letterActual = game.word.charAt(i)

    round.letters.push({
      // User input
      letterUser,
      // The correct letter at the index
      letterActual,
      // Orange input, letter is in the word but not at the index
      isPresent: game.word.includes(letterUser),
      // exact match
      isExactMatch: letterUser === letterActual,
    })
  }

  // Save round to main game object
  game.rounds.push(round)

  // Update the UI, highlighting letters in the keyboard and the cells
  Controller.endOfRound(round.letters)
  Keyboard.highlightLetters(round.letters)

  // Check whether the game has been completed (eg. won). We are checking if at
  // least ONE game round has every single letter in the exact match
  checkAndHandleGameOver()
})

Custom Elements

This project served as a way to learn about and work with custom elements, a native web API to create reusable user interfaces.

My takeaway is that I appreciate the power of Vue and other modern frameworks SO MUCH. Reactivity and clean templates are just something I missed a lot.

Something I really like about custom elements is the fact that you can give them custom properties, such as variables, methods etc.

For instance, let’s create a small counter element with its own internal state.

class Counter extends HTMLElement {
  #count = 0

  constructor(initialState: number) {
    super() // Always needs to be called
    this.count = initialState
  }

  get count() {
    return this.#count
  }

  // Create a setter
  set count(value: number) {
    this.#count = value
    // Automatically update UI on state change
    this.textContent = `Count is: ${this.#count}`
  }

  increment(by: number = 1) {
    this.count += by
  }
}

While having all the properties of a normal HTMLElement, it also contains all the custom stuff we added. Which is super powerful.

const counter = new Counter(10)
document.body.append(counter)
counter.increment(10)
console.log(counter.count) // 20

The game utilizes this a bit. Feel free to check out the elements folder and learn how they work and interact.


The game is composed of 3 main components:

I really hate creating lots of HTML elements. So if I can offload that to JS, I will. One would think the keyboard is placed by hand, but it’s completely generated from a single string and then tweaked with CSS.

const buttonString = 'qwertyuiopasdfghjkl$zxcvbnm#'

// And this is how it's turned into an array of HTMLButtonElements
this.buttons = buttonString.split('').map((char) => {
  const el = document.createElement('button')

  if (char === '$') {
    // Enter key
    el.textContent = 'ENTER'
    el.addEventListener('click', this.__enterHandler.bind(this))
  }
  else if (char === '#') {
    // Backspace key
    el.textContent = 'DELETE'
    el.addEventListener('click', this.__backspaceHandler.bind(this))
  }
  else {
    // Normal letter key
    el.textContent = char.toUpperCase()
    el.addEventListener('click', () => this.__letterHandler(char))
  }

  return el
})

Check out the project if you find it interesting!