示例#1
0
        public void RoverShouldDetectObstacleBeforeMoving()
        {
            var obstacle1 = new Tuple <int, int>(5, 3);
            var obstacle2 = new Tuple <int, int>(1, 1);

            var pluto = new Pluto(5, 5);

            pluto.AddObstacle(obstacle1);
            pluto.AddObstacle(obstacle2);

            var rover = new Rover(3, 3, Orientation.East);

            rover.DeployTo(pluto);
            rover.Move("FF");
            var expectedPosition = new Position(4, 3, Orientation.East);

            Assert.That(rover.Position, Is.EqualTo(expectedPosition));
            Assert.That(rover.DetectedObstacle, Is.EqualTo(obstacle1));

            rover = new Rover(5, 1, Orientation.East);
            rover.DeployTo(pluto);
            rover.Move("FF");
            expectedPosition = rover.Position;
            Assert.That(rover.Position, Is.EqualTo(expectedPosition));
            Assert.That(rover.DetectedObstacle, Is.EqualTo(obstacle2));
        }
示例#2
0
        public string ExecuteInstructions(Rover rover, string instructions, Pluto pluto)
        {
            var result = "";

            foreach (var c in instructions)
            {
                IInstructionOperation operation = null;

                operation = Instruct(rover, c.ToString(), pluto);
                if (operation == null)
                {
                    return($"{c} is not a valid Instruction");
                }
                var instruction = new Instruction(pluto, rover, c.ToString());

                result = operation.Execute(instruction);

                if (result == "IsObstacle")
                {
                    return(result);
                }
            }
            return(result);
        }
示例#3
0
 public void RoverInPlutoShouldBeAbleToMoveAroundThePlanetWithoutFallingOff(Rover rover, Pluto pluto, string commands, Position expectedPosition)
 {
     rover.DeployTo(pluto);
     rover.Move(commands);
     Assert.That(rover.Position, Is.EqualTo(expectedPosition));
 }
示例#4
0
        private static IInstructionOperation Instruct(Rover rover, string instruction, Pluto pluto)
        {
            if (Enum.TryParse <TurnInstructions>(instruction, true, out var turnInstruction))
            {
                return(new TurnOperation());
            }

            if (Enum.TryParse <MoveInstructions>(instruction, true, out var moveInstruction))
            {
                return(new MovementOperation());
            }

            return(null);
        }
示例#5
0
        private void Move(Command command, Pluto pluto)
        {
            int candidateX = x;
            int candidateY = y;

            switch (command)
            {
            case Command.Forward:
                if (orientation == Orientation.North)
                {
                    candidateY = candidateY + 1;
                }
                else if (orientation == Orientation.South)
                {
                    candidateY = candidateY - 1;
                }
                else if (orientation == Orientation.East)
                {
                    candidateX = candidateX + 1;
                }
                else
                {
                    candidateX = candidateX - 1;
                }
                break;

            case Command.Backward:
                if (orientation == Orientation.North)
                {
                    candidateY = candidateY - 1;
                }
                else if (orientation == Orientation.South)
                {
                    candidateY = candidateY + 1;
                }
                else if (orientation == Orientation.East)
                {
                    candidateX = candidateX - 1;
                }
                else
                {
                    candidateX = candidateX + 1;
                }
                break;

            case Command.Left:
                if (orientation == Orientation.North)
                {
                    orientation = Orientation.West;
                }
                else if (orientation == Orientation.South)
                {
                    orientation = Orientation.East;
                }
                else if (orientation == Orientation.East)
                {
                    orientation = Orientation.North;
                }
                else
                {
                    orientation = Orientation.South;
                }
                break;

            case Command.Right:
                if (orientation == Orientation.North)
                {
                    orientation = Orientation.East;
                }
                else if (orientation == Orientation.South)
                {
                    orientation = Orientation.West;
                }
                else if (orientation == Orientation.East)
                {
                    orientation = Orientation.South;
                }
                else
                {
                    orientation = Orientation.North;
                }
                break;

            default:
                throw new Exception($"Command '{command}' is not valid.");
            }

            bool isBlocked = false;

            if (pluto != null)
            {
                List <Tuple <int, int> > obstacles = pluto.Obstacles;
                for (int i = 0; i < obstacles.Count && !isBlocked; i++)
                {
                    Tuple <int, int> obstacle = obstacles[i];
                    int obstacleX             = obstacle.Item1;
                    int obstacleY             = obstacle.Item2;
                    if (obstacle.Item1 == candidateX && obstacle.Item2 == candidateY)
                    {
                        detectedObstacle = new Tuple <int, int>(obstacleX, obstacleY);
                        isBlocked        = true;
                    }
                }

                int maxX = pluto.Width;
                int maxY = pluto.Length;
                if (candidateX > maxX)
                {
                    candidateX = candidateX - maxX - 1;
                }
                if (candidateX < 0)
                {
                    candidateX = maxX + candidateX + 1;
                }

                if (candidateY > maxY)
                {
                    candidateY = candidateY - maxY - 1;
                }
                if (candidateY < 0)
                {
                    candidateY = maxY + candidateY + 1;
                }
            }

            if (!isBlocked)
            {
                x = candidateX;
                y = candidateY;
            }
        }
示例#6
0
 public void DeployTo(Pluto pluto)
 {
     this.pluto = pluto;
 }