Пример #1
0
        public void RunCode(string[] firstPlayerCode, string[] secondPlayerCode, string pathToField, GameFinishType finishType, IEnumerable <BLL.Models.Bot> bots1, IEnumerable <BLL.Models.Bot> bots2, int finalX = 0, int finalY = 0)
        {
            CompilationResult compResult1 = compiler.CompileCode(TaskParameters.Build(firstPlayerCode.Length), firstPlayerCode);
            CompilationResult compResult2 = compiler.CompileCode(TaskParameters.Build(secondPlayerCode.Length), secondPlayerCode);

            if (!compResult1.IsCodeCorrect || !compResult2.IsCodeCorrect)
            {
                throw new ArgumentException("Unable to compile code");
            }

            FieldBuilder fieldBuilder = new FieldBuilder(pathToField);

            Runner.CodeRunners.Models.Field field = fieldBuilder.GetFieldForRunner();
            field = fieldBuilder.PlaceBots(field, bots1.Select(b => new Models.Bot(b.X, b.Y, b.Name)), 1);
            field = fieldBuilder.PlaceBots(field, bots2.Select(b => new Models.Bot(b.X, b.Y, b.Name)), 2);

            FinishGameCondition finishCondition = null;

            if (finishType == GameFinishType.CommandsNumber)
            {
                finishCondition = new CommandNumberCondition();
            }
            else
            {
                finishCondition = new BotOnPointCondition(finalX, finalY);
            }

            IEnumerable <Runner.CodeRunners.Models.Bot> runnerBots1 = bots1.Select(mapper.Map <Runner.CodeRunners.Models.Bot>);
            IEnumerable <Runner.CodeRunners.Models.Bot> runnerBots2 = bots2.Select(mapper.Map <Runner.CodeRunners.Models.Bot>);

            runner.RunCodeGame(compResult1.InformationForCodeRunner, compResult2.InformationForCodeRunner, field, runnerBots1, runnerBots2, finishCondition);

            runner.GameFinished += Runner_GameFinished;
        }
Пример #2
0
        private static void testRunning()
        {
            string Bot1 = @"
                    using Bots.Models.Steps;
                    using Bots.Models;
                    namespace Bots.Core 
                    {
                        public class Bot1 : Bot 
                        {
                            public override Step NextStep(Field f) 
                            {
                                return new MoveStep(this, this.X + 1, this.Y);
                            }
                        }
                    }
                ";

            string Bot2 = @"
                    using Bots.Models.Steps;
                    using Bots.Models;
                    using System;

                    namespace Bots.Core 
                    {
                        public class Bot2 : Bot 
                        {
                            public override Step NextStep(Field f) 
                            {
                                Random r = new Random();

                                int next = r.Next(4);

                                switch(next) 
                                {
                                    case 0:
                                         return new MoveStep(this, this.X + 1, this.Y);
                                        break;
                                    case 1:
                                         return new MoveStep(this, this.X - 1, this.Y);
                                        break;
                                    case 2:
                                         return new MoveStep(this, this.X, this.Y + 1);
                                        break;
                                    case 3:
                                         return new MoveStep(this, this.X, this.Y - 1);
                                        break;

                                }

                                return new MoveStep(this, this.X, this.Y + 1);
                            }
                        }
                    }
                ";

            ILanguageProvider compProvider = new LanguageProvider();
            ICompiler         compiler     = compProvider.GetCompilerForLanguage(CompilerSupportedLanguages.CSharp);

            CompilationResult compileResult1 = compiler.CompileCode(TaskParameters.Build(2), Bot1, Bot2);

            Console.WriteLine($"Compilation result - { compileResult1.IsCodeCorrect }");

            if (compileResult1.Errors.Count > 0)
            {
                Console.Error.WriteLine("Detected errors");
            }

            foreach (string error in compileResult1.Errors)
            {
                Console.Error.WriteLine(error);
            }

            CompilationResult compileResult2 = compiler.CompileCode(TaskParameters.Build(2), Bot2, Bot1);

            if (compileResult1.IsCodeCorrect && compileResult2.IsCodeCorrect)
            {
                IRunnerProvider runProvider = new RunnerProvider();
                IRunner         runner      = runProvider.GetRunnerForLanguage(RunnerSupportedLanguages.CSharp);

                Field field = getRealField();

                compileResult1.InformationForCodeRunner.PlayerName = "player1";
                compileResult2.InformationForCodeRunner.PlayerName = "player2";

                runner.RunCodeGame(compileResult1.InformationForCodeRunner, compileResult2.InformationForCodeRunner, field, bots1.Select(b => new Runner.CodeRunners.Models.Bot()
                {
                    Name = b.Name, X = b.X, Y = b.Y
                }), bots2.Select(b => new Runner.CodeRunners.Models.Bot()
                {
                    Name = b.Name, X = b.X, Y = b.Y
                }),
                                   new CommandNumberCondition());

                runner.GameFinished += Runner_GameFinished;
            }

            Console.ReadKey();
        }