When I need a break, I like to relax by playing a fun puzzle game or word game. I enjoy the British quiz show Countdown—this features two games, a Numbers game and a Letters game, which are both fun to play as a “break” when I’m working. Actually, I prefer the other version of the show with the comedians, 8 Out of 10 Cats Does Countdown, but both shows feature the same games.
The Letters game is a simple puzzle where you need to find the longest word using a list of nine random letters. Just pick a selection of consonants and vowels from the Scrabble letter tiles, and see what words you can come up with. With a bit of creativity, you can also play this game at the Linux command line.
Letters by frequency
I found a list online of the letter frequency in Scrabble, which I used as my starting point to play this word game on Linux:
A 9
B 2
C 2
D 4
E 12
F 2
G 3
H 2
I 9
J 1
K 1
L 4
M 2
N 6
O 8
P 2
Q 1
R 6
S 4
T 6
U 4
V 2
W 2
X 1
Y 2
Z 1
Save that to a new file called letters.freq so we can use it as the starting point to play the Letters game.
Print that many of each letter
We can use the frequency distribution to generate a random selection of letters to play with. To start, we need a short awk command that reads the letters.freq list and prints that many of each letter. This is a basic for loop, where the awk command uses the second field of each line as the number of times to print out the first field. That is, for C 2, the command would print the letter C two times:
$ awk '{for (n=0; n<$2; n++) {print $1}}' letters.freq | head
A
A
A
A
A
A
A
A
A
B
As you can see in this output, the letter A has a frequency of 9, and the letter B has a frequency of 2. The head command only prints the first ten lines by default, so we can see all nine instances of the letter A, and the first instance of the letter B.
To print out a list of all vowels from the letter frequency list, we only need to add a regular expression to the awk command, so we print only vowels:
$ awk '/^[AEIOU]/ {for (n=0; n<$2; n++) {print $1}}' letters.freq | head
A
A
A
A
A
A
A
A
A
E
We can negate the regular expression using ^ before the list of vowels to print only non-vowels as the output. This generates a list of just the consonants:
$ awk '/^[^AEIOU]/ {for (n=0; n<$2; n++) {print $1}}' letters.freq | head
B
B
C
C
D
D
D
D
F
F
Pick a few random letters
I find a mix of five consonants and four vowels makes for a good enough selection that I can find some decent words. To select only four random entries from this list, we can use the shuf command to randomize or shuffle the results. The -n option makes shuf print only a few lines, such as shuf -n 4 to print only four random vowels and shuf -n 5 to list five random consonants:
$ awk '/^[^AEIOU]/ {for (n=0; n<$2; n++) {print $1}}' letters.freq | shuf -n 5
T
T
G
T
S
$ awk '/^[AEIOU]/ {for (n=0; n<$2; n++) {print $1}}' letters.freq | shuf -n 4
O
E
I
I
Putting it all together
To play the word game from the Linux command line, I like to put the commands into a script. Let’s call it letters.bash. Then I can run one instruction to generate a list of four random vowels and five random consonants. Collecting this in a script also makes things tidier, because we can save the letter frequency list (letters.freq) in a file somewhere like $HOME/lib/letters.freq and reference the path. This means we can run the script from anywhere, and the script will always be able to find the letter frequency file:
#!/bin/bash
list=$HOME/lib/letters.freq
awk '/^[^AEIOU]/ {for (n=0; n<$2; n++) {print $1}}' $list | shuf -n 5
awk '/^[AEIOU]/ {for (n=0; n<$2; n++) {print $1}}' $list | shuf -n 4
Save this as letters.bash and make it executable. Then run the script to generate a list of five random consonants and four random vowels:
$ chmod +x letters.bash
$ ./letters.bash
Y
R
T
T
B
E
A
I
E
Try playing the game for yourself and see what words you can come up with. The rules for making words are simple: use letters only once, but no abbreviations and no proper nouns. Use regular words only. If you’re not sure if a word is a proper noun, use the Linux /usr/share/dict/words file to look it up.
For example, with the above list of letters, I was able to find TIRE (4 letters), BEAR (4 letters), TRIBE (4 letters), and BITTY (5 letters). The longest word I could find was BATTERY (7 letters).
Play it your own way
This shows a basic way to generate nine random letters to play the Letters game. The script prints each letter on a separate line, as five consonants and four vowels.
One improvement you can make to the game is to use the shuf command to “shuffle” the nine letters into a random order, and the fmt command to “format” them into a single line of output. This makes the letters easier to see. For example, you can add these extra commands on the command line like this:
$ ./letters.bash | shuf | fmt
I E R P J U V S E
For example, I can find the words PIER (4 letters) and PEER (4 letters). The longest word I could find is REVISE (6 letters) but maybe you can find a longer one.
A better way would to be to change the script to do this for you. I’ll leave that as a challenge for you to make the game your own, and make other changes, so you can play it your own way.