1 | /** |
---|
2 | * Copyright (c) 2001-2014 Mathew A. Nelson and Robocode contributors |
---|
3 | * All rights reserved. This program and the accompanying materials |
---|
4 | * are made available under the terms of the Eclipse Public License v1.0 |
---|
5 | * which accompanies this distribution, and is available at |
---|
6 | * http://robocode.sourceforge.net/license/epl-v10.html |
---|
7 | */ |
---|
8 | package sample; |
---|
9 | |
---|
10 | |
---|
11 | import robocode.HitRobotEvent; |
---|
12 | import robocode.Robot; |
---|
13 | import robocode.ScannedRobotEvent; |
---|
14 | |
---|
15 | import java.awt.*; |
---|
16 | |
---|
17 | |
---|
18 | /** |
---|
19 | * Walls - a sample robot by Mathew Nelson, and maintained by Flemming N. Larsen |
---|
20 | * <p/> |
---|
21 | * Moves around the outer edge with the gun facing in. |
---|
22 | * |
---|
23 | * @author Mathew A. Nelson (original) |
---|
24 | * @author Flemming N. Larsen (contributor) |
---|
25 | */ |
---|
26 | public class Walls extends Robot { |
---|
27 | |
---|
28 | boolean peek; // Don't turn if there's a robot there |
---|
29 | double moveAmount; // How much to move |
---|
30 | |
---|
31 | /** |
---|
32 | * run: Move around the walls |
---|
33 | */ |
---|
34 | public void run() { |
---|
35 | // Set colors |
---|
36 | setBodyColor(Color.black); |
---|
37 | setGunColor(Color.black); |
---|
38 | setRadarColor(Color.orange); |
---|
39 | setBulletColor(Color.cyan); |
---|
40 | setScanColor(Color.cyan); |
---|
41 | |
---|
42 | // Initialize moveAmount to the maximum possible for this battlefield. |
---|
43 | moveAmount = Math.max(getBattleFieldWidth(), getBattleFieldHeight()); |
---|
44 | // Initialize peek to false |
---|
45 | peek = false; |
---|
46 | |
---|
47 | // turnLeft to face a wall. |
---|
48 | // getHeading() % 90 means the remainder of |
---|
49 | // getHeading() divided by 90. |
---|
50 | turnLeft(getHeading() % 90); |
---|
51 | ahead(moveAmount); |
---|
52 | // Turn the gun to turn right 90 degrees. |
---|
53 | peek = true; |
---|
54 | turnGunRight(90); |
---|
55 | turnRight(90); |
---|
56 | |
---|
57 | while (true) { |
---|
58 | // Look before we turn when ahead() completes. |
---|
59 | peek = true; |
---|
60 | // Move up the wall |
---|
61 | ahead(moveAmount); |
---|
62 | // Don't look now |
---|
63 | peek = false; |
---|
64 | // Turn to the next wall |
---|
65 | turnRight(90); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * onHitRobot: Move away a bit. |
---|
71 | */ |
---|
72 | public void onHitRobot(HitRobotEvent e) { |
---|
73 | // If he's in front of us, set back up a bit. |
---|
74 | if (e.getBearing() > -90 && e.getBearing() < 90) { |
---|
75 | back(100); |
---|
76 | } // else he's in back of us, so set ahead a bit. |
---|
77 | else { |
---|
78 | ahead(100); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * onScannedRobot: Fire! |
---|
84 | */ |
---|
85 | public void onScannedRobot(ScannedRobotEvent e) { |
---|
86 | fire(2); |
---|
87 | // Note that scan is called automatically when the robot is moving. |
---|
88 | // By calling it manually here, we make sure we generate another scan event if there's a robot on the next |
---|
89 | // wall, so that we do not start moving up it until it's gone. |
---|
90 | if (peek) { |
---|
91 | scan(); |
---|
92 | } |
---|
93 | } |
---|
94 | } |
---|