コード例 #1
0
        public static IEnumerable <MoveCoordinate> ConvertCommandsToMoveCoordinates(string commands)
        {
            var coordinates = new List <MoveCoordinate>();

            int count = 0;

            while (count < commands.Length)
            {
                string posStringToMove = string.Empty;
                char   rotationLetter  = '\0';

                if (char.IsLetter(commands[count]))
                {
                    rotationLetter = commands[count];
                    count++;
                }

                while (char.IsNumber(commands[count]))
                {
                    posStringToMove = posStringToMove + commands[count];

                    count++;

                    if (count >= commands.Length || char.IsLetter(commands[count]))
                    {
                        break;
                    }
                }

                var posIntToMove = int.Parse(posStringToMove);
                var rotation     = rotationLetter == 'R' ? Rotation.Right : Rotation.Left;

                var moveCoordinate = new MoveCoordinate
                {
                    Rotation       = rotation,
                    PositionToMove = posIntToMove
                };
                coordinates.Add(moveCoordinate);
            }

            return(coordinates);
        }
コード例 #2
0
        private bool UpdateNextGridLocation(MoveCoordinate moveCoordinate)
        {
            if (moveCoordinate == null)
            {
                throw new ArgumentNullException(nameof(moveCoordinate));
            }

            var success = false;

            GridLocation.Direction = moveCoordinate.Rotation == Rotation.Right
                ? _directionsRight[GridLocation.Direction]
                : _directionsLeft[GridLocation.Direction];

            switch (GridLocation.Direction)
            {
            case Direction.N:
                MoveNorth(moveCoordinate.PositionToMove);
                success = true;
                break;

            case Direction.E:
                MoveEast(moveCoordinate.PositionToMove);
                success = true;
                break;

            case Direction.S:
                MoveSouth(moveCoordinate.PositionToMove);
                success = true;
                break;

            case Direction.W:
                MoveWest(moveCoordinate.PositionToMove);
                success = true;
                break;

            default:
                success = false;
                break;
            }

            return(success);
        }