Esempio n. 1
0
 public Position()
 {
     Coordinates = new Coordinates();
 }
Esempio n. 2
0
 public PositionValidator(Coordinates minCordinates, Coordinates maxCoordinates)
 {
     MinCordinates  = minCordinates;
     MaxCoordinates = maxCoordinates;
 }
Esempio n. 3
0
 public Position(Coordinates coordinates, Direction direction)
 {
     Coordinates = coordinates;
     Direction   = Direction;
 }
Esempio n. 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);
        }