예제 #1
0
        public void Lawn_Mower_Should_Decrease_Y_By_One_When_Moving_If_Is_Heading_South()
        {
            var lawnMower = new LawnMower(1, 1, LawnMower.Direction.S, 5, 5);
            lawnMower.Move();

            Assert.AreEqual(1, lawnMower.GetPosition().X);
            Assert.AreEqual(0, lawnMower.GetPosition().Y);
        }
예제 #2
0
 public void Lawn_Mower_Should_End_In_Position_One_Three_North_If_Initial_Position_Is_One_Two_North_And_Commands_Are_LMLMLMLMM()
 {
     var lawnMower = new LawnMower(1, 2, LawnMower.Direction.N, 5, 5);
     var success = lawnMower.ExecuteCommands("LMLMLMLMM");
     Assert.AreEqual(true, success);
     Assert.AreEqual(LawnMower.Direction.N, lawnMower.GetPosition().Heading);
     Assert.AreEqual(1, lawnMower.GetPosition().X);
     Assert.AreEqual(3, lawnMower.GetPosition().Y);
 }
예제 #3
0
 public void Lawn_Mower_Should_End_In_Position_Five_One_East_If_Initial_Position_Is_Three_Three_East_And_Commands_Are_MMRMMRMRRM()
 {
     var lawnMower = new LawnMower(3, 3, LawnMower.Direction.E, 5, 5);
     var success = lawnMower.ExecuteCommands("MMRMMRMRRM");
     Assert.AreEqual(true, success);
     Assert.AreEqual(LawnMower.Direction.E, lawnMower.GetPosition().Heading);
     Assert.AreEqual(5, lawnMower.GetPosition().X);
     Assert.AreEqual(1, lawnMower.GetPosition().Y);
 }
예제 #4
0
 public void Lawn_Mower_Should_Head_South_When_Command_Is_Right_If_Is_Heading_East()
 {
     var lawnMower = new LawnMower(0, 0, LawnMower.Direction.E, 5, 5);
     lawnMower.ChangeDirection(LawnMower.Command.R);
     Assert.AreEqual(LawnMower.Direction.S, lawnMower.GetPosition().Heading);
 }
예제 #5
0
        public void Lawn_Mower_Should_Not_Move_When_Y_Is_Zero_If_Is_Heading_South()
        {
            var lawnMower = new LawnMower(0, 0, LawnMower.Direction.S, 5, 5);
            lawnMower.Move();

            Assert.AreEqual(0, lawnMower.GetPosition().X);
            Assert.AreEqual(0, lawnMower.GetPosition().Y);
        }
예제 #6
0
        public void Lawn_Mower_Should_Not_Move_When_X_Is_Equal_To_Lawn_With_If_Is_Heading_East()
        {
            var lawnMower = new LawnMower(5, 0, LawnMower.Direction.E, 5, 5);
            lawnMower.Move();

            Assert.AreEqual(5, lawnMower.GetPosition().X);
            Assert.AreEqual(0, lawnMower.GetPosition().Y);
        }
예제 #7
0
        public void Lawn_Mower_Should_Not_Move_When_Y_Is_Equal_To_Lawn_Height_If_Is_Heading_North()
        {
            var lawnMower = new LawnMower(0, 5, LawnMower.Direction.N, 5, 5);
            lawnMower.Move();

            Assert.AreEqual(0, lawnMower.GetPosition().X);
            Assert.AreEqual(5, lawnMower.GetPosition().Y);
        }
예제 #8
0
 public void Lawn_Mower_Should_Not_Instantiate_And_Throw_Argument_Exception_If_X_Greater_Than_Lawn_Width()
 {
     var lawnMower = new LawnMower(7, 0, LawnMower.Direction.W, 5, 5);
 }
예제 #9
0
 public void Lawn_Mower_Should_Not_Instantiate_And_Throw_Argument_Exception_If_Y_Greater_Than_Lawn_Height()
 {
     var lawnMower = new LawnMower(0, 7, LawnMower.Direction.W, 5, 5);
 }
예제 #10
0
 public void Lawn_Mower_Should_Not_Execute_Commands_If_One_Of_The_Commands_Is_Invalid()
 {
     var lawnMower = new LawnMower(0, 0, LawnMower.Direction.W, 5, 5);
     var success = lawnMower.ExecuteCommands("MLRZ");
     Assert.AreEqual(false, success);
 }
예제 #11
0
 public void Lawn_Mower_Should_Not_Change_Direction_And_Throw_Argument_Exception_If_Command_Is_Not_L_Or_R()
 {
     var lawnMower = new LawnMower(0, 0, LawnMower.Direction.W, 5, 5);
     lawnMower.ChangeDirection(LawnMower.Command.M);
 }
예제 #12
0
        public void Lawn_Mower_Should_Increase_X_By_One_When_Moving_If_Is_Heading_East()
        {
            var lawnMower = new LawnMower(0, 0, LawnMower.Direction.E, 5, 5);
            lawnMower.Move();

            Assert.AreEqual(1, lawnMower.GetPosition().X);
            Assert.AreEqual(0, lawnMower.GetPosition().Y);
        }
예제 #13
0
 public void Lawn_Mower_Should_Head_West_When_Command_Is_Left_If_Is_Heading_North()
 {
     var lawnMower = new LawnMower(0, 0, LawnMower.Direction.N, 5, 5);
     lawnMower.ChangeDirection(LawnMower.Command.L);
     Assert.AreEqual(LawnMower.Direction.W, lawnMower.GetPosition().Heading);
 }
예제 #14
0
        private static void MownTheLawn(string inputFilePath)
        {
            var tempSplitPath = inputFilePath.Split('\\');
            var outputFilePath = inputFilePath.Replace(tempSplitPath[tempSplitPath.Length - 1], "output.txt");

            using (var reader = new StreamReader(inputFilePath))
            {
                using (var writer = new StreamWriter(outputFilePath))
                {
                    var readLine = reader.ReadLine();
                    if (readLine == null)
                    {
                        FinishProgram("Input file has no lines");
                        return;
                    }

                    var fieldSize = readLine.Split(' ');
                    var fieldWidth = 0u;
                    var fieldHeight = 0u;

                    if (fieldSize.Length < 2 || !uint.TryParse(fieldSize[0], out fieldWidth) ||
                         !uint.TryParse(fieldSize[1], out fieldHeight))
                    {
                        FinishProgram("The height/width of the Lawn is invalid. Expected 2 numbers separated by whitespace, for example: 5 5");
                    }

                    var lawnMowerIndex = 1;
                    while (!reader.EndOfStream)
                    {
                        var mowerLine = reader.ReadLine();

                        if (mowerLine == null)
                        {
                            FinishProgram("The position of the lawn mower #" + lawnMowerIndex + " is not in the file");
                            return;
                        }

                        var position = mowerLine.Split(' ');

                        var x = 0u;
                        var y = 0u;
                        var heading = LawnMower.Direction.N;

                        if (position.Length < 3 || !uint.TryParse(position[0], out x) ||
                            !uint.TryParse(position[1], out y) || !Enum.TryParse(position[2], out heading))
                        {
                            FinishProgram("The position of the lawn mower #" + lawnMowerIndex +
                                          " is invalid. Expected 2 numbers and a direction (N,S,E,W) separated by whitespace, for example: 1 2 N");
                        }

                        var lawnMower = new LawnMower(x, y, heading, fieldWidth, fieldHeight);

                        var commandsLine = reader.ReadLine();

                        if (commandsLine == null)
                        {
                            FinishProgram("No commands provided for lawn mower #" + lawnMowerIndex +
                                         ". Expected a string with a combination of commands (L,R,M), for example: LMLMLMLMM");
                            return;
                        }

                        var success = lawnMower.ExecuteCommands(commandsLine);

                        if (!success)
                        {
                            FinishProgram("The commands provided for lawn mower #" + lawnMowerIndex +
                                          "are invalid. Expected a string with a combination of commands (L,R,M) with no whitespaces, for example: LMLMLMLMM");
                        }

                        writer.WriteLine(lawnMower.GetPosition().ToString());

                        lawnMowerIndex++;
                    }
                }
            }

            Console.WriteLine("");
            Console.WriteLine("Output file with LawnMowers final positions was created successfully '" + outputFilePath + "'");
        }