No Dice? No Problem.

By Loren Segal on February 23rd, 2008 at 7:09 PM

This was my day: Star Wars Monopoly

I invited a friend (who doesn’t happen to be very geeky) over to play some Star Wars monopoly (okay, maybe she is) this afternoon because she had recently realized she had the box lying around her house. We opened up the box and started setting up the game. Got the pieces? Check. Got all the money? Check. Properties? Check. Everything seemed to be going smooth until everything was ready and we were ready to roll the dice… the dice!

Now, I don’t play boardgames often, so it’s not like I can just go to my trivial pursuit box and steal them for a bit. The only other games I have in my house were downstairs hidden behind years of legos and children’s toys (legos are not children’s toys). So, like any geek, the first thing I thought was, "Hmm, what are good ways to generate a random number from 1 to 6 without having to get up?". I looked at my mac…

Requirements Phase

Now first it’s important that we list some base requirements for our Dice application. It was very important that we not have to get up, and since the computer was across the room, this limited our input device. The monitor was also far away, so if the output was text-based, it would have to be displayed appropriately large to be seen across the room.

Design & Implementation

Given the requirements, it seemed that the best choice would be to have the output be spoken out loud by the machine. This would avoid any issues with displaying output from far, and Macs have this stuff built in. Now we were able to choose a language best suited for the task. I thought quickly about doing this in Ruby but remembered that this is really something that AppleScript was built for. Interfacing with the speech module is dead easy, and so is our code:

The AppleScript Dice code

Now we need a way to trigger our dice. Voice recognition also comes built-in on a mac but I couldn’t figure out how to take advantage of it quickly enough so I abandoned that. I tried to think about other input devices that worked from far and settled on the Apple Remote. Now, the remote by default comes with about zero functionality, but I remembered that many people have hacked it up nicely to do pretty much everything under the sun, so I did a little googling and came up with an application called Remote Buddy, which pretty much does just that. Instead of figuring out how to interface specifically with the Script Editor application, I just moved my mouse over the run button (in photo above) and hooked up the play button on the remote to perform a left click:

The Remote Buddy Configuration 

And there we have it. A dice machine. Keep in mind, this all sounds complex but it pretty much happened in under 5 minutes. I go to great lengths to play monopoly.

Update: February 24th 2008: I did a little more googling today while I had more time and came up with the complete-speech-recognition version of my dice app. Turns out the code isn’t that much more complex. Note that we wrap our code in a try because the SpeechRecognitionServer likes to time out.

repeat while true
	try
		tell application "SpeechRecognitionServer"
			set resp to listen for {"roll", "quit"}
			if resp is "quit" then
				return
			else
				set x to ((((random number) * 5) + 1) as integer)
				set y to ((((random number) * 5) + 1) as integer)
				say "You rolled " & x & ", and " & y & ", the total is, " & (x + y)
			end if
		end tell
	on error errmsg
	end try
end repeat

Now all you need to do is tweak your speech recognition settings to not use the "Esc" key to begin speaking. I also turned off the command prefix:

Speech settings


Comments