Пример #1
0
        public Ship(Coords initialCoords, Orientation intitialOrientation, Ocean ocean)
        {
            CurrentPosition    = initialCoords;
            CurrentOrientation = intitialOrientation;
            _ocean             = ocean;

            Ocean.MovementResult res = _ocean.TryMove(null, CurrentPosition);

            if (res == Ocean.MovementResult.Lost)
            {
                _isLost = true;
            }
        }
Пример #2
0
        /// <summary>
        /// Try to move forward.
        /// </summary>
        private void tryForwardMovement()
        {
            Coords newPos = CurrentPosition;

            switch (CurrentOrientation)
            {
            case Orientation.North:
                newPos = new Coords(CurrentPosition.XCoord, CurrentPosition.YCoord + 1);
                break;

            case Orientation.East:
                newPos = new Coords(CurrentPosition.XCoord + 1, CurrentPosition.YCoord);
                break;

            case Orientation.South:
                newPos = new Coords(CurrentPosition.XCoord, CurrentPosition.YCoord - 1);
                break;

            case Orientation.West:
                newPos = new Coords(CurrentPosition.XCoord - 1, CurrentPosition.YCoord);
                break;
            }

            Ocean.MovementResult res = _ocean.TryMove(CurrentPosition, newPos);

            if (res == Ocean.MovementResult.Lost)
            {
                _isLost = true;
                _ocean.AddWarning(CurrentPosition);
            }
            else if (res == Ocean.MovementResult.Warning)
            {
                // Ship has received warning not to move. Nothing to do.
            }
            else
            {
                // Ship succeeded in moving, update position.
                CurrentPosition = newPos;
            }
        }