How to win at Ruzzle with Python

Daniele Gentili
3 min readMay 16, 2020

You can follow this as a tutorial (GitHub repo) , skip the code or just watch the result of me showing how much of a sore loser I am:

code in action

Have you ever experienced the utter frustration of not being able to beat someone at something no matter how hard you tried?…Personally I did not care much for my Ruzzle ability until a friend started poking fun.

Then during this pandemic my natural propension towards being a sore loser mixed with too much spare time set the stage for a useless achievement and getting my revenge.

What are the ingredients to build a Ruzzle bot?

  • Python installation (if you don’t have one you can install Anaconda)
  • PyAutoGui, a python library to control your mouse (follow the link for installation instructions with anaconda)
  • Bluestacks, an android emulator
  • Vocabulary text file of your language of choice

What are the main steps?

  1. Build a python representation of the game frame
  2. Defining the search function for words
  3. Defining the mouse control to draw the words in the app
  4. Playing the game

Building a Python representation of the game frame

Basically we can represent the game as a frame of 4x4 cell objects having features like the letter that they contain, their neighbours, their location on the pc screen, etc.

To do that we can create a Cell class. Using Cells we can create a Frame object. Code:

Defining the search function for words

The search function is the most crucial aspect of the software. It is the way in which we find words in the Ruzzle frame.

There are two main ways to approach the problem:

  1. By generating words one by one and trying them in the frame
  2. By checking all the words against the frame

For a human it is much more intuitive to do the first, as we do not have enough RAM to load all the words we know at once and we are naturally very good at abductive reasoning (i.e. “this word might fit, ah almost, this other one then…”)

For modern computers though the few MBs necessary to store all the words of a language are hardly a problem.

Moreover it is possible to reduce the number of Italian candidate words (i.e. words that might fit in the frame) by 90–95% by simply removing:

  • words containing letters not in the frame
  • words shorter than 2 or longer than 16 letters (game limitations)

Therefore while memory usage is ever so slightly higher, the computational savings by using the second approach are paramount.

How to implement it? A recursive function is the way:

This will be used later to check, for each letter in the frame with which a word might start, whether it is possible to continue the word in one of the neighbouring letters.

Defining the mouse control to draw the words in the app

Now it is time to start implement logic to draw the word in the game frame.

Basically we need to click the first letter and then drag to the next letter to release the mouse only when the last letter has been reached.

To avoid problems with super-human speed it is best to implement a delay in dragging speed and a short pause between words.

Playing the game

In order to play the actual game, the logic above needs to be put to use and a fundamental input needs to be passed. I also added a choice for how many words to draw and whether words should be drawn starting from the longest ones first or in random order. User is asked for:

The user inputs a Ruzzle frame represented as a string (read left to right, top to bottom)

Conclusion

I hope you enjoyed this! I definitely enjoyed making it even though I felt the pettiness of my desire to win…Just kidding it felt great finally winning.

--

--

Daniele Gentili

Science’s neutrality must be questioned when it affects people. The Qs we seek to answer show our interests. Knowledge isn’t accumulated but surfaced/submerged