To learn java I have written a small Tron game.
It includes a nice&simple AI.
Use the direction keys to drive into a different direction. Press return to stop your bike:

Feel free to post questions related the simple AI or the game in here

The complete AI:
Code:
boolean[] aiAlive = new boolean[] {true, true, true};
int[] aiColor = new int[] {Color.blue.getRGB(), Color.red.getRGB(), Color.green.getRGB()};
int[] aiFear = new int[] {500, 200, 20};
int[] aiThinkTime = new int[] {2, 2, 2};
int[] aiThoughtTime = new int[] {0, 0, 0};
int[] aiRandomDirectionChange = new int[] {3000, 2000, 1000};
boolean[] aiCheckSight = new boolean[] {true, true, true};
Point[] aiDirection = new Point[] { new Point(0,1), new Point(-1,0), new Point(0,-1)};
Point[] aiPosition = new Point[] { new Point(100, 0), new Point(100, 100), new Point(0, 100)};
private void addAiStep() {
for (int i=0; i<aiColor.length; i++) {
if (aiAlive[i]) {
aiThoughtTime[i] +=1;
if (aiThoughtTime[i] >= Math.random() * aiThinkTime[i]){
aiThoughtTime[i] = 0;
for (int j=1; j<Math.random()*aiFear[i]+1; j++) {
if (checkCollision(aiPosition[i].x + (aiDirection[i].x * j), aiPosition[i].y + (aiDirection[i].y * j))) {
aiDirection[i].x = directionChange(aiDirection[i].x);
aiDirection[i].y = directionChange(aiDirection[i].y);
if (aiCheckSight[i]) {
for (int k=1; j<aiFear[i]+1; k++) {
if (checkCollision(aiPosition[i].x + (aiDirection[i].x * k), aiPosition[i].y + (aiDirection[i].y * k))) {
aiDirection[i].x *= -1;
aiDirection[i].y *= -1;
break;
}
if (checkCollision(aiPosition[i].x + (aiDirection[i].x * k * -1), aiPosition[i].y + (aiDirection[i].y * k * -1))) {
break;
}
if (k==aiFear[i]) {
if (Math.round(Math.random()) == 1) {
aiDirection[i].x *= -1;
aiDirection[i].y *= -1;
}
}
}
}
if (checkCollision(aiPosition[i].x + aiDirection[i].x, aiPosition[i].y + aiDirection[i].y)) {
aiDirection[i].x *= -1;
aiDirection[i].y *= -1;
} break;
} else {
if (aiRandomDirectionChange[i] == (int)(Math.random() * aiRandomDirectionChange[i] + 1)) {
aiDirection[i].x = directionChange(aiDirection[i].x);
aiDirection[i].y = directionChange(aiDirection[i].y);
if (checkCollision(aiPosition[i].x + aiDirection[i].x, aiPosition[i].y + aiDirection[i].y)) {
aiDirection[i].x *= -1;
aiDirection[i].y *= -1;
}
}
}
}
}
aiPosition[i].x += aiDirection[i].x;
aiPosition[i].y += aiDirection[i].y;
if (checkCollision(aiPosition[i].x, aiPosition[i].y)) {
aiAlive[i] = false;
} else {
Graphics g = tempImage.getGraphics();
g.setColor(new Color(aiColor[i]));
g.drawLine(aiPosition[i].x-aiDirection[i].x, aiPosition[i].y-aiDirection[i].y, aiPosition[i].x, aiPosition[i].y);
g.dispose();
redraw();
}
}
}
}
The complete source code (including a .jar executable):
http://ul.to/xwtkpg/SciLorsSketchTron.zip