コード例 #1
0
    private static int[] Compute(int[] input)
    {
        int i      = 0;
        int opcode = -1;

        while (i < input.Length && opcode != 99)
        {
            Instruction instruction = new NullInstruction(input, i);
            opcode = input[i] % 100;
            var fullOpCode = input[i];
            switch (opcode)
            {
            case 99: break;

            case 1:
                instruction = new Add(input, i);
                break;

            case 2:
                instruction = new Multiply(input, i);
                break;

            case 3:
                instruction = new Input(input, i);
                break;

            case 4:
                instruction = new Output(input, i);
                break;

            case 5:
                instruction = new JumpIfTrue(input, i);
                break;

            case 6:
                instruction = new JumpIfFalse(input, i);
                break;

            case 7:
                instruction = new LessThan(input, i);
                break;

            case 8:
                instruction = new Equal(input, i);
                break;

            default: throw new ArgumentException();
            }
            (var newInput, var instructionPointer) = instruction.Compute();

            i     = instructionPointer;
            input = newInput;
        }
        return(input);
    }
コード例 #2
0
        private IOpcode GetOpCodeFromCurrentPointer()
        {
            var     opcodeValue = Convert.ToUInt32(new string(LocalComputerMemory.PointerValue().ToString().TakeLast(2).ToArray()));
            IOpcode opcode      = null;

            switch ((OpCode)opcodeValue)
            {
            case OpCode.Adds:
                opcode = new Add();
                break;

            case OpCode.Multiplie:
                opcode = new Multiply();
                break;

            case OpCode.Inputstuff:
                opcode = new Input();
                break;

            case OpCode.OutputStuff:
                opcode = new Output();
                break;

            case OpCode.JumpIfTrue:
                opcode = new JumpIfTrue();
                break;

            case OpCode.JumpIfFalse:
                opcode = new JumpIfFalse();
                break;

            case OpCode.isLessThen:
                opcode = new LessThan();
                break;

            case OpCode.isEqual:
                opcode = new Equal();
                break;

            case OpCode.RelativeBase:
                opcode = new RelativeBase();
                break;

            case OpCode.Exit:
                opcode = new Exit();
                break;

            default:
                break;
            }
            return(opcode);
        }
コード例 #3
0
 protected JumpIfTrueTests()
 {
     TestedInstruction = new JumpIfTrue();
 }