Subtle Coolness · chrono index · alpha index


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:

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!