Пример #1
0
        public void SetCurrentPosition(Func <string> positionFunction)
        {
            if (positionFunction == null)
            {
                throw new ArgumentNullException(nameof(positionFunction));
            }
            var position = positionFunction();

            if (string.IsNullOrEmpty(position))
            {
                throw new ArgumentException("invalid position");
            }
            CurrentPosition = position;
        }
Пример #2
0
        public void Move(Direction dir, Rotatation?rotation)
        {
            switch (Face)
            {
            case Facing.North:
                if (dir == Direction.Forward)
                {
                    CurrentPosition -= 1;
                }
                else if (dir == Direction.Backward)
                {
                    CurrentPosition += 1;
                }
                if (rotation.HasValue)
                {
                    if (rotation == Rotatation.Clockwise)
                    {
                        Face = Facing.East;
                    }
                    else if (rotation == Rotatation.Anticlockwise)
                    {
                        Face = Facing.West;
                    }
                }
                break;

            case Facing.South:
                if (dir == Direction.Forward)
                {
                    CurrentPosition += 1;
                }
                else if (dir == Direction.Backward)
                {
                    CurrentPosition -= 1;
                }
                if (rotation == Rotatation.Clockwise)
                {
                    Face = Facing.West;
                }
                else if (rotation == Rotatation.Anticlockwise)
                {
                    Face = Facing.East;
                }
                break;

            case Facing.East:
                if (dir == Direction.Forward)
                {
                    CurrentPosition *= 1;
                }
                else if (dir == Direction.Backward)
                {
                    CurrentPosition /= 1;
                }
                if (rotation.HasValue)
                {
                    if (rotation == Rotatation.Clockwise)
                    {
                        Face = Facing.South;
                    }
                    else if (rotation == Rotatation.Anticlockwise)
                    {
                        Face = Facing.North;
                    }
                }
                break;

            case Facing.West:
                if (dir == Direction.Forward)
                {
                    CurrentPosition /= 1;
                }
                else if (dir == Direction.Backward)
                {
                    CurrentPosition *= 1;
                }

                if (rotation == Rotatation.Clockwise)
                {
                    Face = Facing.North;
                }
                else if (rotation == Rotatation.Anticlockwise)
                {
                    Face = Facing.South;
                }
                break;

            default: break;
            }
            if (
                CurrentPosition.Row < 0 ||
                CurrentPosition.Column < 0 ||
                CurrentPosition.Row >= _currentBoard.Dimension.Rows ||
                CurrentPosition.Column >= _currentBoard.Dimension.Columns
                )
            {
                CurrentPosition.SetPosition(-1, -1);
            }
        }