Building a Better Twister Spinner
by William Jackson on 2015-01-04
I got my wife Twister for Christmas. While we were playing it on Christmas morning, she remarked that the spinner was not great. It seemed to land on Left Foot more than we expected.
My first thought was, “I could write a quick Python script to get a more random spinner result!” So I grabbed my computer and threw this together in about a minute:
import random
parts = ['Right Foot', 'Right Hand', 'Left Foot', 'Left Hand']
actions = ['Red', 'Yellow', 'Green', 'Blue']
while True:
input('{} {}'.format(random.choice(parts), random.choice(actions)))
This is about the bare minimum I could get away with: choose a random body part and a random color and print the combination. We played this way for a while before my wife had an amazing idea.
“It would be really great if the computer could just read the spin result out loud,” she said.
I immediately thought of say
, because my five-year-old daughter loves to play with say
on the command line and
make my computer speak. So another minute or two later I had modified my script:
import random
import subprocess
parts = ['Right Foot', 'Right Hand', 'Left Foot', 'Left Hand']
actions = ['Red', 'Yellow', 'Green', 'Blue']
while True:
move = '{} {}'.format(random.choice(parts), random.choice(actions))
subprocess.call(['say', '-v', 'Samantha', '-r', '150', move])
input(move)
Now we had nice random spins and the computer calling out the moves. But the script still required someone to press Enter after each move. The final tweak was to make it automatically call out moves after a few seconds:
import random
import subprocess
import time
parts = ['Right Foot', 'Right Hand', 'Left Foot', 'Left Hand']
actions = ['Red', 'Yellow', 'Green', 'Blue']
while True:
move = '{} {}'.format(random.choice(parts), random.choice(actions))
print(move)
subprocess.call(['say', '-v', 'Samantha', '-r', '150', move])
time.sleep(10)
At this point I was pretty happy with the behavior of my quick little script. But I took the opportunity anyway to flesh it out a bit and allow command line options to configure various things. It was a good chance to learn how to use the argparse module.
The final script is on GitHub. It includes command line arguments for:
- allowing “in the air” as a valid alternative to a color (our version of Twister had this move on the spinner);
- specifying the number of seconds to wait before automatically spinning again, or requiring someone to press Enter after each spin; and,
- making the computer speak the spins out loud (only on platforms with the
say
command).
This script was a fun little diversion, and now my wife and I can play Twister without needing a third person to operate the spinner!