-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestRobot2.java
More file actions
113 lines (97 loc) · 2.54 KB
/
Copy pathTestRobot2.java
File metadata and controls
113 lines (97 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package EM;
import robocode.*;
import java.awt.Color;
import java.lang.Math;
// API help : https://robocode.sourceforge.io/docs/robocode/robocode/Robot.html
/**
* TestRobot2 - a robot by (your name here)
*/
public class TestRobot2 extends Robot
{
/**
* run: TestRobot2's default behavior
*/
public boolean strafeStyle = true;
public double whereTheHecc = 0;
public double xPrevE = 0;
public double yPrevE = 0;
public double xCurrE = 200;
public double yCurrE = 200;
public void run() {
// Initialization of the robot should be put here
//boolean targetLock = false;
setAdjustGunForRobotTurn(true);
setAdjustRadarForGunTurn(true);
// After trying out your robot, try uncommenting the import at the top,
// and the next line:
setColors(Color.red,Color.blue,Color.green); // body,gun,radar
// Robot main loop
while(true) {
// Replace the next 4 lines with any behavior you would like
turnRadarRight(360);
//turnRadarLeft(360);
//movement
turnLeft(Math.random()*360);
back(50);
ahead(50);
turnRight(Math.random()*360);
}
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Replace the next line with any behavior you would like
//turnGunLeft(90);
//turnGunRight(e.getHeading());
whereTheHecc = e.getBearing() + this.getHeading() - getGunHeading();
if ( whereTheHecc <= 180){
turnGunRight(whereTheHecc);
} else {
turnGunLeft(whereTheHecc);
}
if (e.getDistance() < 50 && getEnergy() > 50) {
fire(3);
} // otherwise, fire 1.
else {
fire(1);
}
// Call scan again, before we turn the gun
scan();
//if xPrevE == && yPrevE == && &&
//xPrevE = e.getDistance()*Math.cos(Math.toRadians(e.getBearing() + this.getHeading()));
//yPrevE = e.getDistance()*Math.sin(Math.toRadians(e.getBearing() + this.getHeading()));
}
/**
* onHitByBullet: What to do when you're hit by a bullet
*/
public void onHitByBullet(HitByBulletEvent e) {
// Replace the next line with any behavior you would like
back(10);
switchStrafe(2);
}
/**
* onHitWall: What to do when you hit a wall
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
back(20);
}
public void switchStrafe(int e) {
switch (e){
case 0:
strafeStyle = true;
break;
case 1:
strafeStyle = false;
break;
case 2:
if(strafeStyle){
strafeStyle = false;
}else{
strafeStyle = true;
}
break;
}
}
}