Wednesday, August 8, 2012

[How to] Write a code to score 39 with 3 stars in Google doodle basketball (cheat sheet)



Today again it was pleasant surprise by Google on Basketball day, they posted a doodle to let us play basket ball. And again I thought to use the same code which I used yesterday [see here]

So my best score achieved with manual results was "33", and with my code my best was "39".

Here goes my code:

 
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <X11/extensions/XTest.h>
Display *display;
unsigned int keycode;
int main(int argc, char *argv[]){
display = XOpenDisplay(NULL);
int i=0;
int slp=198000;
keycode = XKeysymToKeycode(display, 0x020);
while(1){
i++;
if(i%13==0)
{
slp=slp+134000;
}
XTestFakeKeyEvent(display, keycode, True, 0);
usleep(slp);
XTestFakeKeyEvent(display, keycode, False, 0);
XFlush(display);
}
}

You can do the same with java code:
import java.awt.*;
import java.awt.event.KeyEvent;
public class Basketball {
    public static void main(String[] s) throws AWTException
    {
        Robot robot = new Robot();
        robot.delay(3000);

        int i=1,r=1;                                                                            
        int j=230;        
        int k=0;
        while(i==1)                                    
        {
            robot.keyPress(KeyEvent.VK_SPACE);
            robot.delay(j);    
            robot.keyRelease(KeyEvent.VK_SPACE);
            robot.delay(15);  
            k++;
            if(k%5==0)
            {   r++;               
                j=r*95;
            }
        }
    }
}

You can slice and dice some constants to get better results.
Just compile and run the code and quickly go to the browser where the game window should be ready for execution, and just win win win.

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