Automating Google Hurdles 2012 – xautomation and xte
Today, Google’s home page had a little game where you help an athlete run and jump hurdles. A coworker and I were racing to try to beat each other. I wanted to win! With a little Linux know-how, it is fairly easy to automate the game.
xautomation and xte
Xautomation is a tool that allows the user to automate inputs that feed to the X server. In other words, you can create scripts or one-liners to move your mouse around, click, and type on your keyboard. The usage is fairly simple. The program we are interested in is called xte. It is part of the xautomation suite. It accepts commands as arguments or from stdin. Check out the man page A few simple examples:
#Type "Hello World" xte 'str Hello World' #Type "Hello World" and press enter xte 'str Hello World' 'key Return' #Move mouse to position x:160,y:700 and left click xte 'mousemove 160 700' 'mousedown 1' 'mouseup 1' #Move mouse, click, drag, release from stdin #When sending input from stdin, send each command on a new line without single quotes echo -e "mousemove 500 500\nmousedown 1\nmousemove 700 500\nmouseup 1" | xte
The game (you lost it!)
The game requires you to press keys alternating between left and right to move your athlete. You then use the spacebar to jump over hurdles. The hurdles seem to show up at random intervals. To keep things simple, I will ignore the hurdles and just plow straight through them! My solution:
sleep 5 && python -c "print 'key Left\nkey Right\n' * 1000" | xte
I am using a small bit of python code to generate 1000 left-right key press pairs. This was faster on my machine than running
xte 'key Left' 'key Right' in a bash loop. These are then fed to xte to run. This is delayed by 5 seconds to allow me to focus (click on) on my browser and the flash applet before it starts charging! I have noticed that because this completes the track so quickly, hurdles are often not generated and you can get 3 stars. It seems if you hit even one hurdle, your can only get up to two stars.
There are probably many ways to optimize this. My best time is 0.5 seconds with 3 stars using the above code. Can you beat it?
Categorised as: Gaming