Пример #1
0
        public void MoveRover(MarsRoverContext marsRoverContext, Planet planet)
        {
            // Note that I am mutating properties directly from the given arguments. It is better to avoid this because,
            // if for any reason the compiler decides to pas it by value instead of by ref, the method will not behave as expected.
            // Also I am aware of the ref keyword but still, the C# compiler should pass these objects by ref as default.
            int planetXAxis = planet.Size.GetLength(0);
            int planetYAxis = planet.Size.GetLength(1);

            Point currentPoint = marsRoverContext.MarsRover.Location;
            int   currentX = marsRoverContext.MarsRover.Location.X;
            int   currentY = marsRoverContext.MarsRover.Location.Y;
            int   newX = 0, newY = 0;

            CardinalDirection cardinalDirectionRover = marsRoverContext.MarsRover.FacingDirection;

            // North move will cause +1 on the Y-axis
            // East move will cause a +1 on the X-axis
            // South move will cause a -1 on the Y-axis
            // West move will cause a -1 on the X-axis
            switch (cardinalDirectionRover)
            {
            case CardinalDirection.North:
                newX = currentX;
                newY = currentY + 1;
                break;

            case CardinalDirection.East:
                newX = currentX + 1;
                newY = currentY;
                break;

            case CardinalDirection.South:
                newX = currentX;
                newY = currentY - 1;
                break;

            case CardinalDirection.West:
                newX = currentX - 1;
                newY = currentY;
                break;
            }

            if (newY >= planetYAxis || newX >= planetXAxis || newY < 0 || newX < 0)
            {
                Console.WriteLine("The Mars rover is now out of the grid");
                marsRoverContext.SetState(new StuckState());
                return;
            }

            Point newMarsRoverLocation = new Point(newX, newY);

            // For collision later
            planet.RemoveObject(currentPoint);
            planet.PlaceObject(newMarsRoverLocation);
            marsRoverContext.MarsRover.SetNewLocation(newMarsRoverLocation);

            Console.WriteLine($"The Mars rover moved to X {newX} and Y {newY}");
        }
Пример #2
0
        public void MoveRover(MarsRover marsRover, Move marsRoverMove)
        {
            MarsRoverContext marsRoverResult = MarsRovers.Find(rover => rover.MarsRover.Equals(marsRover));

            if (marsRoverResult == null)
            {
                throw new Exception("Mars rover not found.");
            }

            marsRoverResult.MoveRover(marsRoverMove, this);
        }
Пример #3
0
 public void MoveRover(MarsRoverContext context, Planet planet)
 {
     Console.WriteLine("The Mars rover is stuck. Please contact a mechanic.");
 }