Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/robocode/robots/sample/Target.java @ 13019

Last change on this file since 13019 was 13019, checked in by gkronber, 9 years ago

#2069: added necessary robocode files to project to make sure that the robocode problem is self-contained (only a java installation is necessary)

File size: 1.6 KB
Line 
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 */
8package sample;
9
10
11import robocode.AdvancedRobot;
12import robocode.Condition;
13import robocode.CustomEvent;
14
15import java.awt.*;
16
17
18/**
19 * Target - a sample robot by Mathew Nelson.
20 * <p/>
21 * Sits still. Moves every time energy drops by 20.
22 * This Robot demonstrates custom events.
23 *
24 * @author Mathew A. Nelson (original)
25 * @author Flemming N. Larsen (contributor)
26 */
27public class Target extends AdvancedRobot {
28
29  int trigger; // Keeps track of when to move
30
31  /**
32   * TrackFire's run method
33   */
34  public void run() {
35    // Set colors
36    setBodyColor(Color.white);
37    setGunColor(Color.white);
38    setRadarColor(Color.white);
39
40    // Initially, we'll move when life hits 80
41    trigger = 80;
42    // Add a custom event named "trigger hit",
43    addCustomEvent(new Condition("triggerhit") {
44      public boolean test() {
45        return (getEnergy() <= trigger);
46      }
47    });
48  }
49
50  /**
51   * onCustomEvent handler
52   */
53  public void onCustomEvent(CustomEvent e) {
54    // If our custom event "triggerhit" went off,
55    if (e.getCondition().getName().equals("triggerhit")) {
56      // Adjust the trigger value, or
57      // else the event will fire again and again and again...
58      trigger -= 20;
59      out.println("Ouch, down to " + (int) (getEnergy() + .5) + " energy.");
60      // move around a bit.
61      turnLeft(65);
62      ahead(100);
63    }
64  }
65}
Note: See TracBrowser for help on using the repository browser.