コード例 #1
0
        public IList <IInstruction> Parse(string input)
        {
            List <IInstruction> instructions = new List <IInstruction>();

            //e.g. LMLMLMLMM
            foreach (char c in input.ToUpper())
            {
                IInstruction newInstruction = new NullInstruction();;

                switch (c)
                {
                case 'L':
                    newInstruction = new LeftInstruction();
                    break;

                case 'M':
                    newInstruction = new MoveInstruction();
                    break;

                case 'R':
                    newInstruction = new RightInstruction();
                    break;

                default:
                    break;
                }

                instructions.Add(newInstruction);
            }

            return(instructions);
        }
コード例 #2
0
        public void Should_Change_Orientation_Counterclockwise_When_LeftInstruction_Moves()
        {
            ShipPosition    position    = new ShipPosition(5, 5, Orientation.West);
            LeftInstruction instruction = new LeftInstruction(topRight, bottomLeft);

            //test
            ShipPosition actual = instruction.Move(position);

            //assert
            Assert.AreEqual(Orientation.South, actual.Orientation, "orientation not correct");
            Assert.AreEqual(position.X, actual.X, "ship should not have moved x corordinate");
            Assert.AreEqual(position.Y, actual.Y, "ship should not have moved y corordinate");
            Assert.AreEqual(false, actual.Lost, "ship should not be lost");
        }
コード例 #3
0
        // Replace scan loop [<] and [>] with single instruction
        public List <InstructionBase> OptimizeScanLoop(List <InstructionBase> instructions)
        {
            List <InstructionBase> optimized = new List <InstructionBase>();

            for (int i = 0; i < instructions.Count; i++)
            {
                InstructionBase instruction       = instructions[i];
                bool            optimizationFound = false;
                if (instruction is OpenInstruction && i < instructions.Count - 2)
                {
                    LeftInstruction left = instructions[i + 1] as LeftInstruction;
                    if (left?.X == 1 && instructions[i + 2] is CloseInstruction)
                    {
                        Debug.WriteLine("Left scan loop found");
                        optimizationFound = true;
                        optimized.Add(new ScanLeftInstruction());
                        i += 2;
                    }
                    else
                    {
                        RightInstruction right = instructions[i + 1] as RightInstruction;
                        if (right?.X == 1 && instructions[i + 2] is CloseInstruction)
                        {
                            Debug.WriteLine("Right scan loop found");
                            optimizationFound = true;
                            optimized.Add(new ScanRightInstruction());
                            i += 2;
                        }
                    }
                }
                if (!optimizationFound)
                {
                    optimized.Add(instruction);
                }
            }

            return(optimized);
        }