Goalie.java

The Goalie always faces forward. It attempts to keep the angle between the the ball--the goalie--the left side of the goal and the ball--the goalie--the right side of the goal the same: always getting between the ball and the goal.

Is somewhat amazing that this short piece of code produces a pretty good goalie!

package Riddler.Flash;

import sockey.SoccerClient;
import java.awt.*;

public class Goalie extends SoccerClient {
    Point ogl= new Point(0,0);
    Point ogr= new Point(0,0);
    Point ball= new Point(0,0);
    
    public void step() {
        ping(ID_OUR_GOAL_LEFT, ogl, true);
        ping(ID_OUR_GOAL_RIGHT, ogr, true);
        ping(ID_BALL, ball, true);
        // if the ball is far away (compared to the goal)
        // then scootch forward
        int backup= ((ogl.x+ogr.x)*2 > ball.x)?-10:10;
        
        // the goalie tries to keep two angles equal:
        //   ball...goalie...left goalpost,
        //   ball...goalie...right goalpost
        // If the left angle is smaller, move left (180 degrees)
        // to make it bigger.
        int bgl= anglesub(ball.y, ogl.y);
        int bgr= anglesub(ogr.y, ball.y);
        if (bgl < bgr) { // move left
            accelerate(180-backup, 100);
        } else {
            accelerate(0+backup, 100);
        }
    }
}