Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode.TrunkInt/HeuristicLab.Problems.Robocode/3.3/External Evaluator/BattleRunner.java @ 9790

Last change on this file since 9790 was 9790, checked in by ascheibe, 11 years ago

#2069

  • added license headers
  • corrected version information
  • fixed formatting
File size: 5.2 KB
Line 
1/* HeuristicLab
2 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
3 *
4 * This file is part of HeuristicLab.
5 *
6 * HeuristicLab is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * HeuristicLab is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20import robocode.control.*;
21import robocode.control.events.*;
22
23//
24// Please add this file to the /libs forlder in the robocode installation directory, and compile it with the following command:
25// javac -cp .;robocode.jar BattleRunner.java
26//
27public class BattleRunner {
28
29    static String[] defaultRobots = new String[] {
30        "Evaluation.BestSolution*",
31        "sample.Corners",
32        "sample.Crazy",
33        "sample.Fire",
34        "sample.TrackFire",
35        "sample.Walls",
36        "sample.SpinBot",
37        "sample.SittingDuck"
38    };
39    public static String player = "Evaluation.output*";
40    //static int
41    public static int score = 0;
42
43    public static void main(String[] args) {
44
45        // Disable log messages from Robocode
46        RobocodeEngine.setLogMessagesEnabled(false);
47
48        // Create the RobocodeEngine
49        //   RobocodeEngine engine = new RobocodeEngine(); // Run from current working directory
50        RobocodeEngine engine = new RobocodeEngine(new java.io.File("F:/robocode")); // Run from C:/Robocode
51
52        // Add our own battle listener to the RobocodeEngine
53        engine.addBattleListener(new BattleObserver());
54
55        score = 0;
56        String bots = "";
57        String[] robots = new String[defaultRobots.length];
58        robots = defaultRobots.clone();
59
60        if (args.length == 1)
61        {
62            robots[0] = player = args[0];
63            engine.setVisible(true);
64        }
65        else if (args.length == 2)
66        {
67            robots[0] = player = args[0];
68            engine.setVisible(false);
69        }
70        else if (args.length > 2)
71        {
72            robots[0] = player = args[0];
73            for(int i = 1; i < args.length && i < robots.length; i++)
74                robots[i] = args[i];
75        }
76        else
77            engine.setVisible(false);
78
79        for (String s : robots)
80            bots += s + ", ";
81        bots = bots.substring(0, bots.length() - 1);
82
83        // Setup the battle specification
84
85        int numberOfRounds = 5;
86        BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600); // 800x600
87        RobotSpecification[] selectedRobots = engine.getLocalRepository(bots);
88
89        System.out.println("Bots: " + bots);
90        for(RobotSpecification s : selectedRobots)
91            System.out.println("Robot name: " + s.getName());
92
93        for (int i = 1; i < selectedRobots.length; i++) {
94            BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield, new RobotSpecification[] { selectedRobots[0], selectedRobots[i] });
95
96            // Run our specified battle and let it run till it is over
97            engine.runBattle(battleSpec, true); // waits till the battle finishes
98        }
99        // Cleanup our RobocodeEngine
100        engine.close();
101
102        //System.out.println(player);
103        //System.out.println(robots[0]);
104        //System.out.println(args[0]);
105        System.out.println(score / (robots.length - 1));
106
107        //for(String s : robots)
108        //    System.out.println("Robot name: " + s);
109
110        // Make sure that the Java VM is shut down properly
111        System.exit(0);
112    }
113}
114
115//
116// Our private battle listener for handling the battle event we are interested in.
117//
118class BattleObserver extends BattleAdaptor {
119
120    // Called when the battle is completed successfully with battle results
121    public void onBattleCompleted(BattleCompletedEvent e) {
122        //System.out.println("-- Battle has completed --");
123
124        // Print out the sorted results with the robot names
125        //System.out.println("Battle results:");
126        for (robocode.BattleResults result : e.getSortedResults()) {
127            //System.out.println("  " + result.getTeamLeaderName() + ": " + result.getScore());
128            if (result.getTeamLeaderName().equals(BattleRunner.player))
129                BattleRunner.score += result.getScore();
130            else
131                BattleRunner.score -= result.getScore();
132        }
133    }
134
135    // Called when the game sends out an information message during the battle
136    public void onBattleMessage(BattleMessageEvent e) {
137        //System.out.println("Msg> " + e.getMessage());
138    }
139
140    // Called when the game sends out an error message during the battle
141    public void onBattleError(BattleErrorEvent e) {
142        //System.out.println("Err> " + e.getError());
143    }
144}
Note: See TracBrowser for help on using the repository browser.