Skip to content

Commit 2a63bd6

Browse files
author
Terry Jay Steel
committed
added FirstRobot and randomDriver robots
1 parent 24241d1 commit 2a63bd6

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

Robocode/src/robots/FirstRobot.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package robots;
2+
3+
import java.awt.Color;
4+
5+
import robocode.Robot;
6+
import robocode.ScannedRobotEvent;
7+
8+
public class FirstRobot extends Robot {
9+
public void run() {
10+
11+
// setting colours for my robot
12+
setBodyColor(new Color(0,255,255));
13+
setGunColor(new Color(0,200,200));
14+
setRadarColor(new Color(0,100,100));
15+
16+
while (true) {
17+
ahead(100);
18+
turnGunRight(360);
19+
back(100);
20+
turnGunRight(360);
21+
}
22+
}
23+
24+
public void onScannedRobot(ScannedRobotEvent e) {
25+
fire(1);
26+
}
27+
}

Robocode/src/robots/RandomDriver.java

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package robots;
2+
3+
import java.awt.Color;
4+
5+
import robocode.AdvancedRobot;
6+
import robocode.HitWallEvent;
7+
import robocode.ScannedRobotEvent;
8+
import robocode.util.Utils;
9+
10+
public class RandomDriver extends AdvancedRobot {
11+
12+
int travelDirection = 1;
13+
int turnDirection = 1;
14+
15+
public void run() {
16+
17+
// setting colours for my robot
18+
setBodyColor(new Color(255,0,0));
19+
setGunColor(new Color(200,0,0));
20+
setRadarColor(new Color(100,0,0));
21+
setScanColor(Color.RED);
22+
setBulletColor(Color.RED);
23+
24+
setAdjustGunForRobotTurn(true);
25+
setAdjustRadarForGunTurn(true);
26+
setAdjustRadarForRobotTurn(true);
27+
28+
while (true) {
29+
// randomly change speed and direction
30+
if (Math.random() > 0.9) {
31+
turnDirection *= -1;
32+
setMaxVelocity((12*Math.random())+12);
33+
}
34+
setTurnRadarRight(Double.POSITIVE_INFINITY); // scan permanently
35+
setAhead(100*travelDirection);
36+
setTurnLeft(180*turnDirection);
37+
execute();
38+
}
39+
}
40+
41+
public void onHitWall(HitWallEvent e){
42+
travelDirection=-travelDirection;//reverse direction upon hitting a wall
43+
}
44+
45+
public void onScannedRobot(ScannedRobotEvent e) {
46+
47+
// if only 1 enemy, reverse radar to lock on
48+
if(getOthers() == 1) {
49+
setTurnRadarLeft(getRadarTurnRemaining());//lock on the radar
50+
}
51+
52+
double enemyBearing = e.getBearing()+getHeading(); // bearing is relative to your heading, so we'll add this.
53+
double gunTurn = enemyBearing-getGunHeading(); // set our gun to aim at the enemy, but subtract the angle already applied to the gun.
54+
gunTurn = Utils.normalRelativeAngleDegrees(gunTurn);
55+
setTurnGunRight(gunTurn);
56+
if (Math.abs(gunTurn) < 5) {
57+
setFire(1);
58+
}
59+
60+
}
61+
}

0 commit comments

Comments
 (0)