1 /** 2 * Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Eclipse Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://robocode.sourceforge.net/license/epl-v10.html 7 */ 8 package sample; 9 10 11 import robocode.HitRobotEvent; 12 import robocode.Robot; 13 import robocode.ScannedRobotEvent; 14 15 import java.awt.*; 16 17 18 /** 19 * RamFire - a sample robot by Mathew Nelson. 20 * <p/> 21 * Drives at robots trying to ram them. 22 * Fires when it hits them. 23 * 24 * @author Mathew A. Nelson (original) 25 * @author Flemming N. Larsen (contributor) 26 */ 27 public class RamFire extends Robot { 28 int turnDirection = 1; // Clockwise or counterclockwise 29 30 /** 31 * run: Spin around looking for a target 32 */ run()33 public void run() { 34 // Set colors 35 setBodyColor(Color.lightGray); 36 setGunColor(Color.gray); 37 setRadarColor(Color.darkGray); 38 39 while (true) { 40 turnRight(5 * turnDirection); 41 } 42 } 43 44 /** 45 * onScannedRobot: We have a target. Go get it. 46 */ onScannedRobot(ScannedRobotEvent e)47 public void onScannedRobot(ScannedRobotEvent e) { 48 49 if (e.getBearing() >= 0) { 50 turnDirection = 1; 51 } else { 52 turnDirection = -1; 53 } 54 55 turnRight(e.getBearing()); 56 ahead(e.getDistance() + 5); 57 scan(); // Might want to move ahead again! 58 } 59 60 /** 61 * onHitRobot: Turn to face robot, fire hard, and ram him again! 62 */ onHitRobot(HitRobotEvent e)63 public void onHitRobot(HitRobotEvent e) { 64 if (e.getBearing() >= 0) { 65 turnDirection = 1; 66 } else { 67 turnDirection = -1; 68 } 69 turnRight(e.getBearing()); 70 71 // Determine a shot that won't kill the robot... 72 // We want to ram him instead for bonus points 73 if (e.getEnergy() > 16) { 74 fire(3); 75 } else if (e.getEnergy() > 10) { 76 fire(2); 77 } else if (e.getEnergy() > 4) { 78 fire(1); 79 } else if (e.getEnergy() > 2) { 80 fire(.5); 81 } else if (e.getEnergy() > .4) { 82 fire(.1); 83 } 84 ahead(40); // Ram him again! 85 } 86 } 87