public void RunInstructions_Move_faceNorth()
        {
            var instructions = new LawnmowerInstructions
            {
                BoundaryWidth = 5,
                BoundaryHeight = 5,
                StartX = 3,
                StartY = 3,
                StartDirection = "E",
                Instructions = new List<char>
                {
                    'M',
                    'M',
                    'R',
                    'M',
                    'M',
                    'R',
                    'M',
                    'R',
                    'R',
                    'M'
                }
            };

            var result = lawnmowerCommaner.RunInstructions(instructions);

            Assert.AreEqual(5, result.X);
            Assert.AreEqual(1, result.Y);
            Assert.AreEqual("E", result.Facing);
        }
        public void RunInstructions_Move_MoveNorthOnce()
        {
            var instructions = new LawnmowerInstructions
            {
                BoundaryWidth = 5,
                BoundaryHeight = 5,
                StartX = 1,
                StartY = 2,
                StartDirection = "N",
                Instructions = new List<char>
                {
                    'L',
                    'M',
                    'L',
                    'M',
                    'L',
                    'M',
                    'L',
                    'M',
                    'M'
                }
            };

            var result = lawnmowerCommaner.RunInstructions(instructions);

            Assert.AreEqual(1, result.X);
            Assert.AreEqual(3, result.Y);
            Assert.AreEqual("N", result.Facing);
        }
Exemplo n.º 3
0
        public LawnmowerPosition RunInstructions(LawnmowerInstructions lawnmowerInstructions)
        {
            var lawnmower = new LawnmowerPosition
            {
                Facing = lawnmowerInstructions.StartDirection,
                X = lawnmowerInstructions.StartX,
                Y = lawnmowerInstructions.StartY
            };

           foreach(var instruction in lawnmowerInstructions.Instructions)
            {
                lawnmower.Facing = GetNewPosition(lawnmower.Facing, instruction);
                lawnmower.X = getXPosition(lawnmower.X, lawnmowerInstructions.BoundaryWidth,lawnmower.Facing, instruction);
                lawnmower.Y = getYPosition(lawnmower.Y, lawnmowerInstructions.BoundaryHeight, lawnmower.Facing, instruction);
            }

            return lawnmower;
        }
        private List<LawnmowerInstructions> CreateInstructions(string rawInstructions)
        {
            int countLines = 0;
            var output = new List<LawnmowerInstructions>();
            var lawnmowerInstructionBoundary = (lawnmowerBoundaries) null;
            var tempLawnmowerInstruction = new LawnmowerInstructions();

            using (var reader = new StringReader(rawInstructions))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    countLines++;
                    var commands = line.Split(' ');

                    if (countLines == 1)
                    {
                        lawnmowerInstructionBoundary = GetBoundaries(commands);
                        if(lawnmowerInstructionBoundary == null)
                        {
                            return new List<LawnmowerInstructions>();
                        }
                        continue;
                    }

                    if (lineIsAboutLawnmowerPoistion(countLines))
                    {
                        tempLawnmowerInstruction = GetLawnmowerPosition(commands);
                        if(tempLawnmowerInstruction == null)
                        {
                            return new List<LawnmowerInstructions>();
                        }
                        continue;
                    }

                    AddMovementCommandsAndAddToOutputList(line, lawnmowerInstructionBoundary, tempLawnmowerInstruction, output);
                }
            }

            return output;
        }
 private void AddMovementCommandsAndAddToOutputList(string commands, lawnmowerBoundaries lawnmowerInstructionBoundary, LawnmowerInstructions tempLawnmowerInstruction, List<LawnmowerInstructions> output)
 {
     tempLawnmowerInstruction.BoundaryWidth = lawnmowerInstructionBoundary.X;
     tempLawnmowerInstruction.BoundaryHeight = lawnmowerInstructionBoundary.Y;
     tempLawnmowerInstruction.Instructions = commands.ToCharArray();
     output.Add(tempLawnmowerInstruction);
 }