Tuesday, August 7, 2012

[How to] Write a code to win Google doodle hurdle game (Cheat sheet)



Today it was exciting day when I saw everyone playing Google doodle game "Hurdle" So I also tried and got my best as 10.7.

But then I realized that if I can automate keypress events I can reduce lots of efforts, Thus I started writing and finally came up with following code

 
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>

#define PRESS_UP 0
#define PRESS_DOWN 1
#define PRESS_LEFT 2
#define PRESS_RIGHT 3
#define PRESS_ENTER 4
#define PRESS_ESC 5
#define PRESS_HOME 6

Display *display;
unsigned int keycode;

int press(int key){

if(key == PRESS_UP)
keycode = XKeysymToKeycode(display, XK_Up);
else if(key == PRESS_DOWN)
keycode = XKeysymToKeycode(display, XK_Down);
else if(key == PRESS_LEFT)
keycode = XKeysymToKeycode(display, XK_Left);
else if(key == PRESS_RIGHT)
keycode = XKeysymToKeycode(display, XK_Right);
else if(key == PRESS_ENTER)
keycode = XKeysymToKeycode(display, XK_Return);
else if(key == PRESS_ESC)
keycode = XKeysymToKeycode(display, XK_Escape);
else if(key == PRESS_HOME)
keycode = XKeysymToKeycode(display, XK_Home);

XTestFakeKeyEvent(display, keycode, True, 0);
XTestFakeKeyEvent(display, keycode, False, 0);
XFlush(display);
return 0;
}

int main(int argc, char *argv[]){
display = XOpenDisplay(NULL);
while(1)
{
press(PRESS_LEFT);
usleep(42000);
press(PRESS_RIGHT);
usleep(42000);
}
}

TO compile this code use following command
gcc finename.c -lX11 -lXtst

 

With this I started getting 0.5 seconds with different sleep values, Finally I decided to stop at 42000 which makes it believable and I got the score of 9.9.
Please not that you have to jump through all the hurdles to get 3 stars thus limit of usleep must be accurate and manageable.

EDIT: You can use the same code for "Google doodle slalome canoe" with usleep value 2000 to get score in range of 14-15

14 comments:

  1. How to implement it on google doodle?

    ReplyDelete
  2. Compile this program and run it and go on any browser and start running it will automatically run and you just have to press spaces in between to control

    ReplyDelete
  3. puneet can u give steps of compile the program

    ReplyDelete
  4. this code for linux, isn't it?

    ReplyDelete
  5. I compiled in Linux with X11 libraries ... you can do the same in windows using Devcpp or codeblocks with X11 libraries for windows 

    Simple would be to implement it with java using 
    http://stackoverflow.com/questions/4546214/how-to-fire-tab-key-event
    using robot keypress

    ReplyDelete
  6. Yes I did it for Linux you can write your own for any OS in java .... Or may be I will update this post soon with java code :)
    Hint is to use Robot keypress

    ReplyDelete
  7. This code rocks!!
    Thanks
    vsdpsingh :) Cheers to you

    ReplyDelete
  8. If we dont want any star, and remove the sleep command. This will give you 0.9 sec Awesome trick!!

    ReplyDelete
  9. [...] Surabhi’s blog « [How to] Write a code to win Google doodle hurdle game (Cheat sheet) [...]

    ReplyDelete
  10. Write a simple java code :

    import java.awt.*;
    import java.awt.event.KeyEvent;
    public class Hurdle{
        public static void main(String[] s) throws AWTException
        {
            Robot robot = new Robot();
            while(1)                                    
            {
                robot.keyPress(KeyEvent.VK_LEFT); 
    robot.delay(500); 
                robot.keyRelease(KeyEvent.VK_RIGHT);
    robot.delay(200); 
            }
        }
    }
    Something Like this will work I hope

    ReplyDelete
  11. hi my name is prath mesh i like this things but now i want to sbmit doodle.

    ReplyDelete