예제 #1
0
        private RobotOrientation getRobotOrientationFromChar(char orientationChar)
        {
            RobotOrientation result = RobotOrientation.North;

            switch (orientationChar)
            {
            case 'N':
                result = RobotOrientation.North;
                break;

            case 'W':
                result = RobotOrientation.West;
                break;

            case 'S':
                result = RobotOrientation.South;
                break;

            case 'E':
                result = RobotOrientation.East;
                break;

            default:
                throw new Exception("Wrong orientation provided to the engine");
            }
            return(result);
        }
예제 #2
0
 /// <summary>
 /// Translates an orientation information into a transmittable <code>string</code>.
 /// </summary>
 /// <param name="orientation">The orientation to transmit.</param>
 /// <returns>The <code>string</code> representation of the message.</returns>
 public static string EncodeFrom(RobotOrientation orientation)
 {
     return
         (RobotMessageResultClient.IDENTIFIER_ORIENT +
          CommunicationConstants.MSG_SUB_DELIMITER +
          orientation.ToString());
 }
예제 #3
0
 public MartianRobot(int x, int y, RobotOrientation orientation)
 {
     this.X           = x;
     this.Y           = y;
     this.Orientation = orientation;
     this.IsLost      = false;
 }
예제 #4
0
        /// <summary>
        /// Encodes a position info message to a transmittable string.
        /// </summary>
        /// <param name="robotID">The robot the position info is about.</param>
        /// <param name="waypointID">The waypoint the robot is positioned at.</param>
        /// <param name="robotOrientation">The orientation the robot is currently in.</param>
        /// <returns>A transmittable string of the information.</returns>
        public static string EncodeTo(int robotID, int waypointID, RobotOrientation robotOrientation)
        {
            // Translate orientation to radians
            double orientation = 0.0;

            switch (robotOrientation)
            {
            case RobotOrientation.North: orientation = Math.PI * 1.5; break;

            case RobotOrientation.West: orientation = Math.PI; break;

            case RobotOrientation.South: orientation = Math.PI * 0.5; break;

            case RobotOrientation.East: orientation = 0; break;

            default: throw new ArgumentException("Unknown orientation type: " + robotOrientation);
            }
            // Return resulting string
            return
                (ControlMessageResultServer.IDENTIFIER_POS +
                 CommunicationConstants.MSG_SUB_DELIMITER +
                 robotID.ToString() +
                 CommunicationConstants.MSG_SUB_DELIMITER +
                 waypointID.ToString() +
                 CommunicationConstants.MSG_SUB_DELIMITER +
                 orientation.ToString());
        }
예제 #5
0
        public static void SettingAndGettingOrientation(RobotOrientation orientation, char orientationChar)
        {
            var robot = new Robot();

            robot.SetOrientation(orientation);
            Assert.Equal(orientation, robot.GetOrientation());
            Assert.Equal(orientationChar, robot.GetOrientationAsChar());
        }
예제 #6
0
 private void UpdatePosition(int robotID, int waypointID, RobotOrientation orientation)
 {
     this.Dispatcher.Invoke(() =>
     {
         _botPositionBlocks[robotID].Text    = "Position: " + waypointID;
         _botOrientationBlocks[robotID].Text = "Orientation: " + orientation;
     });
 }
예제 #7
0
 public Robot(Point position, RobotOrientation robotOrientation)
 {
     Alive       = true;
     Coordinates = position;
     Orientation = robotOrientation;
     _robotNames[RobotOrientation.East]  = "E";
     _robotNames[RobotOrientation.North] = "N";
     _robotNames[RobotOrientation.South] = "S";
     _robotNames[RobotOrientation.West]  = "W";
 }
예제 #8
0
        public static int CreateRobot(int x, int y, RobotOrientation orientation)
        {
            Robot robot;

            robot.X           = x;
            robot.Y           = y;
            robot.Orientation = orientation;


            return(Arena.Instance.AddRobot(robot));
        }
        private void TurnLeft()
        {
            RobotOrientation orientation = robot.GetOrientation();

            if (orientation == RobotOrientation.North)
            {
                orientation = RobotOrientation.West;
            }
            else
            {
                orientation--;
            }
            robot.SetOrientation(orientation);
        }
        private void TurnRight()
        {
            RobotOrientation orientation = robot.GetOrientation();

            if (orientation == RobotOrientation.West)
            {
                orientation = RobotOrientation.North;
            }
            else
            {
                orientation++;
            }
            robot.SetOrientation(orientation);
        }
예제 #11
0
        public PlaceRobotAction(int x, int y, RobotOrientation orientation)
        {
            if (x < 0 || x > 50)
            {
                throw new ArgumentException(nameof(x));
            }

            if (y < 0 || y > 50)
            {
                throw new ArgumentException(nameof(y));
            }

            this._x           = x;
            this._y           = y;
            this._orientation = orientation;
        }
        private void Move()
        {
            RobotOrientation orientation = robot.GetOrientation();

            int[] robotDisplacement = MoveRobot.GetRobotDisplacement(orientation);
            int[] currentPosition   = robot.GetPosition();
            int[] finalPosition     = { currentPosition[0] + robotDisplacement[0],
                                        currentPosition[1] + robotDisplacement[1] };

            if (!arena.CheckPosition(finalPosition[0], finalPosition[1]))
            {
                throw new Exception("Move puts robot out of the arena");
            }

            robot.SetPosition(finalPosition[0], finalPosition[1]);
        }
예제 #13
0
        public static void Moving(int endPositionX, int endPositionY, RobotOrientation orientation)
        {
            var arenaMock = new Mock <IArena>();
            var robotMock = new Mock <IRobot>();

            int[] mockGetPositionReturn = { 4, 1 };
            arenaMock.Setup(x => x.CheckPosition(endPositionX, endPositionY)).Returns(true);
            robotMock.Setup(x => x.GetOrientation()).Returns(orientation);
            robotMock.Setup(x => x.GetPosition()).Returns(mockGetPositionReturn);
            string commandStream = "M";

            var robotActionExecutor = new RobotActionExecutor(robotMock.Object, commandStream, arenaMock.Object);

            robotActionExecutor.ExecuteAllActions();

            robotMock.Verify(x => x.SetPosition(endPositionX, endPositionY), Times.Once());
        }
예제 #14
0
        public override void Process(string command)
        {
            if (!Validate(command, out Match match))
            {
                return;
            }

            int x = Convert.ToInt32(match.Groups[XPosGroupName].Value);
            int y = Convert.ToInt32(match.Groups[YPosGroupName].Value);
            RobotOrientation orientation = ConvertToOrientation(match.Groups[orientationGroupName].Value);

            try
            {
                context.Robot = context.RobotBuilder.Create(context.Surface, x, y, orientation);
            }
            catch (Exception ex)
            {
                logger.Log($"Robot creation failed. {ex}");
            }
        }
예제 #15
0
        public Robot(ISurface surface, int x, int y, RobotOrientation orientation)
        {
            if (surface == null)
            {
                throw new ArgumentNullException(nameof(surface));
            }

            if (x < surface.MinX || x > surface.MaxX)
            {
                throw new ArgumentOutOfRangeException(nameof(x));
            }

            if (y < surface.MinY || y > surface.MaxY)
            {
                throw new ArgumentOutOfRangeException(nameof(y));
            }

            Surface     = surface;
            X           = x;
            Y           = y;
            Orientation = orientation;
        }
예제 #16
0
        public static int[] GetRobotDisplacement(RobotOrientation orientation)
        {
            int[] robotDisplacement = { 0, 0 };
            switch (orientation)
            {
            case RobotOrientation.North:
                robotDisplacement = North();
                break;

            case RobotOrientation.East:
                robotDisplacement = East();
                break;

            case RobotOrientation.South:
                robotDisplacement = South();
                break;

            case RobotOrientation.West:
                robotDisplacement = West();
                break;
            }
            return(robotDisplacement);
        }
예제 #17
0
 public void SetOrientation(RobotOrientation orientation)
 {
     this.orientation = orientation;
 }
예제 #18
0
        public string ConvertToRobCmd(int robot, double waitTime, List <int> path)
        {
            // Get current position and orientation of the bot
            RobotOrientation currentOrientation = _orientations[robot];
            DTOWaypoint      currentWaypoint    = _waypointManager.Translate(path.First());

            if (currentWaypoint != _positions[robot])
            {
                throw new InvalidOperationException("Path is not starting with the current position of the robot!");
            }

            // Store path just in case the robot does not turn towards the right direction - we can then re-execute it after backtracking
            if (!_botErrorCorrectionMode.ContainsKey(robot) || !_botErrorCorrectionMode[robot])
            {
                _botErrorCorrectionMode[robot] = false;
                _lastPaths[robot] = path;
            }

            // Init command list
            List <RobotActions> commands = new List <RobotActions>();

            // Build all commands necessary to follow the path starting with the second waypoint of the list
            foreach (var wpID in path.Skip(1))
            {
                // Get next waypoint
                DTOWaypoint nextWaypoint = _waypointManager.Translate(wpID);
                // Check whether the waypoints are connected at all
                if (!_waypointManager.IsConnected(currentWaypoint, nextWaypoint))
                {
                    throw new InvalidOperationException("Cannot go to a waypoint that is not connected with this one!");
                }

                // Get orientation we have to turn towards
                RobotOrientation currentGoalOrientation;
                double           xDelta = nextWaypoint.X - currentWaypoint.X; double yDelta = nextWaypoint.Y - currentWaypoint.Y;
                if (Math.Abs(xDelta) == 0 && Math.Abs(yDelta) == 0)
                {
                    throw new InvalidOperationException("Cannot go from a waypoint to itself.");
                }
                if (Math.Abs(xDelta) > Math.Abs(yDelta))
                {
                    if (xDelta > 0)
                    {
                        currentGoalOrientation = RobotOrientation.East;
                    }
                    else
                    {
                        currentGoalOrientation = RobotOrientation.West;
                    }
                }
                else
                if (yDelta > 0)
                {
                    currentGoalOrientation = RobotOrientation.North;
                }
                else
                {
                    currentGoalOrientation = RobotOrientation.South;
                }

                // See whether we have to turn
                switch (currentOrientation)
                {
                case RobotOrientation.North:
                    switch (currentGoalOrientation)
                    {
                    case RobotOrientation.North: /* Nothing to do */ break;

                    case RobotOrientation.West: commands.Add(RobotActions.TurnLeft); break;

                    case RobotOrientation.South: commands.Add(RobotActions.TurnLeft); commands.Add(RobotActions.TurnLeft); break;

                    case RobotOrientation.East: commands.Add(RobotActions.TurnRight); break;

                    default: throw new ArgumentException("Unknown orientation: " + currentGoalOrientation);
                    }
                    break;

                case RobotOrientation.West:
                    switch (currentGoalOrientation)
                    {
                    case RobotOrientation.North: commands.Add(RobotActions.TurnRight); break;

                    case RobotOrientation.West: /* Nothing to do */ break;

                    case RobotOrientation.South: commands.Add(RobotActions.TurnLeft); break;

                    case RobotOrientation.East: commands.Add(RobotActions.TurnLeft); commands.Add(RobotActions.TurnLeft); break;

                    default: throw new ArgumentException("Unknown orientation: " + currentGoalOrientation);
                    }
                    break;

                case RobotOrientation.South:
                    switch (currentGoalOrientation)
                    {
                    case RobotOrientation.North: commands.Add(RobotActions.TurnLeft); commands.Add(RobotActions.TurnLeft); break;

                    case RobotOrientation.West: commands.Add(RobotActions.TurnRight); break;

                    case RobotOrientation.South: /* Nothing to do */ break;

                    case RobotOrientation.East: commands.Add(RobotActions.TurnLeft); break;

                    default: throw new ArgumentException("Unknown orientation: " + currentGoalOrientation);
                    }
                    break;

                case RobotOrientation.East:
                    switch (currentGoalOrientation)
                    {
                    case RobotOrientation.North: commands.Add(RobotActions.TurnLeft); break;

                    case RobotOrientation.West: commands.Add(RobotActions.TurnLeft); commands.Add(RobotActions.TurnLeft); break;

                    case RobotOrientation.South: commands.Add(RobotActions.TurnRight); break;

                    case RobotOrientation.East: /* Nothing to do */ break;

                    default: throw new ArgumentException("Unknown orientation: " + currentGoalOrientation);
                    }
                    break;

                default: throw new ArgumentException("Unknown orientation: " + currentOrientation);
                }

                // Add the go command
                commands.Add(RobotActions.Forward);

                // Update markers of current situation
                currentWaypoint    = nextWaypoint;
                currentOrientation = currentGoalOrientation;
            }

            // Build and return the command string
            return(RobotTranslator.EncodeTo(waitTime, commands));
        }
예제 #19
0
 public IRobot Create(ISurface surface, int x, int y, RobotOrientation orientation)
 {
     return(new Robot(surface, x, y, orientation));
 }
예제 #20
0
 public void TurnLeft()
 {
     Orientation = (Orientation == RobotOrientation.North) ? RobotOrientation.West : Orientation - 1;
 }
예제 #21
0
 public void TurnRight()
 {
     Orientation = (Orientation == RobotOrientation.West) ? RobotOrientation.North : Orientation + 1;
 }
예제 #22
0
 public static void testMovingMoving(RobotOrientation orientation, int expectedX, int expectedY)
 {
     int[] expectedResult = { expectedX, expectedY };
     int[] actualResult   = MoveRobot.GetRobotDisplacement(orientation);
     Assert.Equal(expectedResult, actualResult);
 }