I enjoy a good board game every so often, they are even more fun when you can play them online or as a standalone game. The one game, however, that I can never really play is Scrabble. Simply because I can’t figure out words from the 7 tiles given to you. The best I can do is five letter words, swear words and adding the occasional ‘s’ or ‘ed’ to the end of the words. What I need is a way to cheat, or at least give me a helping hand. As the title of this post suggests there is a very simple and effective way to do this using some commands and files built into Terminal.
The simplest way to cheat at scrabble is to use a word list with plenty of words and then query that list to look for words containing certain letters. There are many ways you can do this and you may develop your own solution, the method I have found is to use a Terminal command called ‘grep’ which finds lines containing a pattern, exactly what we need.
To use this command open Terminal and type the following:
This will change the directory of your current Terminal session. What has been opened is the location of a built in list of words, i’ve mentioned it before here. This list is very long and contains thousands of entries. Although some entries wont be suitable words in Scrabble, it’s a good place to start. If you have a better list of words (such as one downloaded from the internet) you can use the ‘cd’ command and change it to that directory.
To search this list type the following:
grep -x '[ahegilk]*' web2
This command uses the grep program to search through the words file called ‘web2’. The option, -x, makes sure it only matches words which contain the letters within the square brackets. The letters within the square brackets will be searched and found within the words list. The better your words list, the more words you can find, the words list built into your Mac is sufficient, for most words and games. Simply replace the letters in the square brackets with the ones you currently have.
Its worth noting at this point that the star at the end of the square brackets will try and match the letters zero or more times. As a result you may get results where the same letter is repeated multiple times, but you only have one letter to use. You could get round this problem by replacing the star with a question mark, an only allow the letters to appear once. However this doesn’t seem to work on my Mac.
Having a list of words is fine, however in Scrabble you have to add the words onto the board, you need to control how the word starts or ends. This is very simple. To make sure the list only finds words that begin with certain letters, type the following:
grep -x '^be[ahegilk]*' web2
This will find words that contain the letters ‘a h e g i l k’ but begins with ‘be’. The letters at the beginning can be any length, however one or two letters yeild the best results.
You can also search for words that end with certain letters. This, again, is simple. Simply type the following:
grep -x '[ahegilk]*ed$' web2
This will find all the words that end with ‘ed’. Its worth noting that depending on your words file this will yield different results. I know that the words file built into your Mac, that we are using, doesn’t have many words ending in ‘s’ or ‘ed’, so it isn’t that useful. If you have found a bumper words file you may have better luck.
You can combine both commands so you can find words that start and end with a certain letter.
grep -x '^h[ahegilk]*ed$' web2
Very simple and quite useful if you are stuck for words. It does have some limitations such as a limited words file, or repeated letters, but its a simple and quick way to find some words if you are stuck.
If you have found any ways to improve this script, please let me know. I would like to see how this script can evolve and change. With a bit of work it shouldn’t take too long to make a command to find some really long and high scoring words. It may be cheating, but I still end up losing.