Example #1
0
        public void MoveShip(ShipMove move)
        {
            if (move.Direction == MoveDirection.Left ||
                move.Direction == MoveDirection.Right)
            {
                var turnsToMake = move.Amount / 90;

                int turnModifier;

                if (move.Direction == MoveDirection.Left)
                {
                    // 3 rights make a left
                    turnModifier = 3;
                }
                else
                {
                    turnModifier = 1;
                }

                var rightTurns = turnsToMake * turnModifier;

                for (var i = 0; i < rightTurns; i++)
                {
                    var yTemp = waypointY;

                    // Y becomes the previous x
                    waypointY = waypointX;

                    // X becomes y * -1
                    waypointX = yTemp * -1;

                    //WayX = 4 N
                    //WayY = 10 E

                    //One right turn means we go to
                    //WayY = 4 E (prev way X)
                    //WayX = -10 S (prev way Y * -1)

                    //Another right should be
                    //Way Y = -10 W (prev way X)
                    //Way X = -4 S (prev way Y * -1)

                    //A third right should be
                    //Way Y = -4 W (prev way X)
                    //Way X = 10 N (prev way Y * -1)

                    //Fourth right (back to start)
                    //Way Y = 10 E (prev way X)
                    //Way X = 4 N (prev way Y * -1)

                    //Way X -> 4  -> -10 -> -4  -> 10 -> 4
                    //Way Y -> 10 -> 4   -> -10 -> -4 -> 10
                }

                return;
            }

            if (move.Direction == MoveDirection.Forward)
            {
                // Move towards the waypoint
                xPos += waypointX * move.Amount;
                yPos += waypointY * move.Amount;
                return;
            }

            // Move the waypoint
            switch (move.Direction)
            {
            case MoveDirection.North:
                waypointX += move.Amount;
                break;

            case MoveDirection.South:
                waypointX -= move.Amount;
                break;

            case MoveDirection.East:
                waypointY += move.Amount;
                break;

            case MoveDirection.West:
                waypointY -= move.Amount;
                break;

            default:
                throw new InvalidOperationException($"Invalid move direction {move.Direction}.");
            }
        }
Example #2
0
        public void MoveShip(ShipMove move)
        {
            if (move.Direction == MoveDirection.Left ||
                move.Direction == MoveDirection.Right)
            {
                var turnsToMake = move.Amount / 90;

                int turnModifier;

                if (move.Direction == MoveDirection.Left)
                {
                    // 3 rights make a left
                    turnModifier = 3;
                }
                else
                {
                    turnModifier = 1;
                }

                var currentFacingInt = (int)currentFacing;

                currentFacingInt += turnsToMake * turnModifier;

                currentFacingInt = Math.Abs(currentFacingInt);

                Math.DivRem(currentFacingInt,
                            4,
                            out currentFacingInt);

                currentFacing = (MoveDirection)currentFacingInt;
                return;
            }

            var directionToMove = move.Direction;

            if (directionToMove == MoveDirection.Forward)
            {
                directionToMove = currentFacing;
            }

            switch (directionToMove)
            {
            case MoveDirection.North:
                xPos += move.Amount;
                break;

            case MoveDirection.South:
                xPos -= move.Amount;
                break;

            case MoveDirection.East:
                yPos += move.Amount;
                break;

            case MoveDirection.West:
                yPos -= move.Amount;
                break;

            default:
                throw new InvalidOperationException($"Invalid move direction {directionToMove}.");
            }
        }