vendredi 1 juillet 2016

Robot Cleanner, solve with TDD and C#, complete solotion

I received the following question to solve with TDD but after I solved they told me it is not enough good. Please help me what is my mistake?

you can download my complete solution here

-----------------------Robot Cleanner---------------------------------------------------

Background:

When you have a lot of people working in an office it can get dirty quite quickly if you're not careful. However, cleaning staff are expensive. To save money on cleaning staff the best solution was deemed to be the creation of an automatic cleaning robot that cleans the office at night.

Assignment:

Your assignment is to build a prototype of this robot. The assignment is designed to be as simple as possible. The robot will, once given some instructions (shown below as input), run on its own without any human interference. In the morning we can ask the robot to report how many unique places in the office it has cleaned.

Input and Output Criteria:

• All input will be given on standard in.

• All output is expected on standard out.

• First input line: a single integer that represents the number of commands the robot should expect to execute before it knows it is done. The number will be in the range n (0 < n < 10, 000).

• Second input line: consists of two integer numbers that represents the starting coordinates x y of the robot. The value of each coordinate will be in the range x (-100, 000 < x < 100, 000) and y (-100, 000 y 100, 000).

• The third, and any subsequent line, will consist of two pieces of data. The first will be a single uppercase character c e {E, W, S, N), that represents the direction on the compass the robot should head. The second will be an integer representing the number of steps s (0 < s < 100,000) that the robot should take in said direction.

Special Notes :

• The robot will never be sent outside the bounds of the plane.

• All input should be considered well formed and syntactically correct. There is no need, therefore, to implement elaborate input parsing.

• Do not output any error messages. See previous point. The only output should be the number of unique places that the robot cleaned. See below.

• There will no leading or trailing white space on any line of input.

• There should be no leading or trailing whitespace on any line of output.

• Any multi-valued line of input will have a single white space character between each value.

• You can assume, for the sake of simplicity, that the office can be viewed as a grid where the robot moves only on the vertices.

• The robot cleans at every vertex it touches not just where it stops.

The Output :

The output of your program should be a number u, which represents the number of unique places in the office that were cleaned. The output of the number u should be prefixed by "=> Cleaned:" (excluding the quotes).

Example input:

2

10 22

E 2

N 1

Example output: => Cleaned: 4

---------------------------My Answer---------------------------------------------

Program.cs

using System;
using ClassLibraryRobotCleaner;

namespace ConsoleApplicationRobotCleaner
{
    class Program
    {
        static void Main(string[] args)
        {
            RobotCleaner robot = new RobotCleaner();
            robot.NumberOfCommands=Convert.ToInt32(Console.ReadLine());

            string[] coordinate = Console.ReadLine().Split(' ');
            robot.StartingCoordinates=new Coordinate( Convert.ToInt32(coordinate[0]), Convert.ToInt32(coordinate[1]));


            if (robot.NumberOfCommands > 0)
            {
                for (int i = 1; i <= robot.NumberOfCommands; i++)
                {
                    string[] vector = Console.ReadLine().Split(' ');
                    robot.AddVector(new Vector(Convert.ToChar(vector[0]), Convert.ToInt32(vector[1])));
                }
            }

            robot.StartSession();


            Console.WriteLine("=> Cleaned: " + robot.GetNumberOfCleanedPlaces());

            Console.ReadKey();


        }
    }
}

Test Class using ClassLibraryRobotCleaner; using NUnit.Framework;

namespace NUnit.TestsRobotCleaner
{
    [TestFixture]
    public class TestClassRobotCleaner
    {
        [Test]
        public void NumberOfCommands()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.NumberOfCommands = 2;

            Assert.AreEqual(crc.NumberOfCommands, 2);
        }


        [Test]
        public void StartingCoordinates()
        {
            RobotCleaner crc = new RobotCleaner();
            Coordinate cc = new Coordinate(2, 3);
            crc.StartingCoordinates = cc;

            Assert.AreEqual(cc.X, 2);
            Assert.AreEqual(cc.Y, 3);
        }

        [Test]
        public void AddVectors()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.AddVector(new Vector('E', 5));
            crc.AddVector(new Vector('S', 3));

            Assert.AreEqual(crc.GetNumberOfVectors(), 2);
        }

        [Test]
        public void GetOneVector()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.AddVector(new Vector('E', 5));
            crc.AddVector(new Vector('S', 3));
            crc.AddVector(new Vector('W', 2));

            Assert.AreEqual(crc.GetOneVector(0).Directon, 'E');
            Assert.AreEqual(crc.GetOneVector(0).Steps, 5);

            Assert.AreEqual(crc.GetOneVector(1).Directon, 'S');
            Assert.AreEqual(crc.GetOneVector(1).Steps, 3);

            Assert.AreEqual(crc.GetOneVector(2).Directon, 'W');
            Assert.AreEqual(crc.GetOneVector(2).Steps, 2);

        }



        [Test]
        public void GetCurrentCoordinate()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.NumberOfCommands = 0;
            crc.StartingCoordinates = new Coordinate(5, 4);
            crc.StartSession();

            Assert.AreEqual(crc.GetCurrentCoordinate().X, 5);
            Assert.AreEqual(crc.GetCurrentCoordinate().Y, 4);
        }


        [Test]
        public void GetNumberOfCleanedPlaces()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.NumberOfCommands = 0;
            crc.StartingCoordinates = new Coordinate(5, 4);
            crc.StartSession();

            Assert.AreEqual(crc.GetNumberOfCleanedPlaces(), 1);
        }


        [Test]
        public void GetCurrentCoordinate_WithOneCommand()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.NumberOfCommands = 1;
            crc.StartingCoordinates = new Coordinate(0, 0);
            crc.AddVector(new Vector('E', 1));
            crc.StartSession();

            Assert.AreEqual(crc.GetCurrentCoordinate().X, 1);
            Assert.AreEqual(crc.GetCurrentCoordinate().Y, 0);
        }

        [Test]
        public void GetCurrentCoordinate_WithTwoCommand()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.NumberOfCommands = 2;
            crc.StartingCoordinates = new Coordinate(0, 0);
            crc.AddVector(new Vector('E', 1));
            crc.AddVector(new Vector('N', 2));
            crc.StartSession();

            Assert.AreEqual(crc.GetCurrentCoordinate().X, 1);
            Assert.AreEqual(crc.GetCurrentCoordinate().Y, 2);
        }

        [Test]
        public void GetNumberOfCleanedPlaces_withTwoCommands()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.NumberOfCommands = 2;
            crc.StartingCoordinates = new Coordinate(5, 4);
            crc.AddVector(new Vector('E', 5));
            crc.AddVector(new Vector('N', 6));
            crc.StartSession();

            Assert.AreEqual(crc.GetNumberOfCleanedPlaces(), 12);
        }

        [Test]
        public void GetNumberOfCleanedPlaces_withTwoCommandsOverlap()
        {
            RobotCleaner crc = new RobotCleaner();
            crc.NumberOfCommands = 2;
            crc.StartingCoordinates = new Coordinate(5, 4);
            crc.AddVector(new Vector('E', 5));
            crc.AddVector(new Vector('W', 6));
            crc.StartSession();

            Assert.AreEqual(crc.GetNumberOfCleanedPlaces(), 7);
        }

    }

}

Robot Class

using System;
using System.Collections.Generic;

namespace ClassLibraryRobotCleaner
{
    public class RobotCleaner
    {
        public int NumberOfCommands { get; set; }
        public Coordinate StartingCoordinates { get; set; }

        private Coordinate _currentCoordinates;
        private List<Vector> _vectorsList = new List<Vector>();
        private List<Coordinate> _cleanedCoordinates = new List<Coordinate>();


        public void AddVector(Vector vector)
        {
            _vectorsList.Add(vector);
        }

        public int GetNumberOfVectors()
        {
            return _vectorsList.Count;
        }

        public Vector GetOneVector(int v)
        {
            return _vectorsList[v];
        }
        public Coordinate GetCurrentCoordinate()
        {
            return _currentCoordinates;
        }

        public int GetNumberOfCleanedPlaces()
        {
            return _cleanedCoordinates.Count;
        }
        public void StartSession()
        {

            GoToTheCoordinate(StartingCoordinates);
            DoCleaningCurrentPosition();

            for (int i = 0; i < NumberOfCommands; i++)
            {
                Vector v = _vectorsList[i];
                for (int j = 0; j < v.Steps; j++)
                {
                    GoToTheCoordinate(Vector.ConvertDirectionToCoordinate(v.Directon) + _currentCoordinates);
                    DoCleaningCurrentPosition();
                }
            }
        }

        private void DoCleaningCurrentPosition()
        {
            Coordinate ccc = _currentCoordinates;
            if (_cleanedCoordinates.Exists(l => l.X == ccc.X && l.Y == ccc.Y) == false)
            {
                //Robot Cleaning Right Now
                _cleanedCoordinates.Add(ccc);
            }
        }

        private void GoToTheCoordinate(Coordinate coordinate)
        {
            _currentCoordinates = coordinate;
        }

    }
}

Aucun commentaire:

Enregistrer un commentaire