Exemple #1
0
        public override ShipPosition Move(ShipPosition position)
        {
            if (position.Lost)
            {
                return(position);
            }

            switch (position.Orientation)
            {
            case Orientation.East:
                position.Orientation = Orientation.North;
                break;

            case Orientation.North:
                position.Orientation = Orientation.West;
                break;

            case Orientation.West:
                position.Orientation = Orientation.South;
                break;

            case Orientation.South:
                position.Orientation = Orientation.East;
                break;

            default:
                throw new NotImplementedException(string.Format("Ship's orientation {0} is not supported for the Left Instruction", position.Orientation.ToString()));
            }

            return(position);
        }
Exemple #2
0
        public override bool WillBeLost(ShipPosition position)
        {
            //TODO refactor to make cleaner

            if (position.Orientation == Orientation.East && position.X < TopRight.X)
            {
                return(false);
            }

            if (position.Orientation == Orientation.West && position.X > BottomLeft.X)
            {
                return(false);
            }

            if (position.Orientation == Orientation.North && position.Y < TopRight.Y)
            {
                return(false);
            }

            if (position.Orientation == Orientation.South && position.Y > BottomLeft.Y)
            {
                return(false);
            }

            return(true);
        }
Exemple #3
0
        public override ShipPosition Move(ShipPosition position)
        {
            if (position.Lost)
            {
                return(position);
            }

            //validate if the move will result in the ship being lost
            if (WillBeLost(position))
            {
                //mark position as lost, keep old poistion
                position.Lost = true;
                return(position);
            }

            //move position based on orientation
            switch (position.Orientation)
            {
            case Orientation.East:
                position.X = position.X + MovePositions;
                break;

            case Orientation.North:
                position.Y = position.Y + MovePositions;
                break;

            case Orientation.West:
                position.X = position.X - MovePositions;
                break;

            case Orientation.South:
                position.Y = position.Y - MovePositions;
                break;

            default:
                throw new NotImplementedException(string.Format("Ship's orientation {0} is not supported for the Move Instruction", position.Orientation.ToString()));
            }

            return(position);
        }
Exemple #4
0
 /// <summary>
 /// WillBeLost : Validates whether the instruction can be executed on the ships current position
 /// </summary>
 /// <param name="position">current position of the ship</param>
 /// <returns></returns>
 public virtual bool WillBeLost(ShipPosition position)
 {
     return(false);
 }
Exemple #5
0
 //for a given position, move to new position/orientation
 public abstract ShipPosition Move(ShipPosition position);