Esempio n. 1
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);
 }
Esempio n. 2
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);
 }
Esempio n. 3
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);
 }
Esempio n. 4
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 + "'");
        }