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.DeathEvent;
12 import robocode.Robot;
13 import robocode.ScannedRobotEvent;
14 import static robocode.util.Utils.normalRelativeAngleDegrees;
15 
16 import java.awt.*;
17 
18 
19 /**
20  * Corners - a sample robot by Mathew Nelson.
21  * <p/>
22  * This robot moves to a corner, then swings the gun back and forth.
23  * If it dies, it tries a new corner in the next round.
24  *
25  * @author Mathew A. Nelson (original)
26  * @author Flemming N. Larsen (contributor)
27  */
28 public class Corners extends Robot {
29 	int others; // Number of other robots in the game
30 	static int corner = 0; // Which corner we are currently using
31 	// static so that it keeps it between rounds.
32 	boolean stopWhenSeeRobot = false; // See goCorner()
33 
34 	/**
35 	 * run:  Corners' main run function.
36 	 */
run()37 	public void run() {
38 		// Set colors
39 		setBodyColor(Color.red);
40 		setGunColor(Color.black);
41 		setRadarColor(Color.yellow);
42 		setBulletColor(Color.green);
43 		setScanColor(Color.green);
44 
45 		// Save # of other bots
46 		others = getOthers();
47 
48 		// Move to a corner
49 		goCorner();
50 
51 		// Initialize gun turn speed to 3
52 		int gunIncrement = 3;
53 
54 		// Spin gun back and forth
55 		while (true) {
56 			for (int i = 0; i < 30; i++) {
57 				turnGunLeft(gunIncrement);
58 			}
59 			gunIncrement *= -1;
60 		}
61 	}
62 
63 	/**
64 	 * goCorner:  A very inefficient way to get to a corner.  Can you do better?
65 	 */
goCorner()66 	public void goCorner() {
67 		// We don't want to stop when we're just turning...
68 		stopWhenSeeRobot = false;
69 		// turn to face the wall to the "right" of our desired corner.
70 		turnRight(normalRelativeAngleDegrees(corner - getHeading()));
71 		// Ok, now we don't want to crash into any robot in our way...
72 		stopWhenSeeRobot = true;
73 		// Move to that wall
74 		ahead(5000);
75 		// Turn to face the corner
76 		turnLeft(90);
77 		// Move to the corner
78 		ahead(5000);
79 		// Turn gun to starting point
80 		turnGunLeft(90);
81 	}
82 
83 	/**
84 	 * onScannedRobot:  Stop and fire!
85 	 */
onScannedRobot(ScannedRobotEvent e)86 	public void onScannedRobot(ScannedRobotEvent e) {
87 		// Should we stop, or just fire?
88 		if (stopWhenSeeRobot) {
89 			// Stop everything!  You can safely call stop multiple times.
90 			stop();
91 			// Call our custom firing method
92 			smartFire(e.getDistance());
93 			// Look for another robot.
94 			// NOTE:  If you call scan() inside onScannedRobot, and it sees a robot,
95 			// the game will interrupt the event handler and start it over
96 			scan();
97 			// We won't get here if we saw another robot.
98 			// Okay, we didn't see another robot... start moving or turning again.
99 			resume();
100 		} else {
101 			smartFire(e.getDistance());
102 		}
103 	}
104 
105 	/**
106 	 * smartFire:  Custom fire method that determines firepower based on distance.
107 	 *
108 	 * @param robotDistance the distance to the robot to fire at
109 	 */
smartFire(double robotDistance)110 	public void smartFire(double robotDistance) {
111 		if (robotDistance > 200 || getEnergy() < 15) {
112 			fire(1);
113 		} else if (robotDistance > 50) {
114 			fire(2);
115 		} else {
116 			fire(3);
117 		}
118 	}
119 
120 	/**
121 	 * onDeath:  We died.  Decide whether to try a different corner next game.
122 	 */
onDeath(DeathEvent e)123 	public void onDeath(DeathEvent e) {
124 		// Well, others should never be 0, but better safe than sorry.
125 		if (others == 0) {
126 			return;
127 		}
128 
129 		// If 75% of the robots are still alive when we die, we'll switch corners.
130 		if ((others - getOthers()) / (double) others < .75) {
131 			corner += 90;
132 			if (corner == 270) {
133 				corner = -90;
134 			}
135 			out.println("I died and did poorly... switching corner to " + corner);
136 		} else {
137 			out.println("I died but did well.  I will still use corner " + corner);
138 		}
139 	}
140 }
141