private void LoadRunPair(string fileOne, string fileTwo)
        {
            var rules    = Rules.DefaultRules;
            var pproject = new Project(rules);

            pproject.ParserOptions.Instructions = false;
            pproject.ParserOptions.StatusLine   = false;
            pproject.WarriorFiles.Add(fileOne);
            pproject.WarriorFiles.Add(fileTwo);

            var pr = new WarriorParser().Parse(pproject, wrappedConsole);

            if (!pr.Succesfull)
            {
                return;
            }

            var name1 = pproject.Warriors[0].Name;
            var name2 = pproject.Warriors[1].Name;

            name1 = name1.Substring(0, Math.Min(name1.Length, 20));
            name2 = name2.Substring(0, Math.Min(name2.Length, 20));
            Console.Write("Fighting {0} and {1}         \r", name1, name2);
            new EngineSteps().Run(pproject, wrappedConsole);
        }
Example #2
0
        private static void RunServer([NotNull] IEnumerable <string> args)
        {
            var staticContentPath    = GetStaticContentDir();
            var settings             = GetSettingsFile(args);
            var httpListenerPrefix   = settings.HttpListenerPrefix;
            var warriorProgramParser = new WarriorParser();
            var playersRepo          = new PlayersRepo(new DirectoryInfo("../players"), warriorProgramParser);
            var gamesRepo            = new CachingGamesRepo(new GamesRepo(new DirectoryInfo("../games")));
            var sessionManager       = new SessionManager("../sessions");
            var gameServer           = new GameServer();
            var debuggerManager      = new DebuggerManager(gameServer);
            var battleRunner         = new BattleRunner();
            var countdownProvider    = new CountdownProvider(settings.ContestStartTimestamp, TimeSpan.FromHours(settings.ContestDurationInHours));
            var arenaState           = new ArenaState(playersRepo, gamesRepo, countdownProvider, settings.GodModeSecret, settings.GodAccessOnly, settings.SubmitIsAllowed, settings.EnableDeepNavigation);
            var tournamentRunner     = new TournamentRunner(arenaState, battleRunner, settings.BattlesPerPair);
            var httpServer           = new GameHttpServer(httpListenerPrefix, arenaState, sessionManager, debuggerManager, tournamentRunner, staticContentPath);

            Runtime.SetStopHandler(() =>
            {
                log.InfoFormat("Stopping...");
                httpServer.Stop();
                tournamentRunner.Stop();
            });
            tournamentRunner.Start();
            httpServer.Run();
            log.InfoFormat("Listening on: {0}", httpListenerPrefix);
            if (!settings.ProductionMode)
            {
                Process.Start(httpServer.DefaultUrl);
            }
            httpServer.WaitForTermination();
            tournamentRunner.WaitForTermination();
            log.InfoFormat("Stopped");
        }
Example #3
0
 public override void ExpandStatements(ExtendedWarrior warrior, WarriorParser parser, ref int currentAddress,
     int coreSize, bool evaluate)
 {
     foreach (Statement statement in Statements)
     {
         statement.ExpandStatements(warrior, parser, ref currentAddress, coreSize, evaluate);
     }
 }
Example #4
0
 public override int Evaluate(WarriorParser parser, int currentAddress)
 {
     int l = left.Evaluate(parser, currentAddress);
     int r = right.Evaluate(parser, currentAddress);
     switch (operation)
     {
         case BinaryOperation.Plus:
             return l + r;
         case BinaryOperation.Minus:
             return l - r;
         case BinaryOperation.Multiply:
             return l * r;
         case BinaryOperation.Divide:
             if (r == 0)
             {
                 parser.WriteError("Divide by zero during evaluation of " + ToString() + " at " + Location, Location);
                 return 0;
             }
             return l / r;
         case BinaryOperation.Modulo:
             if (r == 0)
             {
                 parser.WriteError("Divide by zero during evaluation of " + ToString() + " at " + Location, Location);
                 return 0;
             }
             return l % r;
         case BinaryOperation.BinOr:
             return l | r;
         case BinaryOperation.BinXor:
             return l ^ r;
         case BinaryOperation.BinAnd:
             return l & r;
         case BinaryOperation.Shl:
             return l << r;
         case BinaryOperation.Shr:
             return l >> r;
         case BinaryOperation.Or:
             return ((l != 0) || (r != 0)) ? 1 : 0;
         case BinaryOperation.And:
             return ((l != 0) && (r != 0)) ? 1 : 0;
         case BinaryOperation.CompareGt:
             return (l > r) ? 1 : 0;
         case BinaryOperation.CompareGe:
             return (l >= r) ? 1 : 0;
         case BinaryOperation.CompareLe:
             return (l <= r) ? 1 : 0;
         case BinaryOperation.CompareLt:
             return (l < r) ? 1 : 0;
         case BinaryOperation.CompareEq:
             return (l == r) ? 1 : 0;
         case BinaryOperation.CompareNe:
             return (l != r) ? 1 : 0;
         default:
             throw new InvalidOperationException();
     }
 }
Example #5
0
 public override int Evaluate(WarriorParser parser, int currentAddress)
 {
     switch (operation)
     {
         case UnaryOperation.Negate:
             return 0 - sub.Evaluate(parser, currentAddress);
         case UnaryOperation.Brackets:
             return sub.Evaluate(parser, currentAddress);
         default:
             throw new InvalidOperationException();
     }
 }
Example #6
0
 public override Mode GetMode(WarriorParser parser, int currentAddress)
 {
     int l = left.Evaluate(parser, currentAddress);
     Mode m = middle.GetMode(parser, currentAddress);
     Mode r = right.GetMode(parser, currentAddress);
     switch (operation)
     {
         case TernaryOperation.If:
             return (l != 0) ? m : r;
         default:
             throw new InvalidOperationException();
     }
 }
Example #7
0
 public override int Evaluate(WarriorParser parser, int currentAddress)
 {
     if (inEval)
     {
         parser.WriteError("Cyclic definition of function : " + name + " at " + Location, Location);
         return 0;
     }
     try
     {
         return EvaluateInternal(parser, currentAddress);
     }
     finally
     {
         inEval = false;
     }
 }
Example #8
0
 public override void ExpandStatements(ExtendedWarrior warrior, WarriorParser parser, ref int currentAddress,
     int coreSize, bool evaluate)
 {
     //set labels, except last which is EQU expression
     for (int l = 0; l < Labels.Count; l++)
     {
         LabelName label = Labels[l];
         if (l == Labels.Count - 1)
         {//equ
             parser.variables[label.GetFullName(parser, currentAddress)] = expression;
         }
         else
         {//labels
             parser.variables[label.GetFullName(parser, currentAddress)] = new Address(currentAddress);
         }
     }
     return;
 }
Example #9
0
 public override Mode GetMode(WarriorParser parser, int currentAddress)
 {
     string fullName = GetFullName(parser, currentAddress);
     if (parser.variables.ContainsKey(fullName))
     {
         Expression ex = parser.variables[fullName];
         if (ex == this)
         {
             parser.WriteError("Label not yet resolved: " + fullName + " at " + Location, Location);
             return 0;
         }
         return ex.GetMode(parser, currentAddress);
     }
     else
     {
         parser.WriteError("Label not defined: " + fullName + " at " + Location, Location);
         return 0;
     }
 }
Example #10
0
        public Game([NotNull] ProgramStartInfo[] programStartInfos)
        {
            this.programStartInfos = programStartInfos;

            var r      = new RandomAllocator(Parameters.CoreSize, Parameters.MinWarriorsDistance);
            var parser = new WarriorParser();

            var lastAddress = 0;

            warriors = new List <WarriorStartInfo>();
            foreach (var psi in programStartInfos)
            {
                var warrior = parser.Parse(psi.Program);
                warriors.Add(new WarriorStartInfo(
                                 warrior,
                                 psi.StartAddress.HasValue ? (int)psi.StartAddress : r.NextLoadAddress(lastAddress, warrior.Length)
                                 ));
                lastAddress = warriors.Last().LoadAddress + warriors.Last().Warrior.Length;
            }

            Init();
        }
        public void Single()
        {
            var rules = Rules.DefaultRules;

            rules.WarriorsCount = 1;

            //var fileOne = Path.Combine(basePath, "maniacs/5/coleman5.red");
            //var fileOne = Path.Combine(basePath, @"pycorewar\Koenigstuhl\94\aggression.red");
            var fileOne = Path.Combine(basePath, @"imp.red");

            var pproject = new Project(rules, fileOne);

            pproject.ParserOptions.Instructions = true;
            pproject.ParserOptions.StatusLine   = false;

            var pr = new WarriorParser().Parse(pproject, wrappedConsole);

            if (pr.Succesfull)
            {
                new EngineSteps().Run(pproject, wrappedConsole);
            }
        }
Example #12
0
 public override int Evaluate(WarriorParser parser, int currentAddress)
 {
     return Original.Evaluate(parser, currentAddress);
 }
Example #13
0
 public override Mode GetMode(WarriorParser parser, int currentAddress)
 {
     Mode l = left.GetMode(parser, currentAddress);
     if (l != Mode.NULL)
         return l;
     return right.GetMode(parser, currentAddress);
 }
Example #14
0
 public int GetValue(WarriorParser parser, int currentAddress)
 {
     return Expression.Evaluate(parser, currentAddress);
 }
Example #15
0
 public abstract Mode GetMode(WarriorParser parser, int currentAddress);
Example #16
0
 public int Evaluate(WarriorParser parser, int currentAddress, int coreSize)
 {
     int raw = Evaluate(parser, currentAddress);
     return Instruction.Wrap(raw, coreSize);
 }
Example #17
0
 public abstract int Evaluate(WarriorParser parser, int currentAddress);
 public int GetValue(WarriorParser parser, int currentAddress)
 {
     return(Expression.Evaluate(parser, currentAddress));
 }
Example #19
0
 public override Mode GetMode(WarriorParser parser, int currentAddress)
 {
     return sub.GetMode(parser, currentAddress);
 }
Example #20
0
 public override int Evaluate(WarriorParser parser, int currentAddress)
 {
     return value;
 }
Example #21
0
 protected virtual int EvaluateInternal(WarriorParser parser, int currentAddress)
 {
     string fullName = GetFullName(parser, currentAddress);
     if (parser.variables.ContainsKey(fullName))
     {
         Expression ex = parser.variables[fullName];
         if (ex == this)
         {
             parser.WriteError("Label not yet resolved: " + fullName + " at " + Location, Location);
             return 0;
         }
         return ex.Evaluate(parser, currentAddress);
     }
     else
     {
         parser.WriteError("Label not defined: " + fullName + " at " + Location, Location);
         return 0;
     }
 }
Example #22
0
 public override string GetFullName(WarriorParser parser, int currentAddress)
 {
     return name + parameter.Evaluate(parser, currentAddress).ToString("00");
 }
Example #23
0
 public abstract void ExpandStatements(ExtendedWarrior warrior, WarriorParser parser, ref int currentAddress,
     int coreSize, bool evaluate);
Example #24
0
 public void SetUp()
 {
     parser = new WarriorParser();
 }
Example #25
0
 public virtual string GetFullName(WarriorParser parser, int currentAddress)
 {
     return name;
 }
Example #26
0
 public override Mode GetMode(WarriorParser parser, int currentAddress)
 {
     return Mode.NULL;
 }
Example #27
0
        public override void ExpandStatements(ExtendedWarrior warrior, WarriorParser parser, ref int currentAddress,
            int coreSize, bool evaluate)
        {
            //set all labels
            foreach (LabelName label in Labels)
            {
                parser.variables[label.GetFullName(parser, currentAddress)] = new Address(currentAddress);
            }

            ExtendedInstruction instruction;
            if (!evaluate)
            {
                instruction =
                    new ExtendedInstruction(Operation, Modifier, A.Mode, Int32.MinValue, B.Mode, Int32.MinValue);
                instruction.Address = currentAddress;
                warrior.Add(instruction);
            }
            else
            {
                instruction = (ExtendedInstruction)warrior.Instructions[currentAddress];
                instruction.ValueA = Instruction.Mod(A.Expression.Evaluate(parser, currentAddress, coreSize), coreSize);
                instruction.ValueB = Instruction.Mod(B.Expression.Evaluate(parser, currentAddress, coreSize), coreSize);
                instruction.ExpressionA = A.Expression.ToString();
                instruction.ExpressionB = B.Expression.ToString();
                if (instruction.ModeA == Mode.NULL)
                {
                    instruction.ModeA=A.Expression.GetMode(parser, currentAddress);
                }
                if (instruction.ModeA == Mode.NULL)
                {
                    instruction.ModeA = Mode.Direct;
                }
                if (instruction.ModeB == Mode.NULL)
                {
                    instruction.ModeB=B.Expression.GetMode(parser, currentAddress);
                }
                if (instruction.ModeB == Mode.NULL)
                {
                    instruction.ModeB = Mode.Direct;
                }
                if (instruction.Modifier == Modifier.NULL)
                {
                    instruction.Modifier =
                        Instruction.DefaultModifier(instruction.Operation, instruction.ModeA, instruction.ModeB);
                }

                if (Comments != null)
                {
                    instruction.Comment = Comments[Comments.Count - 1];
                }
                else
                {
                    instruction.Comment = "";
                }
                instruction.OriginalInstruction = OriginalInstruction;

                if (Labels.Count > 0)
                {
                    instruction.Label = Labels[Labels.Count - 1].GetFullName(parser, currentAddress);
                }
                else
                {
                    instruction.Label = "";
                }
            }
            currentAddress++;
            parser.variables["CURLINE"] = new Value(currentAddress);
        }