public new bool Enqueue(int x)
 {
     if (Count >= Parameters.MaxQueueSize)
     {
         return(false);
     }
     base.Enqueue(ModularArith.Mod(x, coresize));
     return(true);
 }
示例#2
0
        private void PlaceWarrior(RunningWarrior warrior, int address)
        {
            var statements = warrior.Warrior.Statements;

            for (var i = 0; i < statements.Count; ++i)
            {
                Memory[address + i] = new Instruction(statements[i], ModularArith.Mod(address + i), warrior.Index);
            }
        }
示例#3
0
 public void ChangeMemory(int address)
 {
     MemoryDiffs.Add(ModularArith.Mod(address));
 }
示例#4
0
        public StepResult Step()
        {
            if (GameOver)
            {
                return(new StepResult());
            }
            if (CurrentStep >= Parameters.MaxStepsPerWarrior * Warriors.Count)
            {
                GameOver = true;
                return(new StepResult());
            }

            CurrentIp = Warriors[CurrentWarrior].Queue.Dequeue();
            var instruction = Memory[CurrentIp];

            stepResult = new StepResult();

            ExecuteInstruction(Warriors[CurrentWarrior], instruction);

            if (stepResult.KilledInInstruction)
            {
                stepResult.ProgramStateDiffs.Add(new ProgramStateDiff {
                    Program = CurrentWarrior, NextPointer = null, ChangeType = ProcessStateChangeType.Killed
                });
                if (Warriors[CurrentWarrior].Queue.Count == 0)
                {
                    countLivedWarriors--;
                }
            }
            else
            {
                int nextIp;
                if (stepResult.SplittedInInstruction.HasValue)
                {
                    if (Warriors[CurrentWarrior].Queue.Count < Parameters.MaxQueueSize - 1)
                    {
                        nextIp = ModularArith.Mod(stepResult.SplittedInInstruction.GetValueOrDefault());
                        if (!Warriors[CurrentWarrior].Queue.Enqueue(nextIp))
                        {
                            throw new InvalidOperationException("Execution should never overflow queue");
                        }
                        stepResult.ProgramStateDiffs.Add(new ProgramStateDiff {
                            Program = CurrentWarrior, NextPointer = (uint?)nextIp, ChangeType = ProcessStateChangeType.Splitted
                        });
                    }
                }
                nextIp = ModularArith.Mod(stepResult.SetNextIP.HasValue ? stepResult.SetNextIP.GetValueOrDefault() : CurrentIp + 1);
                if (!Warriors[CurrentWarrior].Queue.Enqueue(nextIp))
                {
                    throw new InvalidOperationException("Execution should never overflow queue");
                }
                stepResult.ProgramStateDiffs.Add(new ProgramStateDiff {
                    Program = CurrentWarrior, NextPointer = (uint?)nextIp, ChangeType = ProcessStateChangeType.Executed
                });
            }

            CurrentStep++;

            var nextWarrior = GetNextWarrior(CurrentWarrior);

            LastExecutedWarrior = CurrentWarrior;
            CurrentWarrior      = nextWarrior.GetValueOrDefault();

            if (Warriors.Count > 1 && countLivedWarriors == 1 || Warriors.Count == 1 && countLivedWarriors == 0)
            {
                GameOver = true;
                Winner   = nextWarrior;
                return(stepResult);
            }
            return(stepResult);
        }
 public Instruction this[int index]
 {
     get { return(memory[ModularArith.Mod(index, coresize)]); }
     set { memory[ModularArith.Mod(index, coresize)] = value; }
 }