Пример #1
0
        public List <CommandPayload> Parse(string input)
        {
            List <CommandPayload> commandPayloads = new List <CommandPayload>();

            if (input.EndsWith(".txt", StringComparison.InvariantCultureIgnoreCase))
            {
                string[] lines = File.ReadAllLines(input);
                foreach (string line in lines)
                {
                    CommandPayload commandPayload = ParseCommandPayload(line);
                    commandPayloads.Add(commandPayload);
                }
            }
            else
            {
                CommandPayload commandPayload = ParseCommandPayload(input);
                commandPayloads.Add(commandPayload);
            }

            return(commandPayloads);
        }
Пример #2
0
        public Position Execute(CommandPayload commandPayload)
        {
            switch (commandPayload.Action)
            {
            case Action.Place:
            case Action.Move:
            case Action.Left:
            case Action.Right:
                positionCalculator.Calculate(commandPayload);
                break;

            case Action.Report:
                if (positionValidator.IsValid(positionCalculator.currentPosition.Coordinates) &&
                    positionValidator.IsValid(positionCalculator.currentPosition.Direction))
                {
                    graphics.Display(positionCalculator.currentPosition);
                }
                break;

            default:
                break;
            }
            return(positionCalculator.currentPosition);
        }
Пример #3
0
        public CommandPayload ParseCommandPayload(string payload)
        {
            CommandPayload commandPayload = new CommandPayload();

            string[] payloadSubset = payload.Split(new char[] { ' ' });
            string   action        = null;

            if (payloadSubset == null || payloadSubset.Length == 0)
            {
                action = payload;
            }
            else
            {
                action = payloadSubset[0];
            }

            if (0 == string.Compare(action, "Place", true))
            {
                string[] position = payloadSubset[1].Split(new char[] { ',' });

                commandPayload.Action   = Action.Place;
                commandPayload.Position = new Position();

                commandPayload.Position.Coordinates.X = Convert.ToInt32(position[0]);
                commandPayload.Position.Coordinates.Y = Convert.ToInt32(position[1]);
                if (0 == string.Compare("East", position[2], true))
                {
                    commandPayload.Position.Direction = Direction.East;
                }
                else if (0 == string.Compare("West", position[2], true))
                {
                    commandPayload.Position.Direction = Direction.West;
                }
                else if (0 == string.Compare("North", position[2], true))
                {
                    commandPayload.Position.Direction = Direction.North;
                }
                else if (0 == string.Compare("South", position[2], true))
                {
                    commandPayload.Position.Direction = Direction.South;
                }
            }
            else if (0 == string.Compare(action, "Move", true))
            {
                commandPayload.Action = Action.Move;
            }
            else if (0 == string.Compare(action, "Left", true))
            {
                commandPayload.Action = Action.Left;
            }
            else if (0 == string.Compare(action, "Right", true))
            {
                commandPayload.Action = Action.Right;
            }
            else if (0 == string.Compare(action, "Report", true))
            {
                commandPayload.Action = Action.Report;
            }

            return(commandPayload);
        }
Пример #4
0
        public Position Calculate(CommandPayload commandPayload)
        {
            switch (commandPayload.Action)
            {
            case Action.Place:
                bool isValid = positionValidator.IsValid(commandPayload.Position.Coordinates);
                if (isValid)
                {
                    currentPosition.Coordinates = commandPayload.Position.Coordinates;
                    currentPosition.Direction   = commandPayload.Position.Direction;
                }
                break;

            case Action.Move:
                if (positionValidator.IsValid(currentPosition.Coordinates))
                {
                    Coordinates newCoordinates = new Coordinates();
                    newCoordinates.X = currentPosition.Coordinates.X;
                    newCoordinates.Y = currentPosition.Coordinates.Y;

                    switch (currentPosition.Direction)
                    {
                    case Direction.East:
                        newCoordinates.X = newCoordinates.X + 1;
                        break;

                    case Direction.West:
                        newCoordinates.X = newCoordinates.X - 1;
                        break;

                    case Direction.North:
                        newCoordinates.Y = newCoordinates.Y + 1;
                        break;

                    case Direction.South:
                        newCoordinates.Y = newCoordinates.Y + 1;
                        break;

                    default:
                        break;
                    }
                    if (positionValidator.IsValid(newCoordinates))
                    {
                        currentPosition.Coordinates = newCoordinates;
                    }
                }
                break;

            case Action.Left:
            case Action.Right:
                if (positionValidator.IsValid(currentPosition.Direction))
                {
                    currentPosition.Direction = positions[currentPosition.Direction][commandPayload.Action];
                }
                break;

            default:
                break;
            }

            return(currentPosition);
        }