예제 #1
0
        public MarsRover(int coordX, int coordY, Direction direction, Plateau plateau, NavigationCommand[] navCommands)
        {
            if (plateau == null || (plateau.BottomLeftCoordX == plateau.TopRightCoordX && plateau.BottomLeftCoordY == plateau.TopRightCoordY))
            {
                //if Plateau initialization data is invalid/incomplete, we cannot create a rover that we can work with
                throw new ArgumentException("Plateau is invalid.");
            }

            if (navCommands == null || navCommands.Length == 0)
            {
                throw new ArgumentException("Invalid navigation commands");
            }

            Plateau = plateau;
            Direction = direction;
            NavigationCommands = navCommands;

            if (ValidateRoverCoordinates(coordX, coordY))
            {
                CoordX = coordX;
                CoordY = coordY;
            }
            else
            {
                //if initial values of Rover coordinates are not valid, place the rover in the approximate center of the plateau
                CoordX = Plateau.BottomLeftCoordX + (Plateau.TopRightCoordX - plateau.BottomLeftCoordY) / 2;
                CoordY = Plateau.BottomLeftCoordY + (Plateau.TopRightCoordY - plateau.BottomLeftCoordY) / 2;
            }
        }
예제 #2
0
        public void Navigate_CommandsWithMovesOutOfBounds_ReturnSkippedMoves_AssignCorrectCoords()
        {
            var plateau = new Plateau
            {
                BottomLeftCoordX = 0,
                BottomLeftCoordY = 0,
                TopRightCoordX = 5,
                TopRightCoordY = 5
            };

            //the second move command will be out of bounds
            var commands = new NavigationCommand[] { NavigationCommand.M, NavigationCommand.M, NavigationCommand.R, NavigationCommand.M };

            var rover = new MarsRover(4, 5, App.Enums.Direction.E, plateau, commands);

            var result = rover.Navigate(commands);

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Count, 1);
            Assert.AreEqual(result[0], 1);

            Assert.AreEqual(rover.CoordX, 5);
            Assert.AreEqual(rover.CoordY, 4);
            Assert.AreEqual(rover.Direction, Direction.S);
        }
예제 #3
0
        public void MarsRover_InvalidPlateau_Exception()
        {
            var plateau = new Plateau
            {
                BottomLeftCoordX = 1,
                BottomLeftCoordY = 1,
                TopRightCoordX = 1,
                TopRightCoordY = 1
            };

            var commands = new NavigationCommand[] { NavigationCommand.M, NavigationCommand.M, NavigationCommand.R, NavigationCommand.M };
            var rover = new MarsRover(1, 2, App.Enums.Direction.E, plateau, commands);
        }
예제 #4
0
        public void MarsRover_ValidRoverCoords_AssignRoverCoords()
        {
            var plateau = new Plateau
            {
                BottomLeftCoordX = 0,
                BottomLeftCoordY = 0,
                TopRightCoordX = 5,
                TopRightCoordY = 5
            };

            var commands = new NavigationCommand[] { NavigationCommand.M, NavigationCommand.M, NavigationCommand.R, NavigationCommand.M };
            var rover = new MarsRover(2, 3, App.Enums.Direction.E, plateau, commands);

            Assert.AreEqual(rover.CoordX, 2);
            Assert.AreEqual(rover.CoordY, 3);
        }
예제 #5
0
        public void TryMove_MoveInBounds_ReturnTrue_AssignCorrectCoords()
        {
            var plateau = new Plateau
            {
                BottomLeftCoordX = 0,
                BottomLeftCoordY = 0,
                TopRightCoordX = 5,
                TopRightCoordY = 5
            };

            var commands = new NavigationCommand[] { NavigationCommand.M, NavigationCommand.R, NavigationCommand.M, NavigationCommand.M, NavigationCommand.R };
            var rover = new MarsRover(4, 5, App.Enums.Direction.W, plateau, commands);

            var result = rover.TryMove(NavigationCommand.M);

            Assert.AreEqual(rover.CoordX, 3);
            Assert.AreEqual(rover.CoordY, 5);
            Assert.AreEqual(rover.Direction, Direction.W);
        }
예제 #6
0
        public void TryMove_Right_ReturnTrue_KeepOldCoords_ChangeDirection()
        {
            var plateau = new Plateau
            {
                BottomLeftCoordX = 0,
                BottomLeftCoordY = 0,
                TopRightCoordX = 5,
                TopRightCoordY = 5
            };

            var commands = new NavigationCommand[] { NavigationCommand.M, NavigationCommand.R, NavigationCommand.M, NavigationCommand.M, NavigationCommand.R };
            var rover = new MarsRover(4, 5, App.Enums.Direction.N, plateau, commands);

            var result = rover.TryMove(NavigationCommand.R);

            Assert.AreEqual(rover.CoordX, 4);
            Assert.AreEqual(rover.CoordY, 5);
            Assert.AreEqual(rover.Direction, Direction.E);
        }
예제 #7
0
        public MarsRoverStation(string inputFilePath)
        {
            if (string.IsNullOrEmpty(inputFilePath))
            {
                throw new ArgumentException("Input file path not entered.");
            }

            if (!File.Exists(inputFilePath))
            {
                throw new ArgumentException("Input file path invalid - file doesn't exist.");
            }

            var fileContents = File.ReadAllLines(inputFilePath);

            if (fileContents == null || fileContents.Length < 3)
            {
                throw new ArgumentException("Insufficient data in file.");
            }

            var plateauCoords = fileContents[0].Split(null);
            var coord = 0;
            if (plateauCoords == null || plateauCoords.Length < 2 || !int.TryParse(plateauCoords[0], out coord) || !int.TryParse(plateauCoords[1], out coord))
            {
                throw new ArgumentException("Invalid plateau initialization data");
            }

            var plateau = new Plateau
            {
                BottomLeftCoordX = 0,
                BottomLeftCoordY = 0,
                TopRightCoordX = int.Parse(plateauCoords[0]),
                TopRightCoordY = int.Parse(plateauCoords[1])
            };

            Rovers = new List<MarsRover>();

            var i = 1;
            while (i < fileContents.Length)
            {
                var roverInit = fileContents[i].Split(null);

                var direction = Direction.E;
                if (roverInit == null || roverInit.Length < 3 || !int.TryParse(roverInit[0], out coord) || !int.TryParse(roverInit[1], out coord) || !Enum.TryParse<Direction>(roverInit[2], out direction))
                {
                    throw new ArgumentException("Invalid rover init data.");
                }

                i++;
                if (string.IsNullOrEmpty(fileContents[i]))
                {
                    throw new ArgumentException("Invalid rover command data");
                }
                var commandsInit = fileContents[i].Select(c => c.ToString()).ToArray();

                var listCommands = new List<NavigationCommand>();
                foreach (var cmd in commandsInit)
                {
                    listCommands.Add((NavigationCommand)Enum.Parse(typeof(NavigationCommand), cmd));
                }
                
                var marsRover = new MarsRover(int.Parse(roverInit[0]), int.Parse(roverInit[1]), (Direction)Enum.Parse(typeof(Direction), roverInit[2]), plateau, listCommands.ToArray());
                Rovers.Add(marsRover);

                i++;
            }
        }