RogerBW's Blog

Fun with Letter Tycoon 13 January 2021

I've been playing a lot of Letter Tycoon on BoardGameArena (it seems that losing at Scrabble to my wife is good training for this). I wanted a practice opponent. So I wrote one.

The basic idea is simple enough: given ten available letter cards, make high-scoring words out of them. This gets slightly complicated by the special rules: if you've previously played and staked a claim to the letter X, for example, you can duplicate any one letter in your hand, while if you have a claim on B and your word starts and ends with a vowel it's worth double points. (Otherwise I'd have used the existing an program, which does this sort of anagram finding quite fast.)

So one part of this is clearly "given a word that I can assemble, how much will it score". The other part is reducing the list of dictionary words to only those that one can assemble.

There are existing anagram checkers (it seems to be a standard compsci homework problem) but they take the form "is string A an anagram of string B", i.e. do all the letters appear in whatever order. Not useful.

Generating every possible combination of letters (including subsets of them) is clearly going to be desperately slow.

The trick was to build the right hash function. I assign each of the first 26 primes to a letter (a=2, b=3, c=5, etc.). The hash of a word is the product of the primes of each of its letters. So the same set of letters will produce the same hash, no matter what order it's in.

(Given that I potentially have 12-letter words, and the 26th prime is 101, this could potentially exceed 64 bits – though I could get cunning by assigning the higher primes to less-common letters. Fortunately Rust has a 128-bit integer type so I don't have to.)

Matching those numbers would do for checking whether seaside is an anagram of disease, but not whether I can make aside out of it. (Because if I have a claim on B, as noted above, that might score more points than the longer word.) But using this hash I can simply test:

hash(available) % hash(word) == 0

Consider the combination of letters abc, with value 30 (2×3×5), as my available set. Any word with 0 or 1 of each of a, b and c will have a hash of which 30 is an integer multiple. But if the word has any other prime factor, or an extra factor of one of these, its hash won't evenly divide 30. So I have a quick comparison for choosing which dictionary words to send off to the rather slower score-calculating function.

(Still haven't written the code for the V claim, though, which lets you play two shorter words rather than one longer one – and each of your other claims can only be scored with one of them.)

  1. Posted by John Dallman at 06:52pm on 13 January 2021

    If that had been an example hash function when they were introduced on my computing degree course, a lot of students would have been less confused.

Add A Comment

Your Name
Your Email
Your Comment

Your submission will be ignored if any field is left blank, but your email address will not be displayed. Comments will be processed through markdown.

Tags 1920s 1930s 1940s 1950s 1960s 1970s 1980s 1990s 2000s 2010s 3d printing action aeronautics aikakirja anecdote animation anime army astronomy audio audio tech base commerce battletech beer boardgaming book of the week bookmonth chain of command children chris chronicle church of no redeeming virtues cold war comedy computing contemporary cornish smuggler cosmic encounter coup covid-19 cycling dead of winter doctor who documentary drama driving drone ecchi economics espionage essen 2015 essen 2016 essen 2017 essen 2018 essen 2019 existential risk falklands war fandom fanfic fantasy feminism film firefly first world war flash point flight simulation food garmin drive gazebo genesys geodata gin gkp gurps gurps 101 harpoon historical history horror hugo 2014 hugo 2015 hugo 2016 hugo 2017 hugo 2018 hugo 2019 hugo 2020 hugo-nebula reread in brief avoid instrumented life kickstarter learn to play leaving earth linux liquor lovecraftiana mecha men with beards museum mystery naval non-fiction one for the brow opera parody perl perl weekly challenge photography podcast politics powers prediction privacy project woolsack pyracantha python quantum rail raku ranting raspberry pi reading reading boardgames social real life restaurant reviews romance rpg a day rpgs ruby rust science fiction scythe second world war security shipwreck simutrans smartphone south atlantic war squaddies stationery steampunk stuarts suburbia superheroes suspense television the resistance thirsty meeples thriller tin soldier torg toys trailers travel type 26 type 31 type 45 vietnam war war wargaming weather wives and sweethearts writing about writing x-wing young adult
Special All book reviews, All film reviews
Produced by aikakirja v0.1