/// <summary>
        /// Parses all instructions and returns the command interface list.
        /// </summary>
        /// <param name="instructions">String</param>
        /// <returns>Commands</returns>
        public IEnumerable <ICommand> Parse(string instructions)
        {
            if (string.IsNullOrWhiteSpace(instructions))
            {
                throw new ArgumentNullException(null, "Instructions parameter cannot be null or empty");
            }

            var instructionLines = instructions.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

            if (instructionLines.Length < GameSettingsDefaults.MinimumFormatLength)
            {
                throw new ArgumentException($"The length of the settings cannot be lower than minimum length {GameSettingsDefaults.MinimumFormatLength}");
            }

            var commands = new List <ICommand>();

            #region Line 1: The first line should define the board size

            var boardSize = instructionLines[0];
            _boardValidator.IsValidBoardSize(boardSize);

            commands.Add(ParseBoardSetCommand(boardSize));


            #endregion

            #region Line 2: The second line should contain a list of mines (i.e. list of co-ordinates separate by a space)

            var listOfMines = instructionLines[1];
            _obstaclesValidator.IsValidListOfObstacles(listOfMines);

            var point = listOfMines.Split(' '); // 1,1 1,3 3,3
            commands.AddRange(point.Select(ParseMineSetCommand));


            #endregion

            #region Line 3: The third line of the file should contain the exit point

            var exitPoint = instructionLines[2];
            _exitValidator.IsValidExitPoint(exitPoint);

            commands.Add(ParseExitSetCommand(exitPoint));


            #endregion

            #region Line 4: The fourth line of the file should contain the starting position of the turtle.

            var turtleStartingPosition = instructionLines[3];
            var directions             = Enum.GetValues(typeof(Direction)).Cast <Direction>().Select(x => x.ToString()).ToList();
            _animalValidator.IsValidAnimalStartingPoint(directions, turtleStartingPosition);

            commands.Add(ParseTurtleSetCommand(turtleStartingPosition));


            #endregion

            #region Line 5, ...: The fifth line to the end of the file should contain a series of moves.

            var movementInstructions = new StringBuilder();

            foreach (var instructionLine in instructionLines.Skip(4))
            {
                var movements = Enum.GetValues(typeof(Movement)).Cast <Movement>().Select(x => x.ToString()).ToList();
                _animalValidator.IsValidAnimalMovements(movements, instructionLine);

                movementInstructions.AppendFormat("{0} ", instructionLine);
            }

            if (!string.IsNullOrEmpty(movementInstructions.ToString()))
            {
                commands.Add(ParseTurtleMoveCommand(movementInstructions.ToString().Trim()));
            }

            #endregion

            return(commands);
        }
示例#2
0
        public void Should_ThrowArgumentNullException_When_TheExitPointComesNullOrWhiteSpace(string exitPoint)
        {
            var exception = Assert.Throws <ArgumentNullException>(() => _exitValidator.IsValidExitPoint(exitPoint));

            Assert.Equal("Exit point parameter cannot be null or empty", exception.Message);
        }