Bobbie Smulders Freelance Software Developer

Automated game of Doeo using Java Robot class

Now that pong is fully automated, it is time to optimize.

First I switched to the game Doeo. The goal of the game is to move your mouse as fast over Doeos as possible. A fun game to use as an example for automation.

I also made a small performance enhancement (10 to 15 percent) by not using getRGB () from BufferdImage, but reading directly from an array. It does take some time to get everything into an integer array, but if that’s done, the for-loop is a lot faster.

The optimized code:

import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;

/*

©2010 Bobbie Smulders

RobotDoeo plays an automated game of Doeo.
Original game can be found here:
http://armorgames.com/play/1414/doeo

*/

public class RobotDoeo extends Thread{

	public Rectangle rect;

	public RobotDoeo(int x, int y, int width, int height) {
		rect = new Rectangle(x, y, width, height);
	}

	public void run() {
		try {
			Robot robot = new Robot();

			while(true){
				BufferedImage img = robot.createScreenCapture(rect);
				WritableRaster r=img.getRaster();
				DataBuffer db=r.getDataBuffer();
				DataBufferInt dbi=(DataBufferInt)db;
				int[] data=dbi.getData();            		

				for (int x_scale = 0; x_scale < rect.width; x_scale += 10) {
					for(int y_scale = 0; y_scale < rect.height; y_scale += 10) {
						int rgb = data[x_scale + rect.width * y_scale];
						if (rgb == -5381164 ||
							rgb == -13108 ||
							rgb == -2511443 ||
							rgb == -7093836 ||
							rgb == -1722441){
							robot.mouseMove(rect.x + x_scale, rect.y + y_scale);
						}
					}
				}
			}
		}
		catch(AWTException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new RobotDoeo(10, 100, 550, 430).start();
	}
}