HTML5 game publishing

Word Games in JavaScript: Accents, Unicode and the Letters That Break Hangman

Ñ, ß and the Turkish dotted I break naive word games. Unicode normalization, uppercase that changes length, locale-aware case, grapheme splitting and an on-screen keyboard.

PlayKovo Team4 min read
Games currently available on PlayKovo. Open a title to see its current listing.

Word games are among the most searched browser games, and a lot of the searches are not in English: "juegos del ahorcado", "kostenlose Wortspiele", "jeu du pendu". If you build a hangman, a word search or a crossword in JavaScript with English in mind, the first player who types an "ñ" or an "ß" will break it. This post covers the text handling mistakes that appear again and again in word games, and the small amount of code that avoids them.

Problem one: what is a "letter"?

In English a letter is one character. In Spanish, "ñ" is a letter of its own, but "á" and "é" are usually treated as "a" and "e" with an accent for gameplay. Which treatment you want is a design decision you must make before you write the matching code:

  • Accent-sensitive. The player must type "ñ" and "á". Harder, and awkward on keyboards without those keys.
  • Accent-insensitive. Typing "a" matches "á". The word is displayed with its accents when revealed.

Most hangman games choose the second. But there is a catch: "ñ" is not just "n" with a mark. Treating "ñ" as "n" would let a player guess "n" and reveal the wrong letters in "año". A safe rule is to normalize accents on vowels but keep "ñ" as a distinct letter.

Normalization with Unicode

JavaScript can split accented characters into a base letter and a combining mark. Remove the marks and you get the plain letter:

function baseLetter(ch) {
  if (ch === 'ñ' || ch === 'Ñ') return 'Ñ';          // keep as its own letter
  return ch.normalize('NFD').replace(/[̀-ͯ]/g, '').toUpperCase();
}

Apply the same function to the player's guess and to each letter in the secret word, then compare. Store the original word separately for display.

One more trap: the same visible character can be encoded two ways, as a single precomposed code point or as base plus combining mark. Words you paste from different sources may use different forms. Normalize everything to one form (NFC for storage) when you load your word list, so comparisons never fail for invisible reasons.

Problem two: uppercase changes length

This one surprises people. In German, 'ß'.toUpperCase() returns 'SS', two letters from one. If your game uppercases the word and then indexes by position, the board has the wrong number of boxes.

'straße'.toUpperCase();      // 'STRASSE'  (7 letters, not 6)
'ß'.toLocaleUpperCase('de'); // 'SS'

There are two safe approaches. Either keep ß as a letter in your alphabet and never uppercase it, or choose a single case and store words in it from the start, outside the game. If you transform text at runtime, compare lengths before and after and reject any word that changes.

Problem three: the Turkish dotted I

Turkish has two different letters, dotted "i/İ" and dotless "ı/I". Locale-aware case conversion treats them correctly, but the default conversion does not:

'i'.toUpperCase();              // 'I'  (wrong for Turkish)
'i'.toLocaleUpperCase('tr');    // 'İ'
'I'.toLocaleLowerCase('tr');    // 'ı'

If you support Turkish at all, pass the language to every case conversion. Otherwise the word "kitap" is fine but "ışık" will break your matching.

Problem four: counting visible characters

Some scripts and emoji use sequences of code points to make one visible character. '👨‍👩‍👧'.length is 8. For most word games you will use plain letters, but if you ever display user-supplied text or words from scripts with combining marks, use Intl.Segmenter to split by visible character:

const seg = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
const chars = [...seg.segment(word)].map(s => s.segment);

It is supported in current browsers, but check before relying on it and fall back to Array.from(word) if it is missing.

The on-screen keyboard

Do not depend on the device keyboard. On a phone, the native keyboard covers half the screen and varies by language. Build your own on-screen keyboard from the alphabet of the current language:

  • Spanish: A to Z plus Ñ.
  • German: A to Z plus Ä, Ö, Ü, and optionally ẞ.
  • French: A to Z, with accented letters folded to the base letter.
  • Turkish: the 29-letter alphabet, including Ç, Ğ, İ, Ö, Ş and Ü.

Store the alphabet in the language data, not in code. Our post on localizing your game with the PlayKovo SDK shows how to read the player's language, and canvas text and fonts explains how to make sure your font actually has the accented characters, so "Ñ" does not render as a box.

Word lists

  • Use a list you are licensed to use, and credit it if the license requires it. A dictionary scraped from a site is a legal risk.
  • Remove offensive words. A children's game that serves an inappropriate word is a rejection waiting to happen.
  • Filter by length and difficulty. Short common words for early rounds, longer rarer ones later.
  • Keep one file per language, with one word per line, normalized to NFC.

A test list worth keeping

Put these in a unit test: año, pingüino, straße, ışık, çocuk, œuvre, Zürich. If all of them behave in your game, most real-world words will too.

We use optional cookies for site analytics and site advertising. You can change your choice later. Privacy policy.