import robocode.control.*; import robocode.control.events.*; // // Please add this file to the /libs forlder in the robocode installation directory, and compile it with the following command: // javac -cp .;robocode.jar BattleRunner.java // public class BattleRunner { static String[] defaultRobots = new String[] { "Evaluation.BestSolution*", "sample.Corners", "sample.Crazy", "sample.Fire", "sample.TrackFire", "sample.Walls", "sample.SpinBot", "sample.SittingDuck" }; public static String player = "Evaluation.output*"; //static int public static int score = 0; public static void main(String[] args) { // Disable log messages from Robocode RobocodeEngine.setLogMessagesEnabled(false); // Create the RobocodeEngine // RobocodeEngine engine = new RobocodeEngine(); // Run from current working directory RobocodeEngine engine = new RobocodeEngine(new java.io.File("F:/robocode")); // Run from C:/Robocode // Add our own battle listener to the RobocodeEngine engine.addBattleListener(new BattleObserver()); score = 0; String bots = ""; String[] robots = new String[defaultRobots.length]; robots = defaultRobots.clone(); if (args.length == 1) { robots[0] = player = args[0]; engine.setVisible(true); } else if (args.length == 2) { robots[0] = player = args[0]; engine.setVisible(false); } else if (args.length > 2) { robots[0] = player = args[0]; for(int i = 1; i < args.length && i < robots.length; i++) robots[i] = args[i]; } else engine.setVisible(false); for (String s : robots) bots += s + ", "; bots = bots.substring(0, bots.length() - 1); // Setup the battle specification int numberOfRounds = 5; BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600); // 800x600 RobotSpecification[] selectedRobots = engine.getLocalRepository(bots); System.out.println("Bots: " + bots); for(RobotSpecification s : selectedRobots) System.out.println("Robot name: " + s.getName()); for (int i = 1; i < selectedRobots.length; i++) { BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield, new RobotSpecification[] { selectedRobots[0], selectedRobots[i] }); // Run our specified battle and let it run till it is over engine.runBattle(battleSpec, true); // waits till the battle finishes } // Cleanup our RobocodeEngine engine.close(); //System.out.println(player); //System.out.println(robots[0]); //System.out.println(args[0]); System.out.println(score / (robots.length - 1)); //for(String s : robots) // System.out.println("Robot name: " + s); // Make sure that the Java VM is shut down properly System.exit(0); } } // // Our private battle listener for handling the battle event we are interested in. // class BattleObserver extends BattleAdaptor { // Called when the battle is completed successfully with battle results public void onBattleCompleted(BattleCompletedEvent e) { //System.out.println("-- Battle has completed --"); // Print out the sorted results with the robot names //System.out.println("Battle results:"); for (robocode.BattleResults result : e.getSortedResults()) { //System.out.println(" " + result.getTeamLeaderName() + ": " + result.getScore()); if (result.getTeamLeaderName().equals(BattleRunner.player)) BattleRunner.score += result.getScore(); else BattleRunner.score -= result.getScore(); } } // Called when the game sends out an information message during the battle public void onBattleMessage(BattleMessageEvent e) { //System.out.println("Msg> " + e.getMessage()); } // Called when the game sends out an error message during the battle public void onBattleError(BattleErrorEvent e) { //System.out.println("Err> " + e.getError()); } }