Ejemplo n.º 1
0
    static void Main(string[] args)
    {
        // Create the RobocodeEngine
        RobocodeEngine engine = new RobocodeEngine("C:\\robocode"); // Run from C:\Robocode

        // Add battle event handlers
        engine.BattleCompleted += new BattleCompletedEventHandler(BattleCompleted);
        engine.BattleMessage   += new BattleMessageEventHandler(BattleMessage);
        engine.BattleError     += new BattleErrorEventHandler(BattleError);

        // Show the Robocode battle view
        engine.Visible = true;

        // Disable log messages from Robocode
        RobocodeEngine.LogMessagesEnabled = false;

        // Setup the battle specification

        int numberOfRounds = 5;
        BattlefieldSpecification battlefield = new BattlefieldSpecification(800, 600); // 800x600

        RobotSpecification[] selectedRobots = engine.GetLocalRepository("sample.RamFire,sample.Corners");

        BattleSpecification battleSpec = new BattleSpecification(numberOfRounds, battlefield, selectedRobots);

        // Run our specified battle and let it run till it is over
        engine.RunBattle(battleSpec, true /* wait till the battle is over */);

        // Cleanup our RobocodeEngine
        engine.Close();
    }
Ejemplo n.º 2
0
        public void Battle(BattleTestCase testCase)
        {
            TestCaseFactory.SaveToDisk(testCase);


            RobotSpecification[] robots = _robocode.GetLocalRepository(testCase.AllRobotsCsv);

            var battle = new BattleSpecification(
                testCase.NumberOfRounds,
                testCase.BattlefieldSpecification,
                robots);

            _battleErrorEvents.Clear();
            _battleCompletedEvent = null;

            using (_wait = new AutoResetEvent(false))
            {
                _robocode.RunBattle(battle, false);

                _wait.WaitOne(testCase.TimeOut);
            }


            bool finished = _battleCompletedEvent != null;

            var adapter = new BattleResultAdapter();

            var outcomes = adapter.RecordOutcomes(
                testCase,
                finished ? _battleCompletedEvent.SortedResults : null,
                _battleErrorEvents.Select(e => e.Error),
                finished);

            TestCaseFactory.AddResult(outcomes);

            TestCaseFactory.ForgetAnyUnfinishedBusiness();


            var grouped = outcomes.GroupBy(o => o.OutcomeType).ToDictionary(g => g.Key);

            if (grouped.ContainsKey(OutcomeType.Lost))
            {
                var robotsThatBeatMe = grouped[OutcomeType.Lost];
                Assert.Fail("Lost to: " + robotsThatBeatMe.Select(e => e.EnemyName).Aggregate((n1, n2) => n1 + ", " + n2));
            }
            else if (grouped.ContainsKey(OutcomeType.Won))
            {
                var robotsThatIBeat = grouped[OutcomeType.Won];
                Console.WriteLine("Beat: " + robotsThatIBeat.Select(e => e.EnemyName).Aggregate((n1, n2) => n1 + ", " + n2));
            }
            else
            {
                Assert.Inconclusive();
            }
        }
        public void Run(RobocodeEngineParams engineParams)
        {
            _engine = new RobocodeEngine(Utility.RobocodeDir);

            // Event handlers
            _engine.BattleCompleted += BattleCompleted;
            _engine.BattleMessage   += BattleMessage;
            _engine.BattleError     += BattleError;

            // Setup
            _engine.Visible = true;
            var battlefieldSize = new BattlefieldSpecification(engineParams.Resolution.Width,
                                                               engineParams.Resolution.Height);

            var selectedRobots = _engine.GetLocalRepository(engineParams.RobotNames);
            var battleSpec     = new BattleSpecification(engineParams.NumRounds, engineParams.InactivityTime,
                                                         engineParams.GunCoolingRate, engineParams.HideNames,
                                                         battlefieldSize, selectedRobots);

            // Run battle
            _engine.RunBattle(battleSpec, true);
        }