Tanks!
Implement a Tank for a Tank Battle. The Tank should subclass tanks.Tank.
You only need to implement one method, action. Here is a sample:
SimpleTank.java |
1 | import tanks.*; |
2 | import java.util.*; |
3 | |
4 | public class SimpleTank extends Tank { |
5 | public void action() { |
6 | Direction dir = randomDir(); // choose a random direction |
7 | ScanResult[] results = scanArray(9); // long range scan |
8 | for(int i=0;i<results.length;i++) { |
9 | Pos pos = getPos(); |
10 | ScanResult sr = results[i]; |
11 | if(sr.pos.x == pos.x && sr.pos.y == pos.y) |
12 | throw new Error("same pos!"); |
13 | dir = pos.toward(sr.pos); // direction to target |
14 | aimTurret(dir); |
15 | fire(); |
16 | break; |
17 | } |
18 | Pos p = getPos(); |
19 | Pos np = new Pos(p.x+dir.dx,p.y+dir.dy); |
20 | boolean collision = false; |
21 | for(int i=0;i<results.length;i++) { |
22 | if(equals(np,results[i].pos)) |
23 | collision = true; |
24 | } |
25 | if(!collision) { // avoid collision! |
26 | setDrivingDirection(dir); |
27 | drive(); |
28 | } |
29 | } |
30 | } |
You can download the program here.
-
You can extract the contents of the file using the command:
jar xf tanks-src.jar
- Compile your code like this:
javac SimpleTank.java tanks/*.java
- Running the code:
|