Exemple #1
0
        /// <summary>
        /// Determine next direction
        /// </summary>
        /// <param name="currentDirection"></param>
        /// <param name="mapBoard"></param>
        /// <param name="characterPath"></param>
        /// <returns></returns>
        private static MapMove FindNextMove(MapMove currentDirection, MapDirections mapBoard, StringBuilder characterPath)
        {
            var direction = currentDirection;

            if (currentDirection == MapMove.None)
            {
                direction = GetDirection(MapMove.Up, MapMove.Right, mapBoard, characterPath);
                direction = GetDirection(direction, MapMove.Down, mapBoard, characterPath);
                direction = GetDirection(direction, MapMove.Left, mapBoard, characterPath);
            }
            else if (!mapBoard.IsValidMove(currentDirection))
            {
                // if current position is up or down go left or right and vice versa
                if (currentDirection == MapMove.Up || currentDirection == MapMove.Down)
                {
                    direction = GetDirection(MapMove.Left, MapMove.Right, mapBoard, characterPath);
                }
                else if (currentDirection == MapMove.Left || currentDirection == MapMove.Right)
                {
                    direction = GetDirection(MapMove.Up, MapMove.Down, mapBoard, characterPath);
                }
            }

            return(direction);
        }
Exemple #2
0
        /// <summary>
        /// Based on two input directions determine next move
        /// </summary>
        /// <param name="first"></param>
        /// <param name="second"></param>
        /// <param name="mapBoard"></param>
        /// <param name="characterPath"></param>
        /// <returns></returns>
        private static MapMove GetDirection(MapMove first, MapMove second, MapDirections mapBoard, StringBuilder characterPath)
        {
            var takeFirst  = first != MapMove.None && mapBoard.IsValidMove(first);
            var takeSecond = second != MapMove.None && mapBoard.IsValidMove(second);

            var firstDirectionMark  = GetDirectionMove(first, mapBoard.SearchNextChar(first));
            var secondDirectionMark = GetDirectionMove(second, mapBoard.SearchNextChar(second));

            // first check for possible invalid moves
            // only one direction is valid
            if (takeFirst && firstDirectionMark && takeSecond && secondDirectionMark)
            {
                string exceptionMessage = "Path is invalid at character: " + characterPath.ToString();
                throw new MapException(exceptionMessage);
            }
            // only one direction is valid
            else if (takeFirst && takeSecond)
            {
                string exceptionMessage = "Path is invalid at character: " + characterPath.ToString();
                throw new MapException(exceptionMessage);
            }
            // take valid move
            else if (takeFirst && firstDirectionMark)
            {
                return(first);
            }
            else if (takeSecond && secondDirectionMark)
            {
                return(second);
            }
            else if (takeFirst)
            {
                return(first);
            }
            else if (takeSecond)
            {
                return(second);
            }
            else
            {
                return(MapMove.None);
            }
        }
Exemple #3
0
        /// <summary>
        /// Iterate until end character is found and build map
        /// </summary>
        /// <param name="mapBoard"></param>
        /// <returns></returns>
        private static MapTravelResult CreateMapResult(MapDirections mapBoard)
        {
            var letters       = new StringBuilder();
            var characterPath = new StringBuilder();

            var currentDirection = MapMove.None;

            while (true)
            {
                var currentElement = mapBoard.CurrentElement;
                characterPath.Append(currentElement);

                if (currentElement == MapConstant.End)
                {
                    break;
                }

                if (char.IsLetter(currentElement) && !mapBoard.CurrentPosition)
                {
                    letters.Append(currentElement);
                }

                currentDirection = FindNextMove(currentDirection, mapBoard, characterPath);

                if (currentDirection == MapMove.None)
                {
                    string exceptionMessage = "Path is invalid at character: " + characterPath.ToString();
                    throw new MapException(exceptionMessage);
                }


                mapBoard.AdjustIndexes(currentDirection);
            }

            return(new MapTravelResult(letters.ToString(), characterPath.ToString()));
        }