2
\$\begingroup\$

Anyone can play Google Hurdles today. Here is my score: 1.1 second. Is there are way of improving this score and running faster than a second?

http://www.google.com/doodles/hurdles-2012

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;


public class RunOlim {

private volatile static boolean run=true;

public static void main(String[] args) {
    Robot robot = null;


    Thread th = new Thread(new Runnable(){

        @Override
        public void run() {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                run=false;
            }

        }

    });
    th.start();

    try {
        robot = new Robot();
    } catch (AWTException e) {

        e.printStackTrace();
    }
    while(run){
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.keyRelease(KeyEvent.VK_RIGHT);

        robot.keyPress(KeyEvent.VK_LEFT);
        robot.keyRelease(KeyEvent.VK_LEFT);
    }
}

}
\$\endgroup\$
  • 1
    \$\begingroup\$ OK, this was originally on StackOverflow, and I asked the same thing there: what does the sentence "Everyone could be played google hurdles today" mean? \$\endgroup\$ – David Robinson Aug 7 '12 at 16:55
  • \$\begingroup\$ It emphasizes the target in order to make the question clear. I think giving real world example is best way of asking something? Sentences has to be like: "Everyone must have played google hurdles today" \$\endgroup\$ – Ahmet Karakaya Aug 7 '12 at 16:58
  • \$\begingroup\$ I mean that the sentence itself is not even close to an English sentence. Do you mean "Anyone can play Google Hurdles today"? \$\endgroup\$ – David Robinson Aug 7 '12 at 17:00
  • \$\begingroup\$ I'm confused. How does your code example correlate to Google Doodle for hurdles? And what are you looking to do? \$\endgroup\$ – Ryan Miller Aug 7 '12 at 20:24
1
\$\begingroup\$

First off, this may not really be an answer to your question. You want to play it fast, but I can tell you how you get a great score. Good enough for showing off, if that's what you're after!

You can vastly improve your score, even on a very slow network. And you could even gain even more (secret) medals.

Just play Hurdles once so it shows your score, then - e.g. using jQuery - have fun with the DOM:

$('#hplogo_sbt').html('0.1');
$('#hplogo_sb').find('.hplogo_smh').removeClass('hplogo_smh').addClass('hplogo_smg');

That's about it, 0.1 seconds and 3 gold medals (you could get up to 9 without breaking the layout if you have a 0.1 second time, just insert more "medal" dom nodes).

enter image description here

\$\endgroup\$
2
\$\begingroup\$

There are some general notes about the code, not really performance improvements (however, I have not tested their performance). I guess the real bottleneck could be in the browser/OS/hardware which can't handle more key events.

Anyway, these do not need any other thread:

  1. final long endTime = System.nanoTime() 
        + TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);
    while (endTime > System.nanoTime()) {
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.keyRelease(KeyEvent.VK_RIGHT);
    
        robot.keyPress(KeyEvent.VK_LEFT);
        robot.keyRelease(KeyEvent.VK_LEFT);
    }
    
  2. final long endTime = System.nanoTime() 
        + TimeUnit.NANOSECONDS.convert(10, TimeUnit.SECONDS);
    while (endTime > System.nanoTime()) {
        for (int i = 0; i < 100; i++) {
            robot.keyPress(KeyEvent.VK_RIGHT);
            robot.keyRelease(KeyEvent.VK_RIGHT);
    
            robot.keyPress(KeyEvent.VK_LEFT);
            robot.keyRelease(KeyEvent.VK_LEFT);
        }
    }
    

Maybe these were your first tries too.

Here is a 3rd one with a ScheduledExecutorService:

public static void main(final String[] args) throws AWTException {

    final ScheduledExecutorService scheduledExecutorService = 
        Executors.newScheduledThreadPool(1);
    final Runnable stopCommand = new Runnable() {
        @Override
        public void run() {
            run = false;
        }
    };
    scheduledExecutorService.schedule(stopCommand, 10, TimeUnit.SECONDS);

    final Robot robot = new Robot();

    while (run) {
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.keyRelease(KeyEvent.VK_RIGHT);

        robot.keyPress(KeyEvent.VK_LEFT);
        robot.keyRelease(KeyEvent.VK_LEFT);
    }
    scheduledExecutorService.shutdown();
}

A final note from Clean Code, page 25:

Classes and objects should have noun or noun phrase names like Customer, WikiPage, Account, and AddressParser. [...] A class name should not be a verb.

I'd name it HurdleRunner, for example.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.