Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Robocode/HeuristicLab.Problems.Robocode/External Evaluator/BattleRunner.java @ 9584

Last change on this file since 9584 was 9567, checked in by melkaref, 12 years ago

Removed need to load xml file in Evaluator.cs;
Added BattleRunner.java

File size: 4.4 KB
RevLine 
[9567]1import robocode.control.*;
2import robocode.control.events.*;
3
4//
5// Please add this file to the /libs forlder in the robocode installation directory, and compile it with the following command:
6// javac -cp .;robocode.jar BattleRunner.java
7//
8public class BattleRunner {
9
10    static String[] defaultRobots = new String[] {
11        "Evaluation.BestSolution*",
12        "sample.Corners",
13        "sample.Crazy",
14        "sample.Fire",
15        "sample.TrackFire",
16        "sample.Walls",
17        "sample.SpinBot",
18        "sample.SittingDuck"
19    };
20    public static String player = "Evaluation.output*";
21    //static int
22    public static int score = 0;
23
24    public static void main(String[] args) {
25
26        // Disable log messages from Robocode
27        RobocodeEngine.setLogMessagesEnabled(false);
28
29        // Create the RobocodeEngine
30        //   RobocodeEngine engine = new RobocodeEngine(); // Run from current working directory
31        RobocodeEngine engine = new RobocodeEngine(new java.io.File("F:/robocode")); // Run from C:/Robocode
32
33        // Add our own battle listener to the RobocodeEngine
34        engine.addBattleListener(new BattleObserver());
35
36        score = 0;
37        String bots = "";
38        String[] robots = new String[defaultRobots.length];
39        robots = defaultRobots.clone();
40
41        if (args.length == 1)
42        {
43            robots[0] = player = args[0];
44            engine.setVisible(true);
45        }
46        else if (args.length == 2)
47        {
48            robots[0] = player = args[0];
49            engine.setVisible(false);
50        }
51        else if (args.length > 2)
52        {
53            robots[0] = player = args[0];
54            for(int i = 1; i < args.length && i < robots.length; i++)
55                robots[i] = args[i];
56        }
57        else
58            engine.setVisible(false);
59
60        for (String s : robots)
61            bots += s + ", ";
62        bots = bots.substring(0, bots.length() - 1);
63
64        // Setup the battle specification
65
66        int numberOfRounds = 5;
67        BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600); // 800x600
68        RobotSpecification[] selectedRobots = engine.getLocalRepository(bots);
69
70        System.out.println("Bots: " + bots);
71        for(RobotSpecification s : selectedRobots)
72            System.out.println("Robot name: " + s.getName());
73
74        for (int i = 1; i < selectedRobots.length; i++) {
75            BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield, new RobotSpecification[] { selectedRobots[0], selectedRobots[i] });
76
77            // Run our specified battle and let it run till it is over
78            engine.runBattle(battleSpec, true); // waits till the battle finishes
79        }
80        // Cleanup our RobocodeEngine
81        engine.close();
82
83        //System.out.println(player);
84        //System.out.println(robots[0]);
85        //System.out.println(args[0]);
86        System.out.println(score / (robots.length - 1));
87
88        //for(String s : robots)
89        //    System.out.println("Robot name: " + s);
90
91        // Make sure that the Java VM is shut down properly
92        System.exit(0);
93    }
94}
95
96//
97// Our private battle listener for handling the battle event we are interested in.
98//
99class BattleObserver extends BattleAdaptor {
100
101    // Called when the battle is completed successfully with battle results
102    public void onBattleCompleted(BattleCompletedEvent e) {
103        //System.out.println("-- Battle has completed --");
104
105        // Print out the sorted results with the robot names
106        //System.out.println("Battle results:");
107        for (robocode.BattleResults result : e.getSortedResults()) {
108            //System.out.println("  " + result.getTeamLeaderName() + ": " + result.getScore());
109            if (result.getTeamLeaderName().equals(BattleRunner.player))
110                BattleRunner.score += result.getScore();
111            else
112                BattleRunner.score -= result.getScore();
113        }
114    }
115
116    // Called when the game sends out an information message during the battle
117    public void onBattleMessage(BattleMessageEvent e) {
118        //System.out.println("Msg> " + e.getMessage());
119    }
120
121    // Called when the game sends out an error message during the battle
122    public void onBattleError(BattleErrorEvent e) {
123        //System.out.println("Err> " + e.getError());
124    }
125}
Note: See TracBrowser for help on using the repository browser.