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