-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransCoders.java
175 lines (147 loc) · 3.6 KB
/
TransCoders.java
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
*
* state
* 0 - initial - Wall
* 1 - low energy
* */
import robocode.BulletHitEvent;
import robocode.CharlieBot;
import robocode.HitRobotEvent;
import robocode.ScannedRobotEvent;
import java.awt.*;
public class TransCoders extends CharlieBot {
boolean peek; // Don't turn if there's a robot there
double moveAmount; // How much to move
int state = 1;
/*============= check state =============== */
public void checkState() {
out.println(state);
if(getEnergy() < 80 && getOthers() > 5) {
state = 1;
turnLeft(getHeading() % 90);
ahead(moveAmount);
}
else if(getEnergy() < 80 && getOthers() < 5) {
state = 0;
}
else {
state = 0;
}
}
/**
* run: Move around the walls
*/
public void run() {
// Set colors
setBodyColor(Color.BLACK);
setGunColor(Color.BLACK);
setRadarColor(Color.red);
setBulletColor(Color.red);
setScanColor(Color.red);
// Initialize moveAmount to the maximum possible for this battlefield.
moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight());
// Initialize peek to false
peek = false;
// turnLeft to face a wall.
// getHeading() % 90 means the remainder of
// getHeading() divided by 90.
turnLeft(getHeading() % 90);
ahead(moveAmount);
// Turn the gun to turn right 90 degrees.
peek = true;
turnRight(90);
turnRight(90);
while (true) {
checkState();
if(state == 0) {
/* ============== WALL =========== */
// Look before we turn when ahead() completes.
peek = true;
// Move up the wall
ahead(moveAmount);
// Don't look now
peek = false;
// Turn to the next wall
turnRight(90);
/* ============== WALL =========== */
}
else if(state == 1) {
/* ============== BATTLE =========== */
ahead(100);
turnRight(360);
back(100);
/* ============== BATTLE =========== */
}
}
}
/**
* onHitRobot: Move away a bit.
*/
public void onHitRobot(HitRobotEvent e) {
if(state == 0) {
/* ============== WALL =========== */
// If he's in front of us, set back up a bit.
if (e.getBearing() > -90 && e.getBearing() < 90) {
back(100);
} // else he's in back of us, so set ahead a bit.
else {
ahead(100);
}
/* ============== WALL =========== */
}
else if(state == 1) {
if (e.getBearing() > -10 && e.getBearing() < 10) {
fire(2);
}
if (e.isMyFault()) {
turnRight(10);
}
}
}
@Override
public void onRobotDetected(ScannedRobotEvent event) {
if(state == 0) {
int power = (int) Math.abs(20 - event.getDistance() % 10) + 1;
fire(power);
//smartFire(e.getDistance());
// Note that scan is called automatically when the robot is moving.
// By calling it manually here, we make sure we generate another scan event if there's a robot on the next
// wall, so that we do not start moving up it until it's gone.
if (peek) {
scan();
}
}
else if(state == 1) {
if (event.getBearing() > -10 && event.getBearing() < 10) {
fire(3);
}
}
}
/**
* onScannedRobot: Fire!
*/
@Override
public void onBulletHit(BulletHitEvent event) {
//back(100);
if(state == 0) {
if(getEnergy() < event.getEnergy() + 20) {
turnLeft(getHeading() % 180);
ahead(moveAmount);
}
}
else if(state == 1) {
if(getEnergy() < 40) {
state = 0;
}
else {
back(50);
turnRight(90);
ahead(moveAmount);
}
}
/*if(getOthers() < 7) {
turnRight(90);
ahead(moveAmount);
}*/
}
}