Jotto (5-letter Mastermind) in the NAO robot
I would like to show how to code the NAO robot to beat us at Jotto (5-letter Mastermind) with python
in Choregraphe
. I will employ a brute force technique that does not require any knowledge of the English language, the frequency of its letters, or smart combinations of vowels and consonants to try to minimize the number of attempts. It goes like this:
- Gather all 5-letter words with no repeated letters in a list.
- Choose a random word from that list—your guess—, and ask it to be scored ala Mastermind.
- Filter through the list all words that share the same score with your guess; discard the rest.
- Go back to step 2 and repeat until the target word is found.
Coding this strategy in python
requires only four variables:
whole_dict
: the list with all the wordsstep = [x for x in whole_dict]
: A copy ofwhole_dict
, which is going to be shortened on each step (hence the name). Note that statingstep = whole_dict
will change the contents ofwhole_dict
when we change the contents ofstep
— not a good idea.guess = random.choice(step)
: A random choice from the liststep
.score
: A string containing the two digits we obtain after scoring the guess. The first digit indicates the number of correct letters in the same position as the target word; the second digit indicates the number of correct letters in the wrong position.attempts
: optional. The number of attempts at guessing words. For quality control purposes.
At this point, I urge the reader to stop reading the post and try to implement this strategy as a simple script. When done, come back to see how it can be coded in the NAO robot.
First, a screenshot of the root
view in choregraph. Note the simplicity of the code.
The first box, ALMemory starter
, is a script box where we define all variables needed to run the program — old school. This is the code of that box:
The second box, Deal with my word
, is a flow diagram containing two boxes:
- One simple script box,
Say the word
. This box performs the random choice fromstep
, stores it in the variableguess
, and increments the value ofattempts
by one. It asks then the player ifguess
is the correct word — and spells it, just in case. - A speech recognition box that expects a yes/no answer.
This is the code of Say the word
:
A switch case is next: if the player says “yes”, the program concludes with the script box Success!
, that states the number of attempts. The code is simple, and it illustrates once again the way to access data from ALMemory
. The only relevant portions of the code on that box are the methods onLoad
and onInput_osStart
:
If the player says “no”, we retrieve the two scores with two flow diagram boxes: First Score
and Second Score
. I will present the structure and code of the latter, and leave the code of the former as an exercise.
The first box is a simple Say that asks for the score. The second box is a Speech Recognition box that expects any digit from 0 to 5. The third is a script box that receives the second box output (as a string
), and performs the following operation (only onLoad
and onInput_onStart
are shown):
And finally, the script box Say the score
. This is where the magic happens. In this box we receive the complete score, and filter all the words from step
to eliminate those that do not share the same score as guess
. We perform that with the following code:
That is all! I hope you enjoy playing Jotto with your NAO. The code can be greatly improved by adding motions in the different steps, or implementing better winning strategies. With brute force, NAO is able to find your word in about 5 attempts (as average, of course). Do you think you can find a strategy that brings this number down to 4 attempts? If so, I would love to hear about it.