• Register

HOW WILL YOU SPELL VICTORY? Embark on an epic linguistic adventure in Letters & Legends, where words and wits are your greatest weapons!

Post feature Report RSS Letters & Legends – making the words work

Letters & Legends contains more than 222,000 words - but how do the enemies fight back? Letters & Legends goes live on PC and Android this Friday 26 April 2024.

Posted by on

feature graphic

Buried in the files of Letters & Legends is a very long list of words. 222,076 words, to be exact. This is the list of words that the player can use in the game, and was gleaned from a number of places – primarily an excellent word list put together by Dr Phillip M Feldman, but then added to with resources such as a list of UK/US spellings and a list of dictionary definitions which further bumped up the word count.

Effectively, what happens in the game is this: every time the player (or the enemy, although this is less important for reasons that may become obvious below) adds a letter to their word, the game takes that word and compares it to its own word list. If the word is 3 or more letters long and appears in the list (but not on a slightly overenthusiastic list of swear words), then it is valid and the game calculates its potential score and feeds that back – the player (if it is their turn) can then press STRIKE and hit the enemy.

All of this is fairly easy – but what about the enemy? How does the game AI find words?

The first thing that happens on the enemy’s turn is that the game takes all of the letters tiles and attempts to find all of the words it can and shove them into a list – let’s call it “wordsFound”. Now, as you might imagine, that can take a while, so this process is spread out a bit, and it does not happen with the full player word list. In fact, the enemy word list is much shorter, coming in at 26,261 words, or what Dr Feldman refers to as his “small” and “overflow” dictionaries – “non-slang, non-technical words that one might expect in the vocabulary of a well-educated 18-year-old (and a few that one probably wouldn't expect)”.

The wordsFound list is then completely shuffled to remove any alphabetical sorting, and then sorted by word length.

Dictionaries

In order to introduce the concept of skill to the AI, each enemy in the game has an assigned maximum number of letters that they can use and a target minimum. We set a random number between these 2 values – that is the length that the enemy will aim for. We can then remove any words from wordsFound that are longer than this length.

As the wordsFound list has already been shuffled, then in theory, the top word in the list should be acceptable for use by the enemy. It is possible (sometimes even likely) that the top word is shorter than the length we are aiming for – this is usually fine, and is the reason that we have a target minimum number of letters, not a strict minimum.

Sometimes, though, we don’t want the enemy to use a certain number of letters, so in this case we keep going through wordsFound until we find a valid word. If there is no valid word, then the enemy gives up and shuffles the letters, ending their turn. And, just to make them feel a bit more human, there is a tiny chance that some enemies will just shuffle the letters anyway.

The only thing left to talk about here is word definitions. Sometimes, the game will underline words, and you can press them to learn the definition. This was something I wanted from the start, but it proved to be pretty tough – not from a coding perspective, but from an assets perspective. The Oxford English Dictionary has an API you can plug into that is super powerful, and I initially had the game hooked up to it, but if you exceed a certain number of calls they start charging for the service (and I needed to make 2 calls every time someone clicked on a word – once to find the root of the word, and again to get the definition). Eventually, that could have been an expensive solution, especially as their Ts & Cs don't allow you to store the definitions you use within the game.

WordsFound

Instead, the 1913 edition of Webster’s Unabridged Dictionary is available on Project Gutenberg, and can be used in projects like this. It’s not perfect: lots of words didn’t exist a hundred years ago, and a lot that did have very different meanings (consider “computer”, or several more problematic words). But actually the written tone of the definitions works well with the style of game that I’ve built. I've tuned the definitions list to within an inch of its life, but I still have some issues around incorrect definitions - it's impossible to fix these alone due to the sheer number of words in the database - I encourage anyone who plays the game to send me incorrect or problematic definitions so that I can change them as we go!

And that’s it – hope that was interesting for anyone thinking of doing something similar! Feel free to ask questions or to comment on your own development experiences here! Letters & Legends goes live on PC and Android this Friday 26 April 2024. You will be able to buy it on the Steam store, on the Epic Games store, on Itch.io, and on Google Play. Add to your wishlist today!

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: