Ejemplo n.º 1
0
        /// <summary>
        /// Поднятие рабочего органа манипулятора
        /// </summary>
        /// <param name="time">Время</param>
        /// <param name="ZHight">Высота на которую поднимают (Z)</param>
        /// <param name="YHight">Координата манипулятора (Y)</param>
        /// <param name="x1">Последняя координата контура</param>
        /// <param name="y1">Последняя координата контура</param>
        /// <param name="Zplot">Высота рабочей области</param>
        /// <param name="timeUP">Время подьема</param>
        /// <returns></returns>
        public static IEnumerable <RobotPosition> Stop(double time, double ZHight, double YHight, double x1, double y1, double Zplot, double timeUP)
        {
            var aX = (2 * (YHight - y1) / Math.Pow(10, 6));              // ускорение по оси Х
            var aY = Math.Abs((2 * (ZHight - Zplot)) / Math.Pow(10, 6)); // ускорение по оси Y
            var aZ = (2 * (0 - x1)) / Math.Pow(10, 6) * (-1);            // ускорение по оси Z

            var a = Zplot;

            while (a <= Interpretation.Constants.StartZ)
            {
                RobotPosition result = new RobotPosition();
                result.time = time;
                result.z    = x1 - (aZ * Math.Pow(timeUP * 1000, 2)) / 2;
                result.x    = y1 + (aX * Math.Pow(timeUP * 1000, 2)) / 2;
                a           = Zplot + (aY * Math.Pow((1000 * timeUP), 2)) / 2;
                result.y    = a;
                timeUP      = 0.001 + timeUP;
                time        = 0.001 + time;
                a++;
                yield return(result);
            }
            var g = (int)(time + 1);

            while (time <= g)
            {
                RobotPosition result = new RobotPosition();
                result.time = time;
                result.z    = 0;
                result.x    = YHight;
                result.y    = ZHight;
                time        = 0.001 + time;
                yield return(result);
            }
        }
Ejemplo n.º 2
0
        public void ProcessCommand()
        {
            var rawcommand = _commandReader.GetCommand();
            var command    = _commandManager.ChooseCommand(rawcommand);

            Console.WriteLine($"Valid command received: {command!=null}");

            if (command != null)
            {
                RobotPosition position = null;
                var           result   = command.GetCommandResult(_robot.RobotPosition, out position);
                Console.WriteLine($"Command performed by robot: {result}");
                if (result)
                {
                    if (command is PlaceCommand || command is LeftCommand || command is RightCommand || command is MoveCommand)
                    {
                        _robot.UpdatePosition(position);
                    }
                    else if (command is ReportCommand)
                    {
                        _reportDisplay.ShowRobot(_robot);
                    }
                }
            }
        }
        public void Command_Manager_should_return_place_command_when_place_0_1_north_is_given()
        {
            var          coordinateValidator = new SurfaceCoordinateValidator();
            var          directionValidator  = new DirectionValidator();
            var          paramValidator      = new CommandParamValidator(coordinateValidator, directionValidator);
            var          mgr = new CommandManager(paramValidator, null);
            PlaceCommand cmd = (PlaceCommand)mgr.ChooseCommand("PLACE 0,1 NORTH");

            var coordinate = new SurfaceCoordinate {
                X_Position = 0, Y_Position = 1
            };

            PlaceCommandParam actualParam = (PlaceCommandParam)cmd.CommandParam;

            Assert.AreEqual <CommandType>(CommandType.PLACE, actualParam.CommandParamType);
            Assert.AreEqual <Direction>(Direction.NORTH, actualParam.Direction);
            Assert.AreEqual <SurfaceCoordinate>(coordinate, actualParam.SurfaceCoordinate);

            RobotPosition actualPosition = null;
            var           actual         = cmd.GetCommandResult(null, out actualPosition);

            Assert.AreEqual <bool>(true, actual);

            Assert.AreEqual <Direction>(Direction.NORTH, actualPosition.Direction);
            Assert.AreEqual <SurfaceCoordinate>(coordinate, actualPosition.Coordinate);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Get the proposed new position for the place command.
        /// Does validation check as well
        /// </summary>
        /// <param name="command">Command to place a toy robot</param>
        /// <returns>Proposed position</returns>
        private static RobotPosition GetNewPosition(string[] command)
        {
            if (command.Length < 2)
            {
                throw new Exception("Missing parameters.");
            }

            var parameters = command[1].Split(',');

            if (parameters.Length < 3)
            {
                throw new Exception("Missing parameters.");
            }

            var x         = int.Parse(parameters[0]);
            var y         = int.Parse(parameters[1]);
            var f         = parameters[2].ToUpper();
            var direction = (Direction)Enum.Parse(typeof(Direction), f);

            var newPosition = new RobotPosition()
            {
                X = x,
                Y = y,
                F = direction
            };

            return(newPosition);
        }
        private void HandleRobotCommands(List <RobotCommand> cmdList)
        {
            int counter = 0;

            foreach (var cmd in cmdList)
            {
                cmd.QueueId = counter;
                try
                {
                    Console.WriteLine($"Request command:{cmd.Type}: Direction:{cmd.CurentDirection} request new Position({cmd.MoveTo.X}:{cmd.MoveTo.Y})");
                    var result = _robotStateMachine.Build(cmd.Type).ProcessState(cmd);

                    if (result.isSuccess && result.stateResponse != null)
                    {
                        _currentPosition = result.stateResponse.CurrentPosition;
                        Console.WriteLine($"Success! Position was changed to: [{result.stateResponse.CurrentPosition.X}:{result.stateResponse?.CurrentPosition.Y}:{result.stateResponse?.CurrentPosition.Direction}]");
                    }
                    else
                    {
                        Console.WriteLine($"Position was not changed");
                    }
                }
                catch (Exception ex)
                {
                    _log.Exception(ex, $"Robot command critical failure");
                    Console.WriteLine($"{RobotConstantsValues.CommandFailure}");
                }

                counter++;
            }
        }
 public RobotLocationService()
 {
     _currentPosition = new RobotPosition {
         X = 0, Y = 0, Direction = Direction.N
     };
     _workingMatrixSize = new MatrixSize();
 }
Ejemplo n.º 7
0
        public RobotPosition NewPosition(int step = 1)
        {
            var newPosition = new RobotPosition
            {
                X         = this.X,
                Y         = this.Y,
                Direction = this.Direction,
            };

            if (newPosition.Direction == DirectionEnum.East)
            {
                newPosition.X += step;
            }
            else if (newPosition.Direction == DirectionEnum.West)
            {
                newPosition.X -= step;
            }
            else if (newPosition.Direction == DirectionEnum.North)
            {
                newPosition.Y += step;
            }
            else if (newPosition.Direction == DirectionEnum.South)
            {
                newPosition.Y -= step;
            }

            return(newPosition);
        }
Ejemplo n.º 8
0
        public void RobotPosition_Constructor_ValidData_ValidPosition()
        {
            string        input    = "1,1,WEST";
            RobotPosition position = new RobotPosition(input);

            Assert.AreEqual(input, position.ToString());
        }
        private RobotPosition GetExpectedPosition(RobotPosition currentPosition)
        {
            switch (currentPosition.Facing)
            {
            case Orientation.NORTH:
                if (currentPosition.Y == 3)
                {
                    return(new RobotPosition(currentPosition.X, 4, Orientation.NORTH));
                }
                break;

            case Orientation.EAST:
                if (currentPosition.X == 3)
                {
                    return(new RobotPosition(4, currentPosition.Y, Orientation.EAST));
                }
                break;

            case Orientation.SOUTH:
                if (currentPosition.Y == 1)
                {
                    return(new RobotPosition(currentPosition.X, 0, Orientation.SOUTH));
                }
                break;

            case Orientation.WEST:
                if (currentPosition.X == 1)
                {
                    return(new RobotPosition(0, currentPosition.Y, Orientation.WEST));
                }
                break;
            }

            return(currentPosition);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Calculates a new position applying one unit move in current direction
        /// </summary>
        /// <returns>Calculated new position</returns>
        private RobotPosition ApplyMove()
        {
            // initialize
            var newPosition = new RobotPosition()
            {
                X = robotPosition.X,
                Y = robotPosition.Y,
                F = robotPosition.F
            };

            if (robotPosition.F == Direction.NORTH)
            {
                newPosition.Y++;
            }
            else if (robotPosition.F == Direction.SOUTH)
            {
                newPosition.Y--;
            }
            else if (robotPosition.F == Direction.EAST)
            {
                newPosition.X++;
            }
            else if (robotPosition.F == Direction.WEST)
            {
                newPosition.X--;
            }

            return(newPosition);
        }
Ejemplo n.º 11
0
        public void Should_move_up_when_facing_north()
        {
            var expected = new RobotPosition {X = 0, Y = 1};

            _robotMover.Move('N', _currentRobotPosition);

            Assert.That(_currentRobotPosition, Is.EqualTo(expected));
        }
Ejemplo n.º 12
0
        public void RobotPosition_Constructor_YNegativeInt_GameCommandParameterInvalidException()
        {
            string input = "1,-2,WEST";

            Assert.ThrowsException <GameCommandParameterInvalidException>(() => {
                RobotPosition position = new RobotPosition(input);
            });
        }
Ejemplo n.º 13
0
 public void UpdatePosition(RobotPosition position)
 {
     if (position.Direction == Direction.UNKNOWN || !_surfaceCoordinateValidator.Validate(position.Coordinate))
     {
         throw new ArgumentException("Invalid position to update to robot!");
     }
     _robotPosition = position;
 }
Ejemplo n.º 14
0
        public void RobotPosition_Constructor_2Parts_GameCommandParameterInvalidException()
        {
            string input = "1,1";

            Assert.ThrowsException <GameCommandParameterInvalidException>(() => {
                RobotPosition position = new RobotPosition(input);
            });
        }
Ejemplo n.º 15
0
        public void Should_move_right_when_facing_west()
        {
            var expected = new RobotPosition {X = -1, Y = 0};

            _robotMover.Move('W', _currentRobotPosition);

            Assert.That(_currentRobotPosition, Is.EqualTo(expected));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Places the robot to flat surface.
        /// </summary>
        /// <returns><c>true</c>, if robot to flat surface was placed, <c>false</c> otherwise.</returns>
        /// <param name="expectedPosition">Expected position.</param>
        /// <param name="surface">Surface.</param>
        public static RobotPosition PlaceRobotToFlatSurface(RobotPosition expectedPosition, Surface surface)
        {
            if (expectedPosition == null)
            {
                return(null);
            }

            return(surface.InBoundary(expectedPosition) ? expectedPosition : null);
        }
Ejemplo n.º 17
0
        public void Place(RobotPosition position)
        {
            this.Position = ControllableResponses.PlaceRobotToFlatSurface(position, _surface);
            if (this.Position == null)
            {
                _exceptionFactory.GenerateSafeException($"the robot cannot place to expected postion");
            }

            this.Status = RobotStatus.On;
        }
Ejemplo n.º 18
0
        public void RobotPositionInputIncorrectFormat(string robotPositionInput)
        {
            Grid grid = new Grid()
            {
                GridLimit = new GridPoint(30, 30)
            };
            RobotPosition robotPosition = _parseInputsService.ParseRobotPosition(robotPositionInput, grid);

            Assert.IsNull(robotPosition);
            Assert.AreEqual(1, _parseInputsService.ParseMessages.Count);
        }
Ejemplo n.º 19
0
        public Robot(string name, RobotPosition position, INavigationService navigationService)
        {
            if (!navigationService.IsPositionWithinBoundaries(position))
            {
                throw new ArgumentOutOfRangeException("position", "Starting position is out of bound of the battle area.");
            }

            currentPosition        = position;
            this.navigationService = navigationService;
            this.name = name;
        }
Ejemplo n.º 20
0
 public void Place(RobotPosition newPosition)
 {
     if (IsValidMove(newPosition))
     {
         robotPosition = newPosition;
     }
     else
     {
         throw new System.Exception(INVALIDPOSITIONERROR);
     }
 }
Ejemplo n.º 21
0
        public void RobotPosition_TurnClockwise_North1Time_East()
        {
            string        original = $"1,1,{FacingDirection.NORTH.ToString()}";
            string        expected = $"1,1,{FacingDirection.EAST.ToString()}";
            RobotPosition position = new RobotPosition(original);

            position.Turn();
            RobotPosition expectedPosition = new RobotPosition(expected);

            Assert.AreEqual(expectedPosition.ToString(), position.ToString());
        }
        /// <summary>
        /// Rotate90s the degrees to right.
        /// </summary>
        /// <returns><c>true</c>, if degrees to right was rotate90ed, <c>false</c> otherwise.</returns>
        /// <param name="position">Position.</param>
        public static bool Rotate90DegreesToRight(RobotPosition position)
        {
            if (position == null)
            {
                return(false);
            }

            // the direction is sequenced in array, so less 1 to turn right
            position.Direction = verifyCompassBoundary(Convert.ToInt32(position.Direction) - 1);
            return(true);
        }
Ejemplo n.º 23
0
        public void RobotPositionInputCorrectlyParsed(string robotPositionInput, int XCoordinateParsed, int YCoordinateParsed, OrientationTypes orientationParsed)
        {
            Grid grid = new Grid()
            {
                GridLimit = new GridPoint(50, 50)
            };
            RobotPosition robotPosition = _parseInputsService.ParseRobotPosition(robotPositionInput, grid);

            Assert.AreEqual(XCoordinateParsed, robotPosition.PositionCoordinates.X_Coordinate);
            Assert.AreEqual(YCoordinateParsed, robotPosition.PositionCoordinates.Y_Coordinate);
            Assert.AreEqual(orientationParsed, robotPosition.RobotOrientation);
        }
Ejemplo n.º 24
0
 public void ParseTest(string input, bool successful, RobotPosition expectedPosition)
 {
     if (successful)
     {
         var position = RobotPositionParser.Parse(input);
         Assert.That(position, Is.EqualTo(expectedPosition));
     }
     else
     {
         Assert.Throws <System.ArgumentException>(() => RobotPositionParser.Parse(input));
     }
 }
Ejemplo n.º 25
0
        public void OutOfBoundTest(Point robotLocation)
        {
            var alwaysFaileNavigationService = new Mock <INavigationService>();

            alwaysFaileNavigationService.Setup(a => a.IsPositionWithinBoundaries(It.IsAny <RobotPosition>())).Returns(false);

            var position = new RobotPosition {
                Heading = Heading.East, Location = robotLocation
            };

            Assert.Throws <ArgumentOutOfRangeException>(() => new Robot(It.IsAny <string>(), position, alwaysFaileNavigationService.Object));
        }
Ejemplo n.º 26
0
        public RobotCommand ExecuteTurn(RobotCommand command)
        {
            IsChanged = false;
            Moves.Enqueue(command);
            NeighborCache.Clear();

            switch (command)
            {
            case RobotCommand.Up:
                MoveRobotTo(RobotCommand.Up, RobotPosition.Up());
                break;

            case RobotCommand.Down:
                MoveRobotTo(RobotCommand.Down, RobotPosition.Down());
                break;

            case RobotCommand.Left:
                MoveRobotTo(RobotCommand.Left, RobotPosition.Left());
                break;

            case RobotCommand.Right:
                MoveRobotTo(RobotCommand.Right, RobotPosition.Right());
                break;

            case RobotCommand.Abort:
                State = MapState.Aborted;
                Score = AbortScore;
                break;

            case RobotCommand.Shave:
                for (int dy = -1; dy < 2; dy++)
                {
                    for (int dx = -1; dx < 2; dx++)
                    {
                        if (Cell.At(RobotPosition.X + dx, RobotPosition.Y + dy).IsBeard())
                        {
                            Cell.Set(RobotPosition.X + dx, RobotPosition.Y + dy, CellType.Empty);
                            IsChanged = true;
                        }
                    }
                }
                break;
            }

            if (State == MapState.Valid)
            {
                Score -= 1;
                Simulate();
            }

            return(command);
        }
Ejemplo n.º 27
0
        public CleaningPlanResultsDto ExecuteCleaningProcess(RobotDto robotConfiguration)
        {
            var position = new RobotPosition(robotConfiguration.Start.X, robotConfiguration.Start.Y, robotConfiguration.Start.Facing);
            var robot    = new Robot(position, robotConfiguration.Battery);

            var instructions = new CleaningPlanInstructionsDto
            {
                Map          = robotConfiguration.Map,
                Instructions = new Queue <string>(robotConfiguration.Commands)
            };

            return(robot.ExecuteCleaningPlan(instructions));
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Validates if the position is a valid robot position
        /// </summary>
        /// <param name="position">New proposed robot position</param>
        /// <returns>Indicates a valid move (true/false)</returns>
        private bool IsValidMove(RobotPosition position)
        {
            if (position.X > SQUARETABLETOPUNIT || position.X < 0)
            {
                return(false);
            }

            if (position.Y > SQUARETABLETOPUNIT || position.Y < 0)
            {
                return(false);
            }

            return(true);
        }
        public void Report_console_display_should_report_successfully()
        {
            var reportDisplay = new ReportConsoleDisplay();
            var surfaceCoordinateValidator = new SurfaceCoordinateValidator();
            SurfaceCoordinate coordinate   = new SurfaceCoordinate()
            {
                X_Position = 0, Y_Position = 1
            };
            var pos   = new RobotPosition(Direction.NORTH, coordinate);
            var robot = new Robot(0, "test", surfaceCoordinateValidator);

            robot.UpdatePosition(pos);
            reportDisplay.ShowRobot(robot);
        }
Ejemplo n.º 30
0
        public virtual void UpdatePositionAndClean(RectangularRoom room, Random random)
        {
            Position newPos = RobotPosition.GetNewPosition(RobotPosition, Direction, Speed);

            if (room.isPositionInRoom(newPos))
            {
                room.cleanTileAtPosition(RobotPosition);
                RobotPosition = newPos;
            }
            else
            {
                Direction = random.Next(360);  //Robotul schimba directia cand se loveste de perete
            }
        }
        public void Place_command_throw_arguent_exception_for_incorrect_command_param()
        {
            var originalPos = new SurfaceCoordinate {
                X_Position = 0, Y_Position = 0
            };
            var robotPosition = new RobotPosition(Direction.SOUTH, originalPos);
            var validator     = new SurfaceCoordinateValidator();
            var param         = new MoveCommandParam();
            var cmd           = new PlaceCommand(param, validator);

            RobotPosition actualPosition = null;

            //var actual = cmd.GetCommandResult(out actualPosition);
            Assert.ThrowsException <ArgumentException>(() => cmd.GetCommandResult(null, out actualPosition));
        }
Ejemplo n.º 32
0
        public void GameTests_RandomMove_CheckExpectedPosition_Equal(string[] events, string expectedPosition)
        {
            //int x=0,y=0;
            //FacingDirection face=FacingDirection.WEST;
            Game game = new Game();

            foreach (string gameEvent in events)
            {
                GameEvent temp = new GameEvent(gameEvent);
                game.ReceiveEvent(temp);
            }
            RobotPosition expectedPos = new RobotPosition(expectedPosition);

            Assert.AreEqual(expectedPos.ToString(), game.ReportCurrentPosition());
        }
Ejemplo n.º 33
0
 public void Move(char currentHeading, RobotPosition currentRobotPosition)
 {
     switch (currentHeading) {
         case 'N':
             currentRobotPosition.Up();
             break;
         case 'E':
             currentRobotPosition.Right();
             break;
         case 'W':
             currentRobotPosition.Left();
             break;
         case 'S':
             currentRobotPosition.Down();
             break;
     }
 }
Ejemplo n.º 34
0
 public void SetUp()
 {
     _robotMover = new RobotMover();
     _currentRobotPosition = new RobotPosition {X = 0, Y = 0};
 }
Ejemplo n.º 35
0
        /// <summary>
        /// Separate the instructions and arguments from the send argument strings
        /// </summary>
        /// <param name="command"></param>
        /// <param name="args"></param>
        /// <returns>string</returns>
        public Instruction GetInstruction(string command, ref RobotPosition args)
        {
            Instruction instruction;
            string arguments = "";

            string[] separatearguments = command.Split(' ');
            if(separatearguments.Length>1)
            {
                command = separatearguments[0];
                arguments = separatearguments[1];
            }
            command = command.ToUpper();

            if (Enum.TryParse<Instruction>(command, true, out instruction))
            {
                if (instruction == Instruction.Place)
                {
                    if (!ParsePlaceArguments(arguments, ref args))
                    {
                        instruction = Instruction.Invalid;
                    }
                }
            }
            else
            {
                instruction = Instruction.Invalid;
            }
            return instruction;
        }
Ejemplo n.º 36
0
        /// <summary>
        /// Parse arguments received with Place Instruction
        /// </summary>
        /// <param name="argumentString"></param>
        /// <param name="args"></param>
        /// <returns>bool</returns>
        private bool ParsePlaceArguments(string argumentString, ref RobotPosition args)
        {
            var arguments = argumentString.Split(',');
            int x, y;
            FacingDirection facingdirection;

            if (arguments.Length == 3 && GetCoordinate(arguments[0], out x) &&
                GetCoordinate(arguments[1], out y) && GetFacingDirection(arguments[2], out facingdirection))
            {
                args = new RobotPosition
                {
                    X = x,
                    Y = y,
                    FacingDirection = facingdirection,
                };
                return true;
            }
            return false;
        }