-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCorners.java
140 lines (125 loc) · 3.68 KB
/
Corners.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
/**
* Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://robocode.sourceforge.net/license/epl-v10.html
*/
package sample;
import robocode.DeathEvent;
import robocode.Robot;
import robocode.ScannedRobotEvent;
import static robocode.util.Utils.normalRelativeAngleDegrees;
import java.awt.*;
/**
* Corners - a sample robot by Mathew Nelson.
* <p/>
* This robot moves to a corner, then swings the gun back and forth.
* If it dies, it tries a new corner in the next round.
*
* @author Mathew A. Nelson (original)
* @author Flemming N. Larsen (contributor)
*/
public class Corners extends Robot {
int others; // Number of other robots in the game
static int corner = 0; // Which corner we are currently using
// static so that it keeps it between rounds.
boolean stopWhenSeeRobot = false; // See goCorner()
/**
* run: Corners' main run function.
*/
public void run() {
// Set colors
setBodyColor(Color.red);
setGunColor(Color.black);
setRadarColor(Color.yellow);
setBulletColor(Color.green);
setScanColor(Color.green);
// Save # of other bots
others = getOthers();
// Move to a corner
goCorner();
// Initialize gun turn speed to 3
int gunIncrement = 3;
// Spin gun back and forth
while (true) {
for (int i = 0; i < 30; i++) {
turnGunLeft(gunIncrement);
}
gunIncrement *= -1;
}
}
/**
* goCorner: A very inefficient way to get to a corner. Can you do better?
*/
public void goCorner() {
// We don't want to stop when we're just turning...
stopWhenSeeRobot = false;
// turn to face the wall to the "right" of our desired corner.
turnRight(normalRelativeAngleDegrees(corner - getHeading()));
// Ok, now we don't want to crash into any robot in our way...
stopWhenSeeRobot = true;
// Move to that wall
ahead(5000);
// Turn to face the corner
turnLeft(90);
// Move to the corner
ahead(5000);
// Turn gun to starting point
turnGunLeft(90);
}
/**
* onScannedRobot: Stop and fire!
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Should we stop, or just fire?
if (stopWhenSeeRobot) {
// Stop everything! You can safely call stop multiple times.
stop();
// Call our custom firing method
smartFire(e.getDistance());
// Look for another robot.
// NOTE: If you call scan() inside onScannedRobot, and it sees a robot,
// the game will interrupt the event handler and start it over
scan();
// We won't get here if we saw another robot.
// Okay, we didn't see another robot... start moving or turning again.
resume();
} else {
smartFire(e.getDistance());
}
}
/**
* smartFire: Custom fire method that determines firepower based on distance.
*
* @param robotDistance the distance to the robot to fire at
*/
public void smartFire(double robotDistance) {
if (robotDistance > 200 || getEnergy() < 15) {
fire(1);
} else if (robotDistance > 50) {
fire(2);
} else {
fire(3);
}
}
/**
* onDeath: We died. Decide whether to try a different corner next game.
*/
public void onDeath(DeathEvent e) {
// Well, others should never be 0, but better safe than sorry.
if (others == 0) {
return;
}
// If 75% of the robots are still alive when we die, we'll switch corners.
if ((others - getOthers()) / (double) others < .75) {
corner += 90;
if (corner == 270) {
corner = -90;
}
out.println("I died and did poorly... switching corner to " + corner);
} else {
out.println("I died but did well. I will still use corner " + corner);
}
}
}