Difference between revisions of "Talon/source"

From Robowiki
Jump to navigation Jump to search
m (removing enemy.clear())
(Thanks Rednaxela for the suggestion)
Line 13: Line 13:
 
  * nearer ones are more dangerous to drive towards
 
  * nearer ones are more dangerous to drive towards
 
  */
 
  */
public class Talon extends AdvancedRobot {
+
public final class Talon extends AdvancedRobot {
private static final HashMap enemy = new HashMap();
+
private static double[][] map = new double[10][2];
private static Object[] map;
+
private static int n = 0;
  
 
public void run() {
 
public void run() {
Line 26: Line 26:
 
 
 
while(true) {
 
while(true) {
map = enemy.values().toArray();
 
 
 
angle = 6.3;
 
angle = 6.3;
 
gn = bd = 2000;
 
gn = bd = 2000;
 
while(angle > 0) {
 
while(angle > 0) {
 
angle -= 0.1;
 
angle -= 0.1;
int t = 0;
+
int t = getOthers();
 
try {
 
try {
 
while(true) {
 
while(true) {
double[] v = (double[])map[t++];
+
double[] v = map[--t];
 
if(v[0] < gn) {
 
if(v[0] < gn) {
 
gn = v[0];
 
gn = v[0];
Line 46: Line 44:
 
bd = md;
 
bd = md;
 
setTurnRightRadians(Utils.normalRelativeAngle(angle - getHeadingRadians()));
 
setTurnRightRadians(Utils.normalRelativeAngle(angle - getHeadingRadians()));
setAhead(100);
+
setAhead(20);
 
}
 
}
 
}
 
}
Line 59: Line 57:
  
 
public void onScannedRobot(ScannedRobotEvent e) {
 
public void onScannedRobot(ScannedRobotEvent e) {
enemy.put(e.getName(), new double[]{e.getDistance(),getHeadingRadians() + e.getBearingRadians()});
+
try {
}
+
map[--n] = new double[]{e.getDistance(),getHeadingRadians() + e.getBearingRadians()};
+
} catch(Exception ex) {
public void onRobotDeath(RobotDeathEvent e) {
+
n = getOthers();
enemy.remove(e.getName());
+
}
 
}
 
}
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 18:00, 30 January 2011

package cs;
import robocode.util.Utils;
import robocode.*;
import java.util.*;
import java.awt.*;

/**
 * Talon - a robot by Chase
 * Nano Melee bot
 *
 * The idea is to avoid driving toward enemy robots,
 * nearer ones are more dangerous to drive towards
 */
public final class Talon extends AdvancedRobot {
	private static double[][] map = new double[10][2];
	private static int n = 0;

	public void run() {
		double angle, gn, bd, md;
		
		setAllColors(Color.red);
		
		setAdjustGunForRobotTurn(true);
		setAdjustRadarForGunTurn(true);
		
		while(true) {
			angle = 6.3;
			gn = bd = 2000;
			while(angle > 0) {
				angle -= 0.1;
				int t = getOthers();
				try {
					while(true) {
						double[] v = map[--t];
						if(v[0] < gn) {
							gn = v[0];
							setTurnGunRightRadians(Utils.normalRelativeAngle(v[1] - getGunHeadingRadians()));
						}
						
						md = Math.abs(Utils.normalRelativeAngle(v[1]-angle)) + (120/v[0]);
						
						if(md < bd) {
							bd = md;
							setTurnRightRadians(Utils.normalRelativeAngle(angle - getHeadingRadians()));
							setAhead(20);
						}
					}
				} catch(Exception e) {}
			}

			setFire(gn);
			
			turnRadarRightRadians(1);
		}
	}

	public void onScannedRobot(ScannedRobotEvent e) {
		try {
			map[--n] = new double[]{e.getDistance(),getHeadingRadians() + e.getBearingRadians()};
		} catch(Exception ex) {
			n = getOthers();
		}
	}
}